| Using Simulink | ![]() |
Importing and Exporting Simulation Data
Simulink allows you to import input signal and initial state data from the MATLAB workspace and export output signal and state data to the MATLAB workspace during simulation. This capability allows you to use standard or custom MATLAB functions to generate a simulated system's input signals and to graph, analyze, or otherwise postprocess the system's outputs. See the following sections for more information:
Importing Input Data from the MATLAB Workspace
Simulink can apply input from a model's base workspace to the model's top-level inports during a simulation run. To specify this option, select the Input box in the Load from workspace area of the Data Import/Export pane (see Data Import/Export Pane). Then, enter an external input specification (see below) in the adjacent edit box and click Apply.
The input data can take any of the following forms.
Importing Data Arrays
To use this format, select Input in the Load from workspace pane and select the Array option from the Format list on the Data Import/Export pane. Selecting this option causes Simulink to evaluate the expression next to the Input check box and use the result as the input to the model.
The expression must evaluate to 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. Simulink linearly interpolates or extrapolates input values as necessary if the Interpolate data option is selected for the corresponding Inport.
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 inports.
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 base workspace, you need only select the Input option to input data from the model's base workspace. For example, suppose that a model has two inports, one of which accepts two signals and the other of which accepts one signal. Also, suppose that the base workspace defines u and t as follows:
Using a MATLAB Time Expression to Import Data
You can use a MATLAB time expression to import data from the MATLAB 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 inports. 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:
Simulink evaluates the expression at each step of the simulation, applying the resulting values to the model's inports. Note that Simulink 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, Simulink interprets the expression sin as sin(t).
Importing Data Structures
Simulink 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.
Importing signal-and-time data structures. To import structures that include both signal and time data, select the Structure with time option on from the Format list on the Data Import/Export pane. 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, Simulink stops and displays an error message 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 inport, 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 base 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, enter a in the adjacent text field, and select StructureWithTime as the I/O format.
Importing Signal-Only Structures. 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:
In this case, Simulink reads the input for the first time step from the first element of an inport's value array, the value for the second time step from the second element of the value array, etc.
Per-Port Structures. 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 inport, and so on.
| Choosing a Variable-Step Solver | Exporting Output Data to the MATLAB Workspace | ![]() |
© 1994-2005 The MathWorks, Inc.