To Workspace

Write data to workspace

  • Library:
  • Simulink / Sinks

    HDL Coder / Sinks

  • To Workspace block

Description

The To Workspace block writes input signal data to a workspace. During simulation, the block writes data to an internal buffer. When you pause the simulation or the simulation completes, that data is written to the workspace. Data is not available until the simulation pauses or stops.

The To Workspace block typically writes data to the MATLAB® base workspace. For a sim command in a MATLAB function, the To Workspace block sends data to the workspace of the calling function, not to the MATLAB base workspace. To send the logged data to the base workspace, use an assignin command in the function.

function myfunc
    a = sim('mTest','SimulationMode','normal');
    b = a.get('simout')
    assignin('base','b',b);
end

The To Workspace block icon shows the name of the variable to which the data is written.

Control Amount of Data Saved

If you specify data logging intervals with the Configuration Parameters > Data Import/Export > Logging intervals parameter, the To Workspace block does not log data outside of the intervals. For example, the block logs no data if the intervals are empty ([]). The block stores the logged data in the variable that you specify for the Single simulation output parameter.

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

Block parameters also control the amount of data saved. See Limit data points to last, Decimation, and Sample time.

Log to MAT-File

When you enable the MAT-file logging parameter in Configuration Parameters, the To Workspace block logs its data to a MAT-file. For information about this parameter, see MAT-file logging (Simulink Coder).

Ports

Input

expand all

Workspace data created from input signal. The To Workspace block can save real or complex inputs of any data type that Simulink® supports, including fixed-point and enumerated data types, and bus objects.

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 (concatenate along first dimension)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus

Parameters

expand all

Specify the name of the variable for the saved data.

Programmatic Use

Block Parameter: VariableName
Type: character vector
Values: character vector
Default: 'simout'

Specify the maximum number of input samples to save. If the simulation generates more data points than the specified maximum, the simulation saves only the most recently generated samples. The default value causes the block to write all data.

Programmatic Use

Block Parameter: MaxDataPoints
Type: character vector
Values: scalar | vector
Default: 'inf'

Specify the decimation factor, n, which writes data at every nth time that the block executes. The default value causes the block to write data at every time step.

Programmatic Use

Block Parameter: Decimation
Type: character vector
Values: scalar | vector
Default: '1'

Specify the format for saving simulation output to the workspace.

The default Timeseries format saves nonbus signals as a MATLAB timeseries object and bus signals as a structure of MATLAB timeseries objects.

The Array format saves 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, vector, or matrix.

  • If the input signal is a scalar or a vector, each input sample is output as a row of the array. 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, time corresponds to the third dimension. Suppose again that simout is the name of the resulting workspace array. Then, simout(:,:,1) is the input signal value at the first sample point, simout(:,:,2) is the input signal value at the second sample point, and so on.

The Structure format consists of a structure with three fields:

  • time — Empty field for this format.

  • signals — 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 corresponding signals. The label field contains the label of the input line.

  • blockName — Name of the To Workspace block.

The Structure With Time format is the same as Structure, except that the time field contains a vector of simulation time hits.

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

To read the To Workspace block output directly with a From Workspace block, use either the Timeseries or Structure with Time format. The From Workspace block can read sample-based data from a To Workspace block that was saved in a previous simulation. For details, see Comparison of Signal Loading Techniques.

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

Simulation ModeTimeseriesArray, Structure, or Structure With Time
NormalSupportedSupported
AcceleratorSupportedSupported only in top model, not referenced models
Rapid AcceleratorNot supportedSupported only in top model, not referenced models
Software-in-the-loop (SIL) Not supportedIf MAT-file logging is enabled, supported only in top model, not referenced models
Processor-in-the-loop (PIL)Not supportedIf MAT-file logging is available and enabled, supported only in top model, not referenced models
ExternalNot supportedSupported only in top model, not referenced models
Simulink Coder™ TargetsNot supportedIf MAT-file logging is enabled, supported only in top model, not referenced models

Programmatic Use

Block Parameter: SaveFormat
Type: character vector
Values: 'Timeseries' | 'Structure with Time' | 'Structure' | 'Array'
Default: 'Timeseries'

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 2-by-4-by-10 array.

  • 2-D array (concatenate along first dimension)

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

When the Save format is set to Array or Structure, the dimensions of the output depend on the input dimensions and the setting of the Save 2-D signals as parameter. The following table summarizes the output dimensions under various conditions. In the table, K represents the value of the Limit data points to last parameter.

Input Signal DimensionsSave 2-D Signals As ...Signal to Workspace Output Dimension

M-by-N matrix

2-D array (concatenate along first dimension)

K-by-N matrix.

If you set the Limit data points to last parameter to inf, K represents the total number of samples acquired in each column by the end of simulation. This setting is equivalent to multiplying the input frame size (M) by the total number of M-by-N inputs acquired by the block.

M-by-N matrix

3-D array (concatenate along third dimension)

M-by-N-by-K array.

If you set the Limit data points to last parameter to inf, K represents the total number of M-by-N inputs acquired by the end of the simulation.

Length-N unoriented vector

Any setting

K-by-N matrix

N-dimensional array where N > 2

Any setting

Array with N + 1 dimensions, where the size of the last dimension is equal to K. If you set the Limit data points to last parameter to inf, K represents the total number of M-by-N inputs acquired by the end of simulation

Dependencies

To enable the Save 2-D signals as parameter, set the Save format to Array or Structure.

Programmatic Use

Block Parameter: Save2DSignal
Type: character vector
Values: '2-D array (concatenate along first dimension)' | '3-D array (concatenate along third dimension)'
Default: '3-D array (concatenate along third dimension)'

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, Simulink software logs fixed-point data to the workspace as double.

Programmatic Use

Block Parameter: FixptAsFi
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Specifies the sample period and offset at which to collect data points. This parameter is useful when you are using a variable-step solver where the interval between time steps is not constant. The default value causes the block to inherit the sample time from the driving block. See Specify Sample Time.

Programmatic Use

Block Parameter: SampleTime
Type: character vector
Values: scalar | vector
Default: '-1'

Block Characteristics

Data Types

Boolean | bus | double | enumerated | fixed point | integer | single

Direct Feedthrough

no

Multidimensional Signals

yes

Variable-Size Signals

yes

Zero-Crossing Detection

no

Tips

To make post-processing easier, you can convert data saved by this block to Dataset format. This conversion is useful when post processing this data with other logged data that can use Dataset format (for example, logged states). See Convert Logged Data to Dataset Format. You can also use signal logging with a variable-size signal exception.

Extended Capabilities

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Introduced before R2006a