Load Data Using the From Workspace Block

To load signal data with a From Workspace block:

  1. Create a workspace variable with the signal data that you want to load.

  2. Add a From Workspace block to a model. Connect the From Workspace block to the block that the From Workspace block provides input to.

  3. Double-click the From Workspace block and configure:

    • The workspace data to load

    • The data format for the From Workspace block output

    • How the data is loaded, including sample time, how data for missing data points are handled, and whether to use zero-crossing detection

    Suppose that the workspace contains a column vector of times named T and a column vector of corresponding signal values named U. Entering the expression [T U] for Data parameter yields the required input array. If the required array or structure exists in the workspace, enter the name of the structure or matrix in the Data parameter.

An alternative to using a From Workspace block for loading workspace data is to load data to a root-level input port. For more information, see Root-Level Input Ports.

Specify the Workspace Data

Double-click the From Workspace block, and in the Data parameter, specify the workspace data to load. Specify a MATLAB® expression (for example, the name of a variable in the MATLAB workspace) that evaluates to one of the following:

  • A timeseries or timetable object

    Real signals of type double can be in any format that the From Workspace block supports. For complex signals and real signals of a data type other than double, use any format except Array.

  • A structure of timeseries or timetable objects

    For bus data, use a structure of timeseries or timetable objects. Match the bus hierarchy and specify a timeseries or timetable object for each leaf signal in the bus. Set up the data the same way as you do for loading bus signals to a root-level Inport block. For details, see Load Bus Data to Root-Level Input Ports.

  • A structure, with or without time

    For details, see Specify Structure Data for the From Workspace Block.

  • A two-dimensional matrix

    You can use a matrix to specify only one-dimensional signals. The first element of each matrix row is a timestamp. The rest of each row is a scalar or vector of signal values.

Note

When you specify timetable data to load, each timetable object can contain data for only one signal.

Specify Structure Data for the From Workspace Block

You can use a structure for one-dimensional or multidimensional signals, with or without time values. For the structure, use this format:

  • A signals.values field, which contains a column vector of signal values.

  • An optional signals.dimensions array, which contains the dimensions of the signal.

  • An optional time vector of doubles, which is a column vector of timestamps.

    The nth time element is the timestamp of the nth signals.values element.

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) — Use a structure that has an empty time vector.

  • Continuous signals (the signal is defined for all values of time) — 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 examples, 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.

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

  • 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. M and N are the dimensions of each matrix input and T is the number of time points. Suppose that you want to input 51 time samples of a 4-by-5 matrix signal into one of your model 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 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.

For continuous signals, you can specify a time field, which contains a time vector. How you specify the time values depends on the kind of signal data that you want.

For information about defining MATLAB structures, see Create Structure Array.

Signal DataTime Data Recommendation

Evenly spaced discrete signals

Use an expression in this form:

timeVector = timeStep * [startTime:numSteps-1]' 

The vector is transposed. Also, because the start time is a time step, you need specify the number of steps you want minus 1. For example, to specify 50 time values at 0.2 time steps:

T1 = 0.2 * [0:49]' 

Note

Do not use an expression in this form:

timeVector = [startTime:timeStep:endTime]' 

For example, do not use:

T2 = [0:0.2:10]' 

This time vector form is not equivalent to the form that multiplies by time steps (T1), because of double-precision rounding used by computers. Simulink® expects exact values, with no double-precision rounding. Using the T2 form can lead to unexpected simulation results.

Unevenly spaced values

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

The From Workspace, From File, and Signal Editor blocks support zero-crossing detection. If the root-level input port is connected to one of those blocks, you can specify a zero-crossing time by using a duplicate time entry.

If you load a structure that does not specify a time vector:

  1. Set Sample time (-1 for inherited) to a value other than 0 (continuous).

  2. Clear Interpolate data.

  3. Set Form output after final data value by to a value other than Extrapolation.

Use Data from a To File Block

You can use the From Workspace block to load data exported by a To Workspace block in a previous simulation for use in a later simulation. Save the To Workspace block data in either Timeseries or Structure with Time format. Loading data that was exported to a file by a To File block using MATLAB timeseries does not require that you change the data.

If you set the To File block Save format parameter to Array, transpose the exported array data. The data saved by the To File block contains columns with consecutive timestamps, followed by the corresponding data. The transposed data contains rows with consecutive timestamps, followed by the corresponding data. To provide the required format, use MATLAB load and transpose commands with the MAT-file. To avoid transposing the data again, resave the transposed data.

Load Dataset Data

To use workspace data that is in the Simulink.SimulationData.Dataset format, extract a timeseries or timetable object from the Dataset object. For example, if you use signal logging with the Dataset format and use the default output variable logsout, for a single logged signal enter:

logsout.get(1).values

Specifying Variable-Size Signals

You can use a To Workspace block (with the Structure or Structure With Time format) or a root Outport block to log variable-size signals. Then use the To Workspace variable with the From Workspace block.

Alternatively, create a MATLAB structure that contains variable-size signal data. For each values field in the structure, include a valueDimensions field that specifies the run-time dimensions for the signal. For details, see Simulink Models Using Variable-Size Signals.

Store Data for Model Linked to Data Dictionary

When you use a From Workspace block in a model that is linked to a data dictionary, you must choose the location to store the data that the block refers to. Set the value of the Data parameter based on the workspace or dictionary that contains the target data to load. For more information, see Load Data Using the From Workspace Block.

Sample Time

The From Workspace block Sample time parameter specifies the sample time to load data from a workspace. The timestamps in the workspace data must be monotonically nondecreasing. For details, see Specify Sample Time.

Interpolate Missing Data Values

To use linear Lagrangian interpolation to compute data values for time hits that occur between the time hits for which the workspace supplies the data, select Interpolate data.

For variable-size signals, clear Interpolate data.

Specify Output After Final Data

To determine the block output after the last time hit for which workspace data is available, combine the settings of these parameters:

  • Interpolate data

  • Form output after final data value by

In the From Workspace block documentation, see the Form output after final data value by parameter.

Detect Zero Crossings

By default, the From Workspace block does not enable zero-crossing detection. Zero-crossing detection locates discontinuities, without resorting to excessively small time steps.

The Enable zero-crossing detection parameter applies only if the sample time is continuous (0).

If you select the Enable zero-crossing detection parameter, and if an input array contains multiple entries for the same time hit, Simulink detects a zero crossing at that time hit.

For bus signals, Simulink detects zero crossings across all leaf bus elements.

See Also

Blocks

Related Topics