Start of trail Streaming to Disk navigation bar

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:

  1. 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.

  2. 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.

  3. 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:

  4. 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.

  5. 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.

  6. 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.

  7. 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

To Workspace

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')
block reference page in the Simulink documentation.

Please perform the following steps in order to save data to the MATLAB workspace using the To Workspace block:

  1. 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.

  2. 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.

  3. 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 nth 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.

  4. 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.

  5. 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:

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:

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

Data Import/Export Pane

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')
section in the Simulink documentation.

 

navigation bar