How to Use From Workspace Block in Simulink: Simple Guide
Use the
From Workspace block in Simulink to import variables from the MATLAB workspace into your model. Set the block's Data parameter to the variable name or expression containing your data, and Simulink will read this data during simulation.Syntax
The From Workspace block requires you to specify the Data parameter, which is the name of the variable or expression in the MATLAB workspace that holds your input data. The data can be a vector, matrix, or timeseries object.
Key parts:
Data: Variable name or expression in MATLAB workspace.Sample time: Defines how often data is read during simulation.Form output after final data value by: Defines behavior after data ends (hold last value, zero, or error).
text
From Workspace block parameters:
Data: mySignal
Sample time: 0.1
Form output after final data value by: Holding final valueExample
This example shows how to use the From Workspace block to input a sine wave signal into a Simulink model.
matlab
% Define time vector and sine wave data in MATLAB workspace simTime = 0:0.1:10; signal = sin(2*pi*0.5*simTime)'; % Combine time and data into a two-column matrix inputData = [simTime' signal]; % Open a new Simulink model new_system('exampleModel'); open_system('exampleModel'); % Add From Workspace block add_block('simulink/Sources/From Workspace','exampleModel/From Workspace'); % Set the Data parameter to 'inputData' set_param('exampleModel/From Workspace','VariableName','inputData'); % Add Scope block to visualize output add_block('simulink/Sinks/Scope','exampleModel/Scope'); % Connect From Workspace to Scope add_line('exampleModel','From Workspace/1','Scope/1'); % Set simulation stop time set_param('exampleModel','StopTime','10'); % Run the simulation sim('exampleModel');
Output
A Simulink model named 'exampleModel' runs simulation for 10 seconds, feeding a sine wave from workspace to the Scope block, which displays the sine wave signal.
Common Pitfalls
- Incorrect data format: The data must be a matrix with time in the first column and signal values in the second (or more) columns, or a timeseries object. Using just a vector without time can cause errors.
- Variable not in workspace: The variable name in the block must exactly match a variable in the MATLAB workspace before simulation starts.
- Sample time mismatch: If sample time is set incorrectly, the signal may not update as expected during simulation.
- Data ends before simulation: Decide how the block behaves after data ends (hold last value, zero, or error) to avoid unexpected results.
matlab
%% Wrong way: data as vector without time wrongData = sin(0:0.1:10); % No time column %% Right way: data with time column correctData = [(0:0.1:10)' sin(2*pi*0.5*(0:0.1:10))'];
Quick Reference
Tips for using From Workspace block:
- Always prepare data as
[time, values]matrix or timeseries object. - Ensure variable is in MATLAB workspace before simulation.
- Set sample time to match your data sampling rate.
- Choose output behavior after data ends carefully.
Key Takeaways
The From Workspace block imports data from MATLAB workspace into Simulink models.
Data must be formatted as a time-value matrix or timeseries object.
Ensure the variable exists in workspace before running simulation.
Set sample time and end-of-data behavior to match your simulation needs.
Common errors come from missing time column or variable name mismatches.