How do I define scalar, vector and matrix data in the workspace as a struct to load into multiple root-level inports in a Simulink model?

2 views (last 30 days)
I am following the workflow in the "Loading Data Structures to Root-Level Inputs" section of the MATLAB Documentation titled Load Data to Root-Level Input Ports to define inputs to multiple root-level inports in my Simulink model, each of which has different dimensions. 
In particular, I have one inport with dimensions "1" , one with dimensions "3", and one with dimensions "[2 3]". The workflow that I'm currently doing is leading to this error:
Error loading external input data into root-level Inport <inportName> in model <modelName>.
How do I set this up? 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Nov 2025
The below script shows how to set it up with the correct dimensions in the .values fields, with the data in the workspace stored in a variable named "input_data".
For a sample time "dT" of 0.1, and number of samples "N" set to 100, this script demonstrates how to set the relevant fields of "input_data" for input data that is a scalar, 1x3 vector, and 2x3 matrix respectively. 
For demonstration purposes, the values of the inputs, "val", is set to 0 at all timesteps, but you would modify "val" to contain your desired input values, making sure that it remains the same length as the "input_data.time" vector. 
dT = 0.1; % Sample time
N = 100; % Number of samples
input_data.time = dT*(0:(N-1))';
time_vec = input_data.time;
% Input is a scalar
input_data.signals(1).values = 0*time_vec; % Modify this as desired to your input values
input_data.signals(1).dimensions = 1; % feeds an input port with dimensions = -1
% Input is a 1x3 vector
val = zeros(N,3); % N x 3 matrix where N is number of timesteps.
input_data.signals(2).values = val; % Modify this as desired to your input values
input_data.signals(2).dimensions = 3; % feeds an inport port with dimensions = 3
% Input is a 2x3 matrix
val = zeros(2,3,N);
input_data.signals(3).values = val; % Modify this as desired to your input values
input_data.signals(3).dimensions = [2 3]; % feeds an inport port with dimensions = [2 3]
Then, prior to running your model, you should: 
-In the Configuration Parameters, set the "Input" field to "input_data": 
Once this data is defined as described, you should be able to run your model as expected. 

More Answers (0)

Categories

Find more on Large-Scale Modeling in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!