To read data that you have already defined in the MATLAB® workspace into a Simulink® model, you primarily use the From Workspace block.
Using the From Workspace Block in Simulink
The From Workspace block is a source block in Simulink that allows your model to access variables stored in the base MATLAB workspace or a model workspace. This is essential when you want to use data generated or loaded in MATLAB within your simulation environment.
Adding the Block
- Open your Simulink model.
- Open the Simulink Library Browser.
- Navigate to the Sources library.
- Drag the From Workspace block into your model.
Configuring the Data Source
Once you have added the block, you need to tell it which data to load from the workspace. This is done through the block's parameters, specifically the Data parameter.
Specify data for the From Workspace block to load using the Data parameter. You can specify the value of the Data parameter as a MATLAB® expression, such as a variable name. The expression in the Data parameter must evaluate to data in a format that the From Workspace block supports.
Here's how to configure the Data
parameter:
- Double-click the From Workspace block to open its parameter dialog box.
- In the Data field, enter the name of the variable from your MATLAB workspace that you want to use. For example, if you have a variable named
mySignalData
, you would typemySignalData
. - You can also use a simple MATLAB expression that evaluates to the desired data, though a simple variable name is most common.
- Ensure the variable exists in the MATLAB workspace when you run the simulation.
Supported Data Formats
The From Workspace block supports various data formats. The expression you provide in the Data
parameter must evaluate to one of these supported types. Common supported formats include:
- MATLAB arrays (numeric, complex, logical)
timeseries
objects (recommended for time-dependent data)- Structures with time and signal data fields
Using a timeseries
object is generally recommended as it explicitly includes time information, which is often crucial for simulations.
Example: Reading a Timeseries Variable
Let's walk through a quick example:
-
Create Data in MATLAB: In your MATLAB Command Window or a script, create a
timeseries
object:t = 0:0.1:10; % Time vector data = sin(t); % Data vector myTimeSeries = timeseries(data, t); % Create timeseries object
Now,
myTimeSeries
is a variable in your workspace. -
Add From Workspace Block: In your Simulink model, add a From Workspace block.
-
Configure the Block:
- Double-click the From Workspace block.
- In the Data parameter field, type
myTimeSeries
. - Click OK.
-
Connect and Run: Connect the output of the From Workspace block to a block like a Scope to visualize the data. When you run the Simulink model, the From Workspace block will read the
myTimeSeries
data from the workspace and feed it into the simulation.
Using the From Workspace block is a straightforward way to bring data created or loaded in the MATLAB environment into your Simulink models for simulation and analysis.