0
0
SimulinkHow-ToBeginner · 3 min read

How to Export Data from Simulink to Workspace Easily

To export data from Simulink to the workspace, use the To Workspace block in your model. Configure the block by setting the variable name and data format, then run the simulation to save the data in the MATLAB workspace.
📐

Syntax

The main way to export data from Simulink to the workspace is by using the To Workspace block. You place this block in your model and connect it to the signal you want to export.

  • Variable name: The name of the variable in the workspace where data will be saved.
  • Save format: Choose how data is saved, e.g., Array, Structure, or Structure with time.
  • Sample time: Defines how often data is logged.
simulink
To Workspace block settings:
- Variable name: 'simData'
- Save format: 'Array'
- Sample time: -1 (inherited)
💻

Example

This example shows how to export a sine wave signal from Simulink to the MATLAB workspace using the To Workspace block.

matlab
% Create a simple Simulink model programmatically
model = 'exportExample';
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');

% Run simulation
sim(model);

% Display first 5 rows of exported data
sineData(1:5,:)

% Close model without saving
close_system(model, 0);
Output
[0 0 0.0628 0.0628 0.1257 0.1253 0.1885 0.1874 0.2513 0.2487]
⚠️

Common Pitfalls

  • Not setting the Variable name in the To Workspace block causes no data to appear in the workspace.
  • Choosing the wrong Save format can make data hard to use; Array is simplest for beginners.
  • Forgetting to connect the signal line to the To Workspace block means no data is exported.
  • Running the simulation without the block properly configured results in empty or missing data.
matlab
%% Wrong way: No variable name set
set_param([model '/To Workspace'], 'VariableName', '');
sim(model);
% No data will be saved in workspace

%% Right way: Set variable name
set_param([model '/To Workspace'], 'VariableName', 'sineData');
sim(model);
% Data saved in 'sineData'
📊

Quick Reference

  • Use To Workspace block to export signals.
  • Set Variable name to name your data in workspace.
  • Choose Save format based on how you want data structured.
  • Connect the block to the signal you want to export.
  • Run simulation to save data.

Key Takeaways

Use the To Workspace block to export simulation data to MATLAB workspace.
Always set a clear variable name in the To Workspace block settings.
Choose the appropriate save format for easy data use after simulation.
Connect the To Workspace block properly to the signal you want to export.
Run the simulation after configuring the block to save data successfully.