0
0
SimulinkHow-ToBeginner · 4 min read

How to Import Data from MATLAB Workspace to Simulink

To import data from the MATLAB workspace into Simulink, use the From Workspace block. Prepare your data as a time-series or matrix variable in MATLAB, then set the block's data parameter to that variable name to feed data into your Simulink model.
📐

Syntax

The main method to import data from MATLAB workspace to Simulink is using the From Workspace block.

  • From Workspace block: Reads data from a MATLAB variable.
  • Data variable: A MATLAB variable containing your data, usually a matrix or timeseries object.
  • Time vector: The first column or separate vector representing time points.

Example syntax in Simulink block parameter:

Variable name: myData
matlab
myData = [0 1; 1 3; 2 5; 3 7]; % First column is time, second is data
% In Simulink, set From Workspace block Data parameter to 'myData'
💻

Example

This example shows how to import a simple time-series data from MATLAB workspace into Simulink using the From Workspace block.

matlab
% Define time and data in MATLAB workspace
time = (0:0.1:2)';
data = sin(2*pi*time);
myData = [time data];

% In Simulink:
% 1. Add a 'From Workspace' block.
% 2. Set the Data parameter to 'myData'.
% 3. Connect the block to a Scope to visualize the signal.

% Run the Simulink model to see the imported data as a signal.
⚠️

Common Pitfalls

  • Incorrect data format: The data must be a matrix with time in the first column and signal values in the next columns, or a timeseries object.
  • Variable not in workspace: The variable name must exactly match and be present in the MATLAB base workspace before running Simulink.
  • Time vector issues: Time values must be strictly increasing and match simulation time.
  • Data parameter syntax: The From Workspace block's Data parameter must be a string with the variable name, not the variable itself.
matlab
% Wrong way:
% From Workspace Data parameter: myData (without quotes) - causes error

% Right way:
% From Workspace Data parameter: 'myData' (with quotes)
📊

Quick Reference

StepActionNotes
1Create data variable in MATLAB workspaceMatrix with time in first column, data in others
2Add 'From Workspace' block in SimulinkFound under Sources library
3Set Data parameter to variable name as stringE.g., 'myData'
4Connect block to model signalsTo use imported data
5Run simulationData feeds into Simulink model

Key Takeaways

Use the From Workspace block to import MATLAB workspace data into Simulink.
Format your data as a matrix with time in the first column and signal values next.
Always set the From Workspace block Data parameter as a string with the variable name.
Ensure the variable exists in the MATLAB base workspace before simulation.
Time values must be strictly increasing and match the simulation time span.