You are on page 1of 8

To Workspace

Write data to the workspace.

Library
Sinks

Description
The To Workspace block writes its input to the workspace. The block writes its
output to a matrix or structure that has the name specified by the block's Variable
name parameter. The Save formatparameter determines the output format:
Matrix
The matrix has this form:

The amount of data written and the time steps at which the data is written are
determined by block parameters:
The Maximum number of rows parameter indicates how many data rows
to save. If the simulation generates more rows than the specified
maximum, the simulation saves only the most recently generated rows. To
capture all the data, set this value to inf.
The Decimation parameter allows you to write data at every nth sample,
where n is the decimation factor. The default decimation, 1, writes data at
every time step.
The Sample time parameter allows you to specify a sampling interval at
which to collect points. This parameter is useful when using a variablestep solver where the interval between time steps may not be the same.
The default value of -1 causes the block to inherit the sample time from
the driving block when determining which points to write.

During the simulation, the block writes data to an internal buffer. When the
simulation is completed or paused, that data is written to the workspace. The
block icon shows the name of the matrix to which the data is written.
Structure
This format consists of a structure with three fields: time, signals, and
blockName. The time field is empty. The blockName field contains the name of
the To Workspace block. The signals field contains a structure with two fields:
values and label. The values field contains the matrix of signal values.
Structure with Time
This format is the same as Structure except that the time field contains a vector of
simulation time steps.
Using Saved Data with a From File Block
If the data written using a To Workspace block is to be saved and read later by a
From File block, the time must be added to the data and the matrix must be
transposed. For more information, see From File.
Using Saved Data with a From Workspace Block
If the data written using a To Workspace block is intended to be "played back" in
another simulation using a From Workspace block, the data must contain the
simulation time values. The way you include times depends on the save format.
If the save format is a structure, you can include the simulation time by
choosing Structure with Time as the value of Save format. The block stores the
simulation times as a vector in the time member of the output structure.
If the save format is a matrix, you must add a column of simulation times to the
matrix. You can add a column with time values in two ways:
By multiplexing the output of a Clock block as the first element of the
vector input line of the To Workspace block.

By specifying time as a return value on the Simulation Parameters dialog


box or from the command line, described in Chapter 4. When the

simulation is completed, you can concatenate the time vector ( t) to the


matrix using a command like this:

matrix = [t; matrix];

Examples
In a simulation where the start time is 0, the Maximum number of rows is 100,
the Decimation is 1, and the Sample time is 0.5. The To Workspace block
collects a maximum of 100 points, at time values of 0, 0.5, 1.0, 1.5, & seconds.
Specifying a Decimation of 1 directs the block to write data at each step.
In a similar example, the Maximum number of rows is 100 and the Sample
time is 0.5, but the Decimation is 5. In this example, the block collects up to 100
points, at time values of 0, 2.5, 5.0, 7.5, & seconds. Specifying a Decimation of
5 directs the block to write data at every fifth sample. The sample time ensures
that data is written at these points.
In another example, all parameters are as defined in the first example except that
the Maximum number of rows is 3. In this case, only the last three rows
collected are written to the workspace. If the simulation stop time is 100, data
corresponds to times 99.0, 99.5, and 100.0 seconds (three points).

Data Type Support


A To Workspace block can save input of any real or complex data type to the
MATLAB workspace.

Parameters and Dialog Box

Variable name
The name of the matrix that holds the data.
Maximum number of rows
The maximum number of rows (one row per time step) to be saved. The
default is 1000 rows.
Decimation
A decimation factor. The default is 1.
Sample time
The sample time at which to collect points.
Save format
Format in which to save simulation output to the workspace. The default is
as a structure.

Characteristics
Sample Time

Inherited

Vectorized

Yes

I want to retrieve the data from simulink during simulation, and use serial network function to send
these data to another program. Because I need to use another program to do some tricks and send
command back to simulink, so I have to get data from simulink during runtime so that another

program can make the right command.


I've tried using To Workspace block to export the data.

However, I can only got value in the very beginning of the simulation.
And I've also tried using scope and change some properties: check Save Data To Workspace and
Uncheck Limite data to Last.

First, I started simulation, and I found the ScopeData didn't appear in the Workspace. Only when I
stop simulation, ScopeData would appear in workspace.

And after that, I can use ScopeData.signals.values to get values.


But what I want is: when I start simulation, ScopeData would appear in workspace so that I can send
these data to other program.
Does anyone know how to achieve this?
I found this page might be helpful, but I still don't know how to continuously export data during
simulation.
matlab simulink

share|improve this question

asked Jun 9 '13 at 2:40

Po-Jen Lai
107313

Are you sending the data to a Matlab program or a different program like Ex
program? Tyler Durden Jun 9 '13 at 2:49

I am sending data to Proteus, because I want to write some commands on m


simulink. Po-Jen Lai Jun 9 '13 at 3:16

I know that you can get the data on the simulink lines using
to external mode during the simulation (not sure about other modes). You c
of your simulink) and do this in its callback. pm89
@pm89 Thanks~this is a way that I can try! Po-Jen Lai
add a comment

3 Answers
activeoldest votes

up

Use get_param to read data from just at the current time. Also to send the data back to
vote do Simulink with set_param of a gain or another block.
wn vote An example of get_param
First load and start the simulation:
load_system('myModel')
set_param('myModel','SimulationCommand','Start');
To read data on any line of your simulink model:

accept ed

1.
2.

Get a simulink block object (let's try a Clock with the name Clock):
block = 'myModel/Clock';
rto = get_param(block, 'RuntimeObject');
3.
Then get the data on its first (or any) output port (or input) of that block.
time = rto.OutputPort(1).Data;
You could do the reading, in a timer callback.
Also this might be helpful: Command Line Functionality for Simulink
share|improve this answer

edited Jun 9 '13 at 4:33

answered

pm89
1,4211412
Although this would get you some data, the timer is working in MATLAB and has nothing to do with the Simulink
simulation time, so there's no guarantee of getting data at the simulation times you may need. Phil Goddard
1 '13 at 5:33
I do agree with you @PhilGoddard, but I found this to be the only way. There are however some workarounds and
dirty tricks to prevent missing any data, like increasing the timer frequency (to reduce the chance of loosing any data)
and setting up a counter in simulink (to prevent getting the same data twice and scanning if any data is lost or
not). pm89 Jun 9 '13 at 15:26
The workarounds do not prevent you missing data. They may assist in missing less data, but they cannot guarantee
that you miss no data. Phil Goddard Jun 9 '13 at 15:55
add a comment

up

During simulation Simulink stores logged data in an internal buffer and only writes the data to
vote do the Workspace when the simulation is paused or stopped. It sounds as if you really need to
wn vote write an S-function (which will get signal values on a timestep-by-timestep basis) and
communicate with Proteus that way.

Of course Simulink is a non-realtime simulator, so if you are talking about doing anything
resembling real-time control then you are most likely taking the wrong approach altogether.
share|improve this answer

answered

Phil Goddard
2,4381212
I want to control a robot arm in simulink(simmechanics, actually) using microchip in Porteus as controller. So is this
not achievable? I don't understand the meaning of Simulink is a non-realtime simulator~ Po-Jen Lai
3:55
None of the OS's that MATLAB/Simulink run on are real-time. Amongst other things this means that 1 unit of
simulation time is (almost certainly) not equal to 1 unit of real-time. The clock in Simulink may say it's been running
for 10s, but the clock on your wall may say something less than 10s or something longer than 10s (depending on the
complexity of your model). Unless you only require slow sample rates, and don't care about guaranteed performance,
then for real-time control you need to convert the model to C (using Simulink Coder) and then compile and run that
1 code on a real-time OS. Phil Goddard Jun 9 '13 at 5:24
You can always test your logic with simulink streaming live data to a controller, just put in consideration, that it is not
real time. The main logic can be tested, but critical timings tasks will not be possible (e.g. the reaction time in
microseconds of an event that happens over interrupt or something equivalent) Karim Apr 29 at 9:53
add a comment
up

At any time during simulation you can force Simulink to write the simulation output data to the
vote do workspace:
wn vote
set_param(bdroot,'SimulationCommand','WriteDataLogs');
I've found that this command is quite unstable in my Matlab 2010a for Win64. In particular I
have to avoid it when simulation is stopped (i.e. first
check get_param(bdroot,'SimulationStatus') ), otherwise Matlab shows an error and
asks to be restarted.

share|improve this answ

You might also like