Import Data Structures

Data Structures

The Simulink® software can read data from the workspace in the form of a structure, whose name you specify in the Configuration Parameters > Data Import/Export > Input parameter.

For information about defining MATLAB® structures, see Create a Structure Array in the MATLAB documentation.

The structure always includes a signals substructure, which contains a values field and a dimensions field. For details about the signal data, see Specify Signal Data. Depending on the modeling task that you want to perform, the structure can also include a time field.

You can specify structures for the model as a whole or on a per-port basis. For information about specifying per-port structures for the Input parameter, see One Structure for All Ports or a Structure for Each Port.

The form of a structure that you use depends on whether you are importing data for discrete signals (the signal is defined at evenly-spaced values of time) or continuous signals (the signal is defined for all values of time). For discrete signals, use a structure that has an empty time vector. For continuous signals, the approach that you use depends on whether the data represents a smooth curve or a curve that has discontinuities (jumps) over its range. For details, see:

For both discrete and continuous signals, specify a signals field, which contains an array of substructures, each of which corresponds to a model input port. For details, see Specify Signal Data.

For continuous signals, you may want to specify a time field, which contains a time vector. See Time and Signal Values for Imported Data.

One Structure for All Ports or a Structure for Each Port

You can specify one structure to provide input to all root-level input ports in a model, or you can specify a separated structure for each port.

The per-port structure 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.

The rest of the section about importing structure data focuses on specifying one structure for all ports.

Specify Signal Data

Each signals substructure must contain two fields: values and dimensions.

The Values Field

The values field must contain an array of inputs for the corresponding input port. If you specify a time vector, each input must correspond to a time value specified in the time field.

If the inputs for a port are scalar or vector values, the values field must be an M-by-N array. If you specify a time vector, M must be the number of time points specified by the time field and N is the length of each vector value.

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.

The Dimensions 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 or the Trigger block 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.

Specify Time Data

You can specify a time vector as part of the data structure to import. The Time and Signal Values for Imported Data section indicates when you may want to add a time vector.

The following table provides recommendations for how to specify time values, based on the kind of signal data you want to import.

Signal DataTime Data Recommendation

Inport or Trigger block with a discrete sample time

Do not specify a time vector. Simulink reads one signal value at each time step.

Evenly-spaced discrete signals

Consider using an expression in the following form:

TimeVector = Ts * (0:N); 

where Ts is the time step and N is the number of time steps.

Unevenly-spaced values

Use any valid MATLAB array expression; for example, [1:5 5:10] or (1 6 10 15).

If the root-level input port is from a From Workspace, From File, or Signal Builder block, which support zero-crossing detection, you can specify a zero-crossing time by using a duplicate time entry.

Examples of Specifying Signal and Time Data

In the first example, consider the following model that has a single input port:

  1. Create an input structure for loading 11 time samples of a two-element signal vector of type int8 into the model:

    N = 10
    Ts = 0.1
    a.time = (0:N)*Ts';
    c1 = int8([0:1:10]');
    c2 = int8([0:10:100]');
    a.signals(1).values = [c1 c2];
    a.signals(1).dimensions = 2;
  2. In the Configuration Parameters > Data Import/Export > Input parameter edit box, specify the variable a.

  3. In the Inport block dialog box, in the Signal Attributes tab, set Port dimensions to 2 and Data type to int8.

As another example, consider a model that 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 structure, 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;

Enter the structure name (a) in the Configuration Parameters > Data Import/Export > Input parameter edit box.

    Note:   Note that in this model you do not need to specify the dimension and data type, because the default values are 1 and double.

Was this topic helpful?