| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
| On this page… |
|---|
Real-Time Workshop MAT-file data logging facility enables a generated program to save system states, outputs, and simulation time at each model execution time step. The data is written to a MAT-file, named (by default) model.mat, where model is the name of your model. In this tutorial, data generated by the model f14rtw.mdl is logged to the file f14rtw.mat. Refer to Building a Generic Real-Time Program for instructions on setting up f14rtw.mdl in a working directory if you have not done so already.
To configure data logging, click Data Import/Export in the center pane of the Model Explorer. The process is the same as configuring a Simulink model to save output to the MATLAB workspace. For each workspace return variable you define and enable, the Real-Time Workshop software defines a parallel MAT-file variable. For example, if you save simulation time to the variable tout, your generated program logs the same data to a variable named rt_tout. You can change the prefix rt_ to a suffix (_rt), or eliminate it entirely. You do this by selecting Real-Time Workshop in the center pane of the Model Explorer, then clicking the Interface tab.
Note Simulink lets you log signal data from anywhere in a model via the Log signal data option in the Signal Properties dialog box (accessed via context menu by right-clicking signal lines). The Real-Time Workshop software does not use this method of signal logging in generated code. To log signals in generated code, you must either use the Data Import/Export options described below or include To File or To Workspace blocks in your model. |
In this tutorial, you modify the f14rtw model so that the generated program saves the simulation time and system outputs to the file f14rtw.mat. Then you load the data into the MATLAB workspace and plot simulation time against one of the outputs. The f14rtw model should be open and configured as it was at the end of the previous tutorial.
To use the data logging feature:
Open Model Explorer by selecting Model Explorer from the model's View menu.
In the Model Hierarchy pane, click the + sign preceding the model name to reveal its components.
Click Configuration (Active) in the left pane.
Click Data Import/Export in the center pane. The Data Import/Export pane appears at the right. Its Save to workspace section lets you specify which outport data is to be saved to the workspace and what variable names to use for it.
Select the Time option. This tells Simulink to save time step data during simulation as a variable named tout. You can enter a different name to distinguish different simulation runs (for example using different step sizes), but take the default for this tutorial. Selecting Time enables the Real-Time Workshop code generator to create code that logs the simulation time to a MAT-file.
Select the Output option. This tells Simulink to save output signal data during simulation as a variable named yout. Selecting Output enables the Real-Time Workshop code generator to create code that logs the root Output blocks (Angle of Attack and Pilot G Force) to a MAT-file.
If any other options are enabled, clear them. Set Decimation to 1 and Format to Array. The figure below shows the dialog.

Open the Pilot G Force Scope block of the model, then run the model by choosing Simulation > Start in the model window. The resulting Pilot G Force scope display is shown below.

Verify that the simulation time and outputs have been saved to the MATLAB workspace in MAT-files. At the MATLAB prompt, type:
whos *out
Simulink displays:
Name Size Bytes Class Attributes tout 601x1 4808 double yout 601x2 9616 double
Verify that Pilot G Force was correctly logged by plotting simulation time versus that variable. At the MATLAB prompt, type:
plot(tout,yout(:,2))
The resulting plot is shown below.

In the second part of this tutorial, you build and run a Real-Time Workshop executable of the f14rtw model that outputs a MAT-file containing the simulation time and outputs you previously examined. Even though you have already generated code for the f14rtw model, you must now regenerate that code because you have changed the model by enabling data logging. The steps below explain this procedure.
To avoid overwriting workspace data with data from simulation runs, the Real-Time Workshop code generator modifies identifiers for variables logged by Simulink. You can control these modifications from the Model Explorer:
Open Model Explorer by selecting Model Explorer from the model's View menu.
In the Model Hierarchy pane, click the + sign preceding the model name to reveal its components.
Click Configuration (Active) in the left pane.
In the center pane, click Real-Time Workshop. The Real-Time Workshop pane appears to the right.
Click the Interface tab.
Set MAT-file variable name modifier to _rt. This adds the suffix _rt to each variable that you selected to be logged in the first part of this tutorial (tout, yout).
Clear the Generate code only check box, if it is currently selected. The pane should look like this:

To generate code and build an executable, click the Build button.
When the build concludes, run the executable with the command:
!f14rtw
The program now produces two message lines, indicating that the MAT-file has been written.
** starting the model ** ** created f14rtw.mat **
Load the MAT-file data created by the executable and look at the workspace variables from simulation and the generated program by typing:
load f14rtw.mat whos tout* yout*
Simulink displays:
Name Size Bytes Class Attribute tout 601x1 4808 double tout_rt 601x1 4808 double yout 601x2 9616 double yout_rt 601x2 9616 double
Note that all arrays have the same number of elements.
Observe that the variables tout_rt (time) and yout_rt (Pilot G Force and Angle of Attack) have been loaded from the file. Plot Pilot G Force as a function of time.
plot(tout_rt,yout_rt(:,2))
The resulting plot is identical to the plot you produced in step 10 of the previous part of this tutorial:

![]() | Building a Generic Real-Time Program | Code Verification | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |