Table of Contents > QUARC > User's Guide > QUARC Basics for Models > Data Collection
Streaming to the MATLAB Workspace
Data can be collected from the model during real-time execution or normal simulation, and stored to a variable in the MATLAB workspace. It is useful to store data in the MATLAB workspace so that you can use MATLAB functions to analyze the data or use MATLAB plotting functions for visualization purposes. You can also set conditions on when you want to collect the data in external mode. For more information on how to set these conditions, please refer to the Signal Triggering section in this documentation.
There are three methods to save data to a variable in the MATLAB workspace. The following list may be used to refer to each method:
Using the Scope Parameters Dialog
The Scope block available in the Simulink/Sinks category of the Simulink library can be configured to save the data being plotted to the MATLAB workspace. Configuring a Scope in this manner is convenient because Scopes are typically attached to the signals of interest. Scopes are able to save both scalar and vector input signals to the MATLAB workspace. For further details on the Scope block, please refer to the Scope, Floating Scope, Signal Viewer Scope block reference page in the Simulink documentation.
In order to save data to the MATLAB workspace using a Scope, following these steps:
After inserting a "Scope" block in your Simulink model, double-click on the block to open the Scope window. Then click on the second toolbar button, dubbed the Parameters button, on the Scope display as illustrated below.
Once in the Parameters dialog, there are two panes available: General and Data history. Choose the Data history pane. There are four parameters in this pane. Below is an illustration of the Data history pane in the Scope Parameters dialog.
The first parameter in the pane is called Limit data points to last, which limits the number of sample points saved to the MATLAB workspace. Perform one of the following steps:
If you are using normal simulation mode for your model, select the Limit data points to last check box and enter the number of points to save as shown below.
If you want to run your model in real-time, it is recommended that the data points be limited using the Duration parameter in the Signal and Triggering dialog of the External Mode Control Panel. These two parameters are used for the same purpose, but one is used in external mode while the other one is used for normal simulation. For further details on the Duration parameter, please refer to the Duration parameter definition section.
The second parameter in the Scope Parameters pane is called Save data to workspace. It enables saving of the data to the MATLAB workspace, if selected. Select its check box. Notice that the Variable name and Format parameters become active once the Save data to workspace option is enabled. The figure below illustrates this case.
The Variable name field identifies the name of the variable to which you want to
save your data. The default name for the variable is ScopeData
. Change it to another variable name
if desired.
The last parameter is called Format and it specifies the format of the data when it is saved in the workspace variable. There are three formats that may be selected from the drop down list, as shown below.
Select Array
only for a Scope with one set of axes. For Scopes with more than one set of axes,
select Structure
if you do not want to store time data and
select Structure with time
if you want to store time data.
Click on OK.
It is important to note that after you apply the changes to the Scope parameters, you have to rebuild your model when using external mode. Otherwise, Simulink will complain that the model checksum is invalid and the code must be rebuilt. In normal simulation mode, all you need to do is save your model after you apply the changes.
After running your model, the saved variable can be found in the MATLAB workspace and you can use different MATLAB functions to manipulate or visualize the saved data.
Using the block
You can use the Simulink To Workspace block in order to save data to a variable in the MATLAB workspace in both real-time and normal simulation. You can find the To Workspace block under the Simulink/Sinks category of the Simulink library. For more information on the block itself, please refer to the
MATLAB Command Line
Click to copy the following command line to the clipboard. Then paste it in the MATLAB Command Window:
matlab: doc('To Workspace')Please perform the following steps in order to save data to the MATLAB workspace using the To Workspace block:
Once you have inserted the To Workspace block in your Simulink model, double-click on the block. The block parameters dialog should appear as shown below.
The first parameter is the Variable name, which defines the name of the
variable to which you want to save your data. The default name is simout
. Change it to the name
you wish to use.
The next parameter in the dialog, called Limit data points to last, limits
the number of sample points saved to the MATLAB workspace. The default value is inf
, meaning all the data
is saved. Enter a value for this parameter if you are using normal simulation. In external mode,
it is recommended that the number of data points to save be limited via the Duration parameter in
the Signal & Triggering dialog of the External Mode Control Panel.
These two parameters are used for the same purpose, but one is used in external mode while the other one is used in
normal simulation. For further details on the Duration parameter, please
refer to the Duration parameter definition section.
Note: the Decimation may be used to write every n
th sample instead of
every data point, where n
is
the decimation factor. The default value is 1
, meaning data is written at every step time. In general,
decimation is not required so the default value should be used. The
Sample time parameter specifies the sampling interval at which to collect points.
The default value is -1
, meaning the sample time is inherited from the input signal. The default is
sufficient in the majority of cases.
The Save format parameter specifies the format in which to save the data. There are three formats that may be selected from the drop down list, as shown below.
Select Array
to save the data as a matrix. The array format includes the time.
Choose Structure
if you do not need to
store time data and Structure with time
if time data is required with the structure.
Click on OK.
It is important to note that after you apply the changes to the To Workspace parameters, you have to rebuild your model when using external mode. Otherwise, Simulink will complain that the model checksum is invalid and the code must be rebuilt. In normal simulation mode, all you need to do is save your model after you apply the changes.
After running your model, the saved variable can be found in the MATLAB workspace and you can use different MATLAB functions to manipulate or visualize the saved data.
Using the Data Import/Export Pane
The Data Import/Export pane in the Configuration Parameters dialog may be used to save data to the MATLAB workspace in normal simulation. However, unlike the other techniques for streaming data to the MATLAB workspace, the Data Import/Export pane cannot be used to stream data directly to the MATLAB workspace in external mode. Instead, this pane is used in external mode in conjunction with MAT-file logging to stream data to disk. The resulting MAT-file may then be loaded into the MATLAB workspace. For more information on MAT-file logging, please refer to the MAT-File Logging section of this documentation. Below is an illustration of the Data Import/Export pane.
Notice that there are three panels in the Data Import/Export pane: Load from workspace, Save to workspace and Save options. The first panel is for importing data from the workspace into your model. Since this section deals with streaming data to the MATLAB workspace, this panel will not be discussed here. The other two panels are for saving data to the workspace.
Save to Workspace Panel
The following are the descriptions of some parameters in the Save to workspace panel. Refer to the Simulink documentation for the detailed explanaations about eacah of the parameters:
Time: the variable to which you want to save your time data. The default name for this
variable is tout
and may be changed to any desired name.
States: the variable to which you want to save your model's states. The default name for this
variable is xout
and may be changed to any desired name.
Output: the variable to which you want to save signal data, such as the signals feeding
root-level Outport blocks. The default name for this
variable is yout
and may be changed to any desired name.
Final states: the variable to which you want to save your model's final states at the end of
simulation. The default name for this variable is xFinal
and may be changed to any
desired name.
Signal Logging: data from global signal logging for your model gets saved into
this variable. The default name for this variable is logsout
and may be changed
to any desired name.
The description of the last parameter is out of the scope of this document.
You may choose which data to save to the MATLAB workspace by enabling the parameter associated with the desired information. For example, if you only want to save time data and signal data from your model, enable the Time and Output parameters only.
Save Options Panel
The third panel in the pane is called Save options and is used to specify how the data should be saved in the MATLAB workspace. The following are descriptions of each parameter in this panel:
Limit data points to last: limits the number of sample points saved to the MATLAB workspace.
Decimation: allows you to write data at every nth sample, where n is
the decimation factor. The default value is 1
meaning data is written at every timestep.
After setting all the parameters in this pane, you can click on OK for the changes to take effect in your model. In normal simulation mode, there is no need to build your model. All you need to do is run the simulation. In external mode, however, in the event that you are doing MAT-file logging, you do need to rebuild the model. Otherwise, Simulink will issue an error message about the model checksum being out of date. For a detailed discussion of the Data Import/Export pane please refer to the
MATLAB Command Line
Click to copy the following command line to the clipboard. Then paste it in the MATLAB Command Window:
qc_open_matlab_help('data_import_export')
Copyright ©2024 Quanser Inc. This page was generated 2024-10-17. Submit feedback to Quanser about this page.
Link to this page.