Documentation Center

  • Trials
  • Product Updates

From Workspace

Read data from workspace

Library

Sources

Description

The From Workspace block reads data from a workspace and outputs the data as a signal.

The From Workspace icon displays the expression specified in the Data parameter. For details about how Simulink® software evaluates this expression, see Symbol Resolution.

Specifying the Workspace Data

In the Data parameter of the block, enter a MATLAB® expression that specifies the workspace data. The expression must evaluate to one of the following:

  • A MATLAB timeseries object

  • A structure of MATLAB timeseries objects

  • An array or structure containing an array of simulation times and corresponding signal values

The format of a MATLAB timeseries object, matrix, or structure loaded (imported) by a From Workspace block is the same as that used to load root-level input port data from the workspace.

Using Data Saved by a To File Block

You can use the From Workspace block to load MATLAB timeseries object data that was saved by a To File block, without making any changes to the data.

To use the From Workspace block to load Array format data, before the From Workspace block loads the data that a To File block saved, transpose the array data. The data saved by the To File block contains consecutive time stamps at the beginnings of columns, followed by the corresponding data. The transposed data contains consecutive time stamps at the beginning of rows, followed by the corresponding data. To provide the required format, use MATLAB load and transpose commands with the MAT-file (see Reshaping a Matrix). To avoid having to transpose the data again later, resave the transposed data for future use.

Using Data Saved by a To Workspace Block

To 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. For details, see Techniques for Importing Signal Data.

Loading Variable-Size Signals

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

Alternatively, you can create a MATLAB structure to contain 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.

Interpolating Missing Data Values

If you select the Interpolate data option, the block uses linear Lagrangian interpolation to compute data values for time steps that occur between time steps for which the workspace supplies data.

For variable-size signals, clear Interpolate data.

Specifying Output After Final Data

Combine the settings of the Form output after final data value by and Interpolate data parameters to determine the block output after the last time step for which workspace data is available. For details, see the Form output after final data value by parameter.

Detecting Zero Crossings

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 the input array contains more than one entry for the same time step, Simulink detects a zero crossing at that time step.

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

Data Type Support

The From Workspace block accepts data from the workspace and outputs real or complex signals of any type supported by Simulink, including fixed-point and enumerated data types.

The From Workspace block also accepts a bus object as a data type. To load bus data, use a structure of MATLAB timeseries objects. For details, see Import Structures of timeseries Objects for Buses.

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.

Parameters and Dialog Box

Data

A MATLAB expression that evaluates to one of the following:

  • A MATLAB timeseries object

  • A structure of MATLAB timeseries objects

    To load bus data, use a structure of MATLAB timeseries objects. For details, see Import Structures of timeseries Objects for Buses.

  • A two-dimensional matrix:

    • The first element of each matrix row is a time stamp.

    • The rest of each row is a scalar or vector of signal values.

    The leftmost element of each row is the time stamp of the value(s) in the rest of the row.

  • A structure, with or without time, which contains:

    • An array or matrix named signals.values, which contains scalars or vectors of signal values.

    • An array named signals.dimensions, which contains the dimensions of the signals.

    • Optionally, a vector named time, which contains time stamps.

    The n'th time element is the time stamp of the n'th signals.values element. For details, see Techniques for Importing Signal Data.

For example, 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 this parameter yields the required input array. If the required array or structure already exists in the workspace, enter the name of the structure or matrix in this field.

Output data type

The required data type for the data for the workspace variable that the From Workspace block loads. For non-bus types, you can use Inherit: auto to skip any data type verification. For more information, see Specify Block Output Data Types.

To load bus data, use a structure of MATLAB timeseries objects. For details, see Import Structures of timeseries Objects for Buses.

  • Inherit: auto — Default.

  • double

  • single

  • int8

  • uint8

  • int16

  • uint16

  • int32

  • uint32

  • boolean

  • fixdt(1,16,0) — Data type is fixed-point (1,16,0).

  • fixdt(1,16,2^0,0) — Data type is fixed-point (1,16,2^0,0).

  • Enum: <class_name> — Data type is enumerated, for example, Enum: Basic Colors.

  • Bus: <bus_object> — Data type is a bus object.

  • <data type expression> — The name of a data type object, for example Simulink.NumericType. Do not specify a bus object as the expression.

>> (Show data type assistant)

Displays the Data Type Assistant, to help you to set the Output data type parameter.

Mode

The category of data to specify. For more information, see Specify Block Output Data Types.

  • Inherit — Inheritance rule for data types. Selecting Inherit enables a second menu/text box to the right. (Default)

  • Built in — Built-in data types. Selecting Built in enables a second menu/text box to the right. Select one of the following choices:

    • double — Default

    • single

    • int8

    • uint8

    • int16

    • uint16

    • int32

    • uint32

    • boolean

    • Fixed point — Fixed-point data types

    • Enumerated — Enumerated data types. Selecting Enumerated enables a second menu/text box to the right, where you can enter the class name.

    • Bus — Bus object. Selecting Bus enables a Bus object parameter to the right, where you enter the name of a bus object that you want to use to define the structure of the bus. If you need to create or change a bus object, click Edit (to the right of the Bus object field) to open the Simulink Bus Editor. For details, see Manage Bus Objects with the Bus Editor.

    • Expression — Expression that evaluates to a data type. Selecting Expression enables a second menu/text box to the right, where you enter the expression. Do not specify a bus object as the expression.

Sample time

Sample rate of data from the workspace. For details, see Specify Sample Time.

Interpolate data

Select this option to have the block linearly interpolate at time steps for which no corresponding workspace data exists. Otherwise, the current output equals the output at the most recent time for which data exists.

If you select this option, the block linearly interpolates a missing data point from the two known data points between which it falls. For example, suppose the block reads the following time series from the workspace:

time:      1   2   3    4
signal:   253 254  ?  256

The block would output:

time:      1   2   3    4
signal:   253 254 255  256

If you clear the Interpolate data option, the block uses the most recent data value supplied from the workspace to provide any missing data values. For example, the result of the incomplete set of signal values shown above would be:

time:      1   2   3    4
signal:   253 254 254  256

Integer data — If the input data type is an integer type and an interpolated data point exceeds the data type's range, the block sets the missing data point to be the maximum value that the data type can represent. Similarly, if the interpolated or extrapolated value is less than the minimum value that the data type can represent, the block sets the missing data point to the minimum value that the data type can represent. For example, suppose that the data type is uint8 and the value interpolated for a missing data point is 256.

time:      1   2   3   4
signal:   253 254 255  ?

In this case, the block sets the value of the missing point to 255, the largest value that can be represented by the uint8 data type:

time:      1   2   3    4
signal:   253 254 255  255

Boolean data — If the input data is boolean, the block uses the value of the nearest workspace data point as the value of missing data point when determining missing data points that fall between the first and last known points. For example, suppose the workspace supplies values at time steps 1 and 4 but not at 2 and 3:

time:     1 2 3 4
signal:   1 ? ? 0

In this case, the block would use the value of data point 1 as the value of data point 2 and the value of data point 4 as the value of data point 3:

time:     1 2 3 4
signal:   1 1 0 0

The block uses the value of the last known data point as the value of time steps that occur after the last known data point.

Combine the settings of the Interpolate data and Form output after final data value by parameters to determine the block output after the last time step for which workspace data is available. For details, see the Form output after final data value by parameter.

Enable zero-crossing detection

If you select the Enable zero-crossing detection parameter, then when the input array contains more than one entry for the same time step, Simulink detects a zero crossing at that time step. For example, suppose the input array has this data:

time:     0 1 2 2 3
signal:   2 3 4 5 6

At time 2, there is a zero crossing from input signal discontinuity. For more information, see Zero-Crossing Detection.

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

Form output after final data value by

Combine the settings of the Form output after final data value by and Interpolate data parameters to determine the block output after the last time step for which workspace data is available. The following table describes the block output based on the values of the two options:

Setting for Form Output After Final Data Value BySetting for Interpolate DataBlock Output After Final Data

Extrapolation

On

Extrapolated from final data value

Off

Error

Setting to zero

On

Zero

Off

Zero

Holding final value

On

Final value from workspace

Off

Final value from workspace

Cyclic repetition

On

Error

Off

Repeated from workspace if the workspace data is in structure-without-time format. Error otherwise.

For example, if Form output after final data value by is Extrapolation and Interpolate data is selected, the block uses the last two known data points to extrapolate data points that occur after the last known point. Consider the following model.

In this model, the From Workspace block reads data from the workspace consisting of the output of the Simulink Sine block sampled at one-second intervals. The workspace contains the first 16 samples of the output. The top and bottom X-Y plots display the output of the Sine Wave and From Workspace blocks, respectively, from 0 to 20 seconds. The straight line in the output of the From Workspace block reflects the block's linear extrapolation of missing data points at the end of the simulation.

Examples

The From Workspace block allows you to read 1-D and 2-D signals into Simulink.

Reading 1-D Signals in Array Format

Create two signals x and y with a time vector t, and then import the values into Simulink with an array.

  1. In the MATLAB Command Window, enter

    t = [0:0.2:10]';
    x = sin(t);
    y = 10*sin(t);

    The time vector must be a column vector.

  2. Add a From Workspace block to your model.

  3. Double-click the block to open the block parameters dialog. In the Data field, enter the array [t,x,y],

Reading 1-D Signals in Structure Format

Create two signals x and y with a time vector t, and then import the values into Simulink with a structure.

  1. In the MATLAB Command Window, enter

    t = [0:0.2:10]';
    x = sin(t);
    y = 10*sin(t);
    wave.time = t;
    wave.signals.values = [x,y];
    wave.signals.dimensions =2;

    The time vector must be a column vector. The signals.dimensions field for a 1-D signal is a scalar corresponding to the number of signals or number of columns in the signals.values field. Each column in the signals.values field corresponds to a signal

  2. Add a From Workspace block to your model.

  3. Double-click the block to open the block parameters dialog. In the Data field, enter the structure name.

Using Sample Time from Model

If you do not have a time vector, you can define the sample time in your model.

  1. In the MATLAB Command Window, enter

    wave.time = [];
  2. Double-click the From Workspace block to open the block parameters dialog. In the Sample time field, enter a time interval. For example, enter 0.2. Clear the Interpolate data check box. From the Form output after final data value by, select either Setting to zero, Holding final value, or Cyclic repetition. Do not select Extrapolation.

Reading 2-D Signals in Structure Format

To load 2-D signals from the MATLAB workspace into Simulink, you must have the signals in a structure format. This example creates a 10–by–10 matrix (2-D signal) using the magic function, and then creates a 3-D matrix by adding a time vector.

  1. In the MATLAB Command Window, enter

    t1 = [0:0.2:10]';
    m = magic(10);
    M = repmat(m,[1 1 length(t1)]);
    data.time=t1;
    data.signals.values = M;
    data.signals.dimensions=[10 10];

    The time vector must be a column vector. The signals.values field is a 3-D matrix where the third dimension is the signal values at a specific time. The signals.dimensions field is a two element vector where the first element is the number of rows and the second element is the number of columns in the signals.values field.

  2. Double-click the From Workspace block to open the block parameters dialog. In the Data field, enter the name of the structure.

Characteristics

Sample Time

Specified in the Sample time parameter

Scalar Expansion

No

Dimensionalized

Yes

Multidimensionalized

Yes

Zero-Crossing Detection

Yes, if enabled.

Was this topic helpful?