| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
The Simulink software can input data from a workspace and apply it to the model's top-level input ports during a simulation run. To specify this option:
Select the Input box in the Load from workspace area of the Data Import/Export Pane pane.
Enter an external input specification in the adjacent edit box and click Apply.
The Simulink software resolves symbols used in the external input specification as described in Resolving Symbols. See the documentation of the sim command for some data import capabilities that are available only for programmatic simulation.
Note The use of the Input box is independent of the setting of the Format list on the Data Import/Export pane. |
The input data can take any of the following forms:
Time series — see Importing Time-Series Data
Array — see Importing Data Arrays
Time expression — see Using a MATLAB Time Expression to Import Data
Structure — see Importing Data Structures
The Simulink software linearly interpolates or extrapolates input values as necessary if the Interpolate data option is selected for the corresponding Inport.
Any root-level Inport block can import data specified by a time-series object (see Simulink.Timeseries) residing in a workspace. In addition, any root-level input port defined by a bus object (see Simulink.Bus) can import data from a time-series array object (see Simulink.TSArray) that has the same structure as the bus object. Time-series objects are a derivation of standard MATLAB time-series objects and, therefore, can be manipulated using the MATLAB Time Series Tools. See "Using Time Series Tools" in the MATLAB Data Analysis documentation for further details.
Importing time-series objects allows you to import data logged by a previous simulation run (see Logging Signals). For example, suppose that you have a model that references several other models. You could use data logged from the inputs of the referenced models when simulating the top model as inputs for the referenced models simulated by themselves. This allows you to test the referenced models independently of the top model and each other.
To import data from time-series objects and time-series array objects, enter a comma-separated list of variables or expressions into the Input edit field on the Data Import/Export pane of the Configuration Parameters dialog box (see Configuration Parameters Dialog Box). Each variable or expression in the Input list should evaluate to the appropriate time-series object or time-series array object that corresponds to one of the model's root-level input ports, with the first item corresponding to the first root-level input port, the second to the second root-level input port, and so on. The model sldemo_mdlref_counter_bus, referenced by the top model sldemo_mdlref_bus, contains an example of importing time-series objects.
To use this demo, open sldemo_mdlref_bus and run the simulation. The top model is configured to store logged signals into a variable named topOut. Currently, two signals are being logged: COUNTERBUS and OUTPUTBUS. After running the simulation, you can view the logged signals by typing topOut at the MATLAB prompt.
topOut = Simulink.ModelDataLogs (sldemo_mdlref_bus): Name Elements Simulink Class COUNTERBUS 2 TsArray OUTPUTBUS 2 TsArray
The variable topOut is a Simulink.ModelDataLogs object that contains, in this case, two Simulink.TsArray objects corresponding to the two logged bus signals. The Simulink.TsArray object COUNTERBUS can be used as the input to the submodel sldemo_mdlref_counter_bus to run this model independently of the top model. This is accomplished by entering topOut.COUNTERBUS into the Input edit field on the Data Import/Export pane of the Configuration Parameters dialog box, as shown below.

By using the time-series array object as the input to sldemo_mdlref_counter_bus, independently running this model produces the same output as when run within the top model sldemo_mdlref_bus.
This import format consists of a real (noncomplex) matrix of data type double. The first column of the matrix must be a vector of times in ascending order. The remaining columns specify input values. In particular, each column represents the input for a different Inport block signal (in sequential order) and each row is the input value for the corresponding time point.
The total number of columns of the input matrix must equal n + 1, where n is the total number of signals entering the model's input ports.
The default input expression for a model is [t,u] and the default input format is Array. So if you define t and u in the MATLAB workspace, you need only select the Input option to input data from the model workspace. For example, suppose that a model has two input ports, In1 that accepts two signals, and In2 that accepts one signal. Also, suppose that the MATLAB workspace defines t and u as follows:
t = (0:0.1:1)'; u = [sin(t), cos(t), 4*cos(t)];
When the simulation runs, the signals sin(t) and cos(t) will be assigned to In1 and the signal 4*cos(t) will be assigned to In2.
Note The array input format allows you to load only real (noncomplex) scalar or vector data of type double. Use the structure format to input complex data, matrix (2-D) data, and/or data types other than double. |
You can use a MATLAB time expression to import data from a workspace. To use a time expression, enter the expression as a string (i.e., enclosed in single quotes) in the Input field of the Data Import/Export pane. The time expression can be any MATLAB expression that evaluates to a row vector equal in length to the number of signals entering the model's input ports. For example, suppose that a model has one vector Inport that accepts two signals. Furthermore, suppose that timefcn is a user-defined function that returns a row vector two elements long. The following are valid input time expressions for such a model:
'[3*sin(t), cos(2*t)]' '4*timefcn(w*t)+7'
The expression is evaluated at each step of the simulation, applying the resulting values to the model's input ports. Note that the Simulink software defines the variable t when it runs the simulation. Also, you can omit the time variable in expressions for functions of one variable. For example, the expression sin is interpreted as sin(t).
The Simulink software can read data from the workspace in the form of a structure whose name is specified in the Input text field. You can import structures that include only signal data or both signal and time data. The type of data structure is evaluated based on the structure itself.
Limitation: You cannot import fixed-point data that is contained in a structure. Consider using a Simulink.Timeseries object instead of a structure.
To import structures that include both signal and time data, the input structure must have two top-level fields: time and signals. The time field contains a column vector of the simulation times. The signals field contains an array of substructures, each of which corresponds to a model input port.
Each signals substructure must contain two fields named values and dimensions, respectively. The values field must contain an array of inputs for the corresponding input port where each input corresponds to a time point specified by the time field. The dimensions field specifies the dimensions of the input. If each input is a scalar or vector (1-D array) value, the dimensions field must be a scalar value that specifies the length of the vector (1 for a scalar). If each input is a matrix (2-D array), the dimensions field must be a two-element vector whose first element specifies the number of rows in the matrix and whose second element specifies the number of columns.
Note You must set the Port dimensions parameter of the Inport to be the same value as the dimensions field of the corresponding input structure. If the values differ, an error message is displayed when you try to simulate the model. |
If the inputs for a port are scalar or vector values, the values field must be an M-by-N array where M is the number of time points specified by the time field and N is the length of each vector value. For example, the following code creates an input structure for loading 11 time samples of a two-element signal vector of type int8 into a model with a single input port:
a.time = (0:0.1:1)'; c1 = int8([0:1:10]'); c2 = int8([0:10:100]'); a.signals(1).values = [c1 c2]; a.signals(1).dimensions = 2;
To load this data into the model's input port, you would select the Input option on the Data Import/Export pane and enter a in the input expression field.
If the inputs for a port are matrices (2-D arrays), the values field must be an M-by-N-by-T array where M and N are the dimensions of each matrix input and T is the number of time points. For example, suppose that you want to input 51 time samples of a 4-by-5 matrix signal into one of your model's input ports. Then, the corresponding dimensions field of the workspace structure must equal [4 5] and the values array must have the dimensions 4-by-5-by-51.
As another example, consider the following model, which has two inputs.

Suppose that you want to input a sine wave into the first port and a cosine wave into the second port. To do this, define a vector, a, as follows, in the MATLAB workspace:
a.time = (0:0.1:1)'; a.signals(1).values = sin(a.time); a.signals(1).dimensions = 1; a.signals(2).values = cos(a.time); a.signals(2).dimensions = 1;
Select the Input box for this model, and enter a in the adjacent text field.
The Structure format is the same as the Structure with time format except that the time field is empty. For example, in the preceding example, you could set the time field as follows:
a.time = []
In this case, the input for the first time step is read from the first element of an input port's value array, the value for the second time step from the second element of the value array, etc. If you enter the structure without time, the Inport block must have a discrete sample time.
This format consists of a separate structure-with-time or structure-without-time for each port. Each port's input data structure has only one signals field. To specify this option, enter the names of the structures in the Input text field as a comma-separated list, in1, in2,..., inN, where in1 is the data for your model's first port, in2 for the second input port, and so on.
In some cases, the Simulink software calculates block sample hits at sample times different from those specified by a time vector generated in MATLAB. Typically, these are small floating point inaccuracies that can cause the Simulink product to apparently miss a specified time step in lieu of a different sample point. In order to avoid these numerical inaccuracies, generate the time vector either in MATLAB or in Simulink based on the fundamental sample time of the model.
For example, if the model has a fundamental sample time Ts (see Purely Discrete Systems) of 0.001 then the time vector Tvector should be calculated with the command
Tvector = Ts*[n1, n2, n3...];
where n1, n2, n3, etc. are integers that, when multiplied by the fundamental sample time, yield the desired time vector.
![]() | Logging Signals | Exporting Data to the MATLAB Workspace | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |