You are on page 1of 7

1902 - How do I load data from the MATLAB workspace to Simulink using the From Workspace Block?

The From Workspace block in Simulink allows you to read 1-D and 2-D signals into Simulink. The following examples illustrate the different ways you can use the From Workspace block to read signals from the MATLAB workspace.

Reading 1-D Signals into Simulink


Create two sine signals, x and y, in MATLAB:
t=[0:0.2:10]'; % t is the time vector for the two sine waves x=sin(t); y=10*sin(t);

There are two things to keep in mind:


The time vector, t, must be a column vector. You have the option to use either the array or structure format to read 1-D signals into Simulink. Both options are explained below.

Reading 1-D Signals in Array Format


To import the two sine waves that you created above in array format, you need to set the Data parameter of the From Workspace block to [t x y] as shown below (Figure 1).

Figure 1 To import the two sine waves that you created above in a structure format, you have to create a structure that contains three fields:
time signals.values signals.dimensions

Assuming that the name of the structure you want to create is wave, you can define the time field for your signal by typing the following at the MATLAB prompt.
wave.time = t;

The signals.values field is where your data will be stored. It can be defined in this example by typing
wave.signals.values = [x y];

Note: Each column in the signals.values field corresponds to a signal. The signals.dimensions field for a 1-D signal is a scalar corresponding to the number of signals (or number of columns in the signals.values field) that you want to import into Simulink. It can be defined in this example by typing:
wave.signals.dimensions = 2;

To import the wave variable into Simulink, you can type wave in the Data parameter of the From Workspace block as shown below (Figure 2).

Figure 2 Alternatively, if you do not have a time vector or if you want to use the simulation time as the time for your signal data, you can define the time field as follows.
wave.time = [];

If you choose the above option, you have to specify a sampling time for the block. You also have to uncheck the Interpolate Data check box and choose one of the following options for the "Form output after final data value by" parameter (Figure 3).
SettingToZero HoldingFinalValue Cyclic Repetition

This is how the From Workspace dialog box should look if the sampling time for the signal is set to 0.2 and you chose the SettingToZero option for the "Form output after final data value by" parameter (Figure 3).

Figure 3 Note: Do not choose the "Extrapolation" option.

Reading 2-D Signals into Simulink


In order to load 2-D signals from the MATLAB workspace into Simulink, you must have the signal in a structure format. The structure will require the following fields.
time signals.values signals.dimensions

Note: For more information about structures, see the Structures and Cell Arrays section in the Using MATLABUser's Guide. The structure format is similar to the one for 1-D signals:

The time field has the same format. The signals.values field is a 3-D matrix where the third dimension recodesents the signal at a specific time instance (Figure 4). The signals.dimensions field is a two element vector where the first element corresponds to the number of rows and the second element corresponds to the number of columns of your 2-D signal.

Figure 4 Here's an example that demonstrates how to read a 2-D signal from the MATLAB workspace into Simulink:
t1 = [0:0.2:10]'; m=magic(10); %creates a 10-by-10 matrix M=repmat(m,[1 1 length(t1)]); % creates a 3-D matrix from m and time vector data.time=t1; data.signals.values=M; data.signals.dimensions=[10 10];

To load the data variable into Simulink, you can type data in the Data parameter of the From Workspace block as shown below:

Figure 5 Alternatively, if you want to use the simulation time as your time vector, you can define your structure as follows.
m=magic(10); data.time=[]; data.signals.values=m; data.signals.dimensions=[10 10];

Again, if you choose the above option, you have to specify a sampling time for the block(Figure 6). You also have to uncheck the Interpolate Data check box and choose one of the following options for the "Form output after final data value by" parameter.
SettingToZero HoldingFinalValue Cyclic Repetition

This is how the From Workspace dialog box should look if the sampling time for the signal is set to 0.2 and you chose the SettingToZero option for the "Form output after final data value by" parameter (Figure 6).

Figure 6 Note: Do not choose "Extrapolation".

You might also like