Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Data Logging

Tutorial Overview

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.

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.

Data Logging During Simulation

To use the data logging feature:

  1. Open Model Explorer by selecting Model Explorer from the model's View menu.

  2. In the Model Hierarchy pane, click the + sign preceding the model name to reveal its components.

  3. Click Configuration (Active) in the left pane.

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

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

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

      Note   The sort order of the yout array is based on the port number of the Outport blocks, starting with 1. Angle of Attack and Pilot G Force are logged to yout(:,1) and yout(:,2), respectively.

  7. If any other options are enabled, clear them. Set Decimation to 1 and Format to Array. The figure below shows the dialog.

  8. Click Apply to register your changes.

  9. Save the model.

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

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

Data Logging from Generated Code

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:

  1. Open Model Explorer by selecting Model Explorer from the model's View menu.

  2. In the Model Hierarchy pane, click the + sign preceding the model name to reveal its components.

  3. Click Configuration (Active) in the left pane.

  4. In the center pane, click Real-Time Workshop. The Real-Time Workshop pane appears to the right.

  5. Click the Interface tab.

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

  7. Clear the Generate code only check box, if it is currently selected. The pane should look like this:

  8. Click Apply to register your changes.

  9. Save the model.

  10. To generate code and build an executable, click the Build button.

  11. When the build concludes, run the executable with the command:

    !f14rtw
  12. The program now produces two message lines, indicating that the MAT-file has been written.

    ** starting the model ** 
    ** created f14rtw.mat ** 
  13. 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.

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

  


Related Products & Applications

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