Main Content

Log signals to MDF file

Log signal and output data from generated code to MDF file

Since R2024a

Model Configuration Pane: Code Generation / Interface

Description

Generate code that logs signals and outputs to an ASAM Measurement Data Format (MDF) file. After executing the generated code on your target hardware, you can view the time series of generated code variables by importing the MDF file into the Simulation Data Inspector. You can also use Vehicle Network Toolbox™ MDF-file functions to analyze the time series.

Dependencies

The code generator produces code that logs:

  • Signal data if the Signal logging check box is selected.

  • Output data if the Output check box is selected.

Settings

Off (default) | On
On

Generate code that logs data to an MDF file.

Off

Do not generate code that logs data to an MDF file.

Examples

expand all

This example shows how you can generate code that logs signals and outputs to an MDF file.

Create a simple model:

  1. Open Simulink® and create a simple model, mdfTest, which contains these blocks:

    • Sine Wave

    • Gain

    • Outport

  2. Double-click the Sine Wave block. Set Sample time to 0.1, and then click OK.

  3. Double-click the Gain block. Set Gain to 2, and then click OK.

  4. Connect the Sine Wave block to the Gain block and name the connection Test Signal.

  5. Connect the Gain block to the Outport block and name the connection Output Signal.

  6. Configure Test Signal for logging:

    1. Select Test Signal.

    2. In the Prepare gallery, click Log Signals.

  7. Verify that the Signal logging and Output check boxes are selected.

mdfTest model.

Run a model simulation and view signal and output data:

  1. In the Simulate section, click Run.

  2. When the simulation is complete, in the Review Results gallery, click Data Inspector. The Simulation Data Inspector displays Test Signal and Output Signal.

    Output and Test signals produced by simulation of mdfTest model.

Use Embedded Coder® to generate code that logs signal and output data to an MDF file:

  1. In the Apps gallery, under Code Generation, click Embedded Coder.

  2. On the C Code tab, in the Prepare gallery, click Settings, which opens the Configuration Parameters dialog box.

  3. Specify these settings:

    • Log signals to MDF file — On

    • Solver Type — Fixed-step

    • Fixed-step size (fundamental sample time) — 0.1

    • Treat each discrete rate as a separate task — On

    • Support long long — On

  4. In the Generate Code section, click Build. The software generates and builds code, producing an executable file. For example, mdfTest.exe.

Run the file on your target hardware, which, in this example, is your development computer. In the Command Window, enter:

system('mdfTest')
The executed code logs data to mdfTest.mf4.

Import the MDF file into the Simulation Data Inspector. In the Command Window, enter:

mdfVisualize('mdfTest.mf4')

The Simulation Data Inspector displays the values of the logged variables and the simulation time for each rate against the timestamp at which the values were logged – information that the file stores. To display the logged variables against the simulation time, create XY plots.

Click the Visualizations and layouts button and use the signals to create XY plots.

XY Data dialog box that shows SimulationTime-OutputSignal and SimulationTime-TestSignal pairings.

When you click OK, the Simulation Data Inspector displays data from the MDF file.

XY Data dialog box that shows the SimulationTime-OutputSignal and SimulationTime-TestSignal pairings.

You can also use Vehicle Network Toolbox MDF-file functions to analyze the time series. For example, to read the data, run:

mdfData = mdfRead('mdfTest.mf4')

Recommended Settings

ApplicationSetting
DebuggingOn
TraceabilityNo impact
EfficiencyOff
Safety precautionNo recommendation

Programmatic Use

Parameter: LogToMDFFile
Type: character vector
Value: 'on' | 'off'
Default: 'off'

Limitations

An MDF file does not contain Boolean data types. The generated code logs Boolean values as integers.

Logging to MDF file is not supported when the configuration parameter ExtMode is 'on'.

Version History

Introduced in R2024a