Documentation Center

  • Trial Software
  • Product Updates

To Workspace

Write data to MATLAB workspace

Library

Sinks

Description

The To Workspace block inputs a signal and writes the signal data to the MATLAB® workspace. During the simulation, the block writes data to an internal buffer. When the simulation is completed or paused, that data is written to the workspace. The block icon shows the name of the array to which the data is written.

To specify the name of the workspace variable to which the To Workspace block writes the data, use the Variable name parameter. To specify the data format of the variable, use the Save format parameter. You can specify to save the data in one of the following formats:

  • A MATLAB timeseries object (or structure of MATLAB timeseries objects for bus data)

  • An array

  • Structure

  • Structure with time

Saving Data for Use by a From Workspace Block

To use a From Workspace block to play back the sample-based data that was saved by a To Workspace block in a previous simulation, in the To Workspace block, use the Timeseries or Structure with time format.

Controlling the Amount of Data Saved

For variable-step solvers, to control the amount of data available to the To Workspace block, use the Model Configuration Parameters > Data Import/Export > Output options parameter. For example, to have Simulink® write data at identical time points over multiple simulations, select the Produce specified output only option.

Then use To Workspace block parameters to control when and how much of this data the block writes:

  • Use the Limit data points to last parameter to specify how many sample points to save. If the simulation generates more data points than the specified maximum, the simulation saves only the most recently generated samples. To capture all the data, set this value to inf.

  • Use the Decimation parameter to have the To Workspace block write data at every nth sample, where n is the decimation factor. The default decimation, 1, writes data at every time step.

  • Use the Sample time parameter to specify a sampling interval at which to collect points. This parameter is useful when you are using a variable-step solver where the interval between time steps might not be the same. The default value of -1 causes the block to inherit the sample time from the driving block when determining the points to write. See Specify Sample Time in the online documentation for more information.

For example, suppose you have a simulation where the start time is 0, the Limit data points to last is 100, the Decimation is 1, and the Sample time is 0.5. The To Workspace block collects a maximum of 100 points, at time values of 0, 0.5, 1.0, 1.5, ..., seconds. Specifying a Decimation value of 1 directs the block to write data at each step.

In a similar example, the Limit data points to last is 100 and the Sample time is 0.5, but the Decimation is 5. In this example, the block collects up to 100 points, at time values of 0, 2.5, 5.0, 7.5, ..., seconds. Specifying a Decimation value of 5 directs the block to write data at every fifth sample. The sample time ensures that data is written at these points.

In another example, all parameters are as defined in the first example except that the Limit data points to last is 3. In this case, only the last three sample points collected are written to the workspace. If the simulation stop time is 100, data corresponds to times 99.0, 99.5, and 100.0 seconds (three points).

MAT-File Logging

If you enable the Model Configuration Parameters > Code Generation > Interface > MAT-file logging parameter, then To Workspace logs its data to a MAT-file.

Frame-Based Signals

By default, the To Workspace block treats input signals as sample-based.

To have the To Workspace block treat input signals as frame-based, set:

  1. Save format to either Array or Structure

  2. Save 2-D signals as to 2-D array (concatenating along first dimension) or 3-D array (concatenating along third dimension)

    For examples of how these two settings store frame-based data, in the DSP System Toolbox™ documentation, see the Signal To Workspace block reference page. (These settings produce the same kind of results in the To Workspace block as they do in the Signal to Workspace block.)

Data Type Support

The To Workspace block can save to the MATLAB workspace real or complex inputs of any data type that Simulink supports, including fixed-point and enumerated data types, as well as bus objects.

For more information, see Data Types Supported by Simulink.

Parameters and Dialog Box

Variable name

Specify the name of the variable to which the software saves the data.

Limit data points to last

Specify the maximum number of input samples to save. The default is inf.

Decimation

Specify the decimation factor. The default is 1.

Save format

Specify one of these formats for saving simulation output to the workspace:

  • Timeseries (Default)

    Save non-bus signal input as a MATLAB timeseries object and bus signal input as a structure of MATLAB timeseries objects.

  • Array

    Save the input as an N-dimensional array where N is one more than the number of dimensions of the input signal. For example, if the input signal is a vector, the resulting workspace array is two-dimensional. If the input signal is a matrix, then the array is three-dimensional.

    How Simulink stores samples in the array depends on whether the input signal is a scalar or vector or a matrix. If the input is a scalar or a vector, each input sample is output as a row of the array. For example, suppose that the name of the output array is simout. Then, simout(1,:) corresponds to the first sample, simout(2,:) corresponds to the second sample, and so on. If the input signal is a matrix, the third dimension of the workspace array corresponds to the values of the input signal at specified sampling point. For example, suppose again that simout is the name of the resulting workspace array. Then, simout(:,:,1) is the value of the input signal at the first sample point; simout(:,:,2) is the value of the input signal at the second sample point; and so on.

    If you select Array, the Save 2-D signals as parameter appears.

    To treat input signals as frame-based, set Save format to either Array or Structure.

  • Structure

    This format consists of a structure with three fields:

    • time — This field is empty for this format.

    • signals — A structure with three fields: values, dimensions, and label. The values field contains the array of signal values. The dimensions field specifies the dimensions of the values array. The label field contains the label of the input line.

    • blockName — Name of the To Workspace block

    If you select Structure, the Save 2-D signals as parameter appears.

    To treat input signals as frame-based, set Save format to either Structure or Array.

  • Structure With Time

    This format is the same as Structure, except that the time field contains a vector of simulation time steps.

    To read To Workspace block output directly with a From Workspace block, use either the Timeseries or Structure with Time format. For details, see Techniques for Importing Signal Data.

    Structure with Time format does not support frame-based signals. Use Array or Structure format instead.

The following table shows how simulation mode support depends on the Save format value.

Simulation ModeTimeseriesArray, Structure, or Structure With Time
NormalSupported.Supported.
AcceleratorSupported.Supported only in top model, not referenced models.
Rapid AcceleratorNot supported.Supported only in top model, not referenced models.
Software-in-the-Loop (SIL) Not supported.If MAT-file logging is enabled, supported only in top model, not referenced models.
Processor-in-the-Loop (PIL)Not supported.If MAT-file logging is available and enabled, supported only in top model, not referenced models.
ExternalNot supported.Supported only in top model, not referenced models.
Simulink Coder™ TargetsNot supported.If MAT-file logging is enabled, supported only in top model, not referenced models.

Save 2-D signal as

If you set Save format to Array or Structure, the Save 2-D signals as as parameter appears.

Specify one of these formats for saving 2-D signals to the workspace:

  • 3-D array (concatenate along third dimension) (Default)

    This setting is well-suited for sample-based signals. Data is concatenated along the third dimension. For example, 2-by-4 matrix input for 10 samples is stored as a 2x4x10 array.

  • 2-D array (concatenate along first dimension)

    This setting is well-suited for frame-based signals. The data is concatenated along the first dimension. For example, 2-by-4 matrix input for 10 samples is stored as a 20x4 array

  • Inherit from input (this choice will be removed — see release notes)

    This setting is for backward compatibility. To configure this block to treat input signals as frame-based in future releases, set this to 2-D array (concatenate along first dimension) or 3-D array (concatenate along third dimension).

Log fixed-point data as a fi object

By default, the To Workspace block logs fixed-point data to the MATLAB workspace as a Fixed-Point Designer™ fi object. If you clear this parameter, fixed-point data is logged to the workspace as double.

Sample time

Specify the sample period and offset at which to collect data. This parameter is useful when you are using a variable-step solver where the interval between time steps might not be constant. The default is-1, which inherits the sample time from the driving block. See Specify Sample Time for more information.

Examples

The aero_vibratiaero_vibrati example shows how to use the To Workspace block.

In the DSP System Toolbox documentation, the Signal To Workspace block reference page includes examples of handing frame-based data. (The To Workspace block handles frame-based data in a similar way as the Signal To Workspace block does.)

Characteristics

Sample Time

Specified in the Sample time parameter

Dimensionalized

Yes

Multidimensionalized

Yes

Was this topic helpful?