quarc_data_logging_demo.mdl
Open this model

QUARC Data Logging Demo

This example demonstrates how to log data into a variable in the MATLAB workspace using the "To Workspace" block from Simulink. It also demonstrates how to stream data to a MAT-file using the "To File" block from Simulink, the "To Host File" block from the QUARC targets library and the MAT-file logging feature of Simulink. For details of the "To Host File" block, please refer to the To Host File block reference page in the QUARC documentation. For information on the MAT-file logging feature, you can refer to the MAT-File Logging section. For a complete discussion on how to stream data to workspace and disk, please refer to the Data Collection section of the QUARC documentation.

Data Logging Demo Simulink Diagram

Configuring the Demonstration

To set up the demonstration, double-click on the Enabled Subsystem to access the data logging blocks. Make sure that the variable name is different for each of the three blocks. Otherwise, one block might overwrite the data saved by another block. Also, make sure that the MAT-file names are different for the "To File" and the "To Host File" blocks to avoid overwriting the files.

Next, open the Configuration Parameters dialog by pressing Ctrl+E or selecting the Simulation/Configuration Parameters... menu item. Navigate to the Code Generation/Interface pane and make sure that the MAT-file logging parameter is enabled.

Demonstration

Building, deploying and starting the model

Refer to One Step Process in QUARC Basic Procedures to build, deploy and start the model.

Running the model

Double-click on the "Sawtooth" Scope block to display the sawtooth wave generated by the Signal Generator.

The "Sawtooth" Scope traces a sawtooth wave for 5 seconds, since its input comes from the Enabled Subsystem. The Enable signal for this subsystem is a step signal with duration of 5 seconds (starting at the 2 seconds mark and finishing 5 seconds later).

Scope

The model stops after 10 seconds. At this time, a variable called sawtooth_workspace is created in the MATLAB workspace. Type the following command in the MATLAB command window to plot the data saved in this variable:

plot(sawtooth_workspace(:,1));

title('Data Logged by To Workspace Block');

Figure

The "To File" block saves its input into a MAT-file called quarc_data_logging_t.mat. The "_t" suffix is added to indicate that this file is saved on the target machine. In our case, the host and target are the same PC. If we ran this model on a remote target, the "To File" block would save the MAT-file on the target machine, in a directory specified by the MEX-file arguments parameter. More details can be found in the Using the To File Block section of the QUARC documentation.

Browse to the directory in which the quarc_data_logging_t.mat is saved. Right-click on the file in the MATLAB Current Directory window and choose the Import data... context menu item. The Import Wizard window pops up. Make sure the checkbox besides the sawtooth_target variable is checked. Click on the Finish button to import this variable to the MATLAB workspace.

After importing the variable, type the following line in the MATLAB command window to plot the data saved in the sawtooth_target variable:

plot(sawtooth_target(2,:));

title('Data Logged by To File Block');

Figure

The "To Host File" block saves its input into a MAT-file called quarc_data_logging_h.mat. The "_h" postfix is added to indicate that this file is saved on the host machine in the current working directory set for MATLAB. More details can be found in the Using the To Host File Block section of the QUARC documentation.

Find the quarc_data_logging_h.mat file, right-click on the file in the MATLAB Current Directory window and choose the Import data... context menu item. The Import Wizard window pops up. Make sure the checkbox besides the sawtooth_host variable is checked. Click on the Finish button to import this variable to the MATLAB workspace.

After importing the variable, type the following line in the MATLAB command window to plot the data saved in the sawtooth_host variable:

plot(sawtooth_target(2,:));

title('Data Logged by To Host File Block');

Figure

With MAT-file logging enabled, a MAT-file gets created for all the workspace variables created by any blocks streaming data to workspace. In addition, any signal connected to an "Outport" block will be saved into a variable in the MAT-file. The name of this MAT-file is "model.mat" where "model" is the name of the model. In our case the name is quarc_data_logging.mat. This file is saved on the target machine. In our case, the host and target are the same PC. If we ran this model on a remote target, the MAT-file would be saved on the target machine, in a directory specified by the MEX-file arguments parameter. To upload the MAT-file from the target machine to the host, simply select the Upload MAT File... menu item from the QUARC menu of the Simulink diagram. It will upload the MAT-file from the target and place it in your current MATLAB directory.

More details can be found in the MAT-File Logging section of the QUARC documentation.

Browse to the directory in which the quarc_data_logging.mat is saved. Right-click on the file in the MATLAB Current Directory window and choose the Import data... context menu item. The Import Wizard window pops up. Make sure the checkbox besides the rt_sawtooth_workspace variable is checked. Click on the Finish button to import this variable to the MATLAB workspace. Note that the "rt_" prefix is added to indicate that this variable has been saved using MAT-file logging. In addition, notice that the only variable being saved in the MAT-file besides the rt_tout is the sawtooth_workspace variable saved by the "To Workspace" block in our main diagram.

After importing the variable, type the following line in the MATLAB command window to plot the data saved in the rt_sawtooth_workspace variable:

plot(rt_sawtooth_workspace(:,1));

title('Data Logged Using MAT-File Logging');

Figure

Running the example on a different target

To run the example on a different target, refer to the instructions on the Running QUARC Examples on Remote Targets page.