0
0
SimulinkHow-ToBeginner · 4 min read

How to Use To Workspace Block in Simulink: Simple Guide

In Simulink, use the To Workspace block to save simulation data to the MATLAB workspace. Connect the block to the signal you want to save, set the variable name, and run the simulation to export data for further analysis.
📐

Syntax

The To Workspace block syntax involves setting these key parameters:

  • Variable name: The name of the variable in MATLAB workspace where data is saved.
  • Save format: Choose between Array, Structure, or Structure with time formats.
  • Decimation: Controls how often data points are saved (e.g., every sample or every nth sample).

Connect the block's input port to the signal you want to log.

text
To Workspace block parameters:
- Variable name: 'simData'
- Save format: 'Array'
- Decimation: 1

Connect signal --> To Workspace block
💻

Example

This example shows how to save a sine wave signal to the workspace using the To Workspace block.

matlab
% Create a simple Simulink model programmatically
model = 'sine_to_workspace';
new_system(model);
open_system(model);

% Add Sine Wave block
add_block('simulink/Sources/Sine Wave',[model '/Sine Wave']);

% Add To Workspace block
add_block('simulink/Sinks/To Workspace',[model '/To Workspace']);

% Set To Workspace block parameters
set_param([model '/To Workspace'], 'VariableName', 'sineData', 'SaveFormat', 'Array');

% Connect blocks
add_line(model, 'Sine Wave/1', 'To Workspace/1');

% Set simulation stop time
set_param(model, 'StopTime', '10');

% Run simulation
sim(model);

% Display first 5 rows of saved data
sineData(1:5,:)
Output
[ 0 0 0.01 0.0628 0.02 0.1253 0.03 0.1874 0.04 0.2487 ]
⚠️

Common Pitfalls

  • Incorrect variable name: Using spaces or special characters can cause errors.
  • Wrong save format: Choosing Array when you need time stamps requires manual time handling.
  • Decimation too high: Setting decimation >1 may skip important data points.
  • Block not connected: If the To Workspace block is not connected to a signal, no data is saved.
matlab
% Wrong way: Variable name with space
set_param([model '/To Workspace'], 'VariableName', 'my data'); % Causes error

% Right way: Use underscore
set_param([model '/To Workspace'], 'VariableName', 'my_data');
📊

Quick Reference

ParameterDescriptionTypical Values
Variable nameName of variable in MATLAB workspacee.g., 'simData'
Save formatFormat of saved data'Array', 'Structure', 'Structure with time'
DecimationData saving frequency1 (every sample), 10 (every 10 samples)
Limit data pointsMax number of points savedDefault is Inf (no limit)

Key Takeaways

Connect the To Workspace block to the signal you want to save before running simulation.
Set a valid variable name without spaces or special characters to avoid errors.
Choose the save format based on whether you need time stamps or just data arrays.
Use decimation carefully to balance data size and detail.
After simulation, access saved data directly in MATLAB workspace for analysis.