MATLAB Examples

MAT-file Logging on SD Card for NXP FRDM-K64F Board

This example shows you how to log signals from a Simulink® model in the form of MAT-files on SD card mounted on NXP FRDM-K64F board.

Copyrights(C) 2017 The MathWorks. Inc.

Contents

Introduction

Simulink Coder Support Package for NXP FRDM-K64F Board supports logging of signals from your Simulink® model. These signals are logged as MAT-file(s) on the SD card mounted on NXP FRDM-K64F board.

Signal logging enables you to monitor the signal behavior and to perform any historical analysis of the data. The data can be saved in any of these formats: Structure, Structure with time, or Array.You can save the signals using the following blocks.

  • To Workspace
  • Scope
  • Output port

This example provides you the workflow to enable MAT-file logging on NXP FRDM-K64F board.

Prerequisites

Before you start with this example, we recommend you to complete

Getting Started with Simulink Coder Support Package for NXP FRDM-K64F Board.

SD Card Logging.

Required Hardware

To run this example you need the following hardware:

  • NXP FRDM-K64F board
  • USB type A to Micro-B cable
  • SD card

Task 1 - Configure a Simulink Model for MAT-File Logging

1. Connect the NXP FRDM-K64F Board to your computer with a USB cable.

2. Insert the SD card in the slot provided on the board.

3. Open the SD Card Logging model. This model is pre-configured for the NXP FRDM-K64F target with MAT-File logging enabled. In this example, signals from the Scope block is logged.

To learn how to setup a model for this target, please see the Getting Started with NXP FRDM-K64F Support Package example.

To enable MAT-File Logging follow step 4 - 9

4. To open the Model Configuration Parameters window, click the gear icon on the Simulink® toolbar.

5. Browse to Code Generation > Interface > Advanced Parameters. Select the MAT-file logging option. Alternatively, you can type MAT-file logging in the search box.

6. Apply the changes and click OK.

7. Double-click the Scope block. Click the gear icon to open the Configuration Parameters dialog box. In the Logging tab, select the Log data to Workspace option.

8. Apply the changes and click OK.

9. In the Simulink® model, provide Simulation stop time.This will be the duration for which the signal is logged. However, the model continues to run on the hardware and is not affected by the time specified.

For example, in the demo model provided "Simulation stop time" is 10. The signal will be logged till time 10 seconds. For " Simulation stop time = Inf " the signal will be logged till the SD card is full or the board is disconnected.

Task 2 - Deploy Code on NXP FRDM-K64F Board

Ensure the model is configured to be deployed on the NXP FRDM-K64F hardware.

1. In the model, click the Deploy To Hardware button on the toolbar. The model is built and deployed on the NXP FRDM-K64F board.On successful deployment, the LED on the board will begin to blink.

2. To view the logged MAT-files, remove the SD card from the board and insert the same on your computer.

On successful deployment of the model, the MAT-files are created on the SD card as shown below:

The model running on hardware creates multiple MAT-files depending on the duration for which the signals are logged. The files are named as '<modelname>_<runindex>_<fileindex>.mat'. '<modelname>' is the name with which the Simulink® model is saved. '<runindex>' starts with 1 and is incremented by one during successive runs.<fileindex> starts with 1 and is incremented by 1 every time a new MAT file is created in the same run.

After importing the MAT-files, you can use it like a regular MAT-file for any further analysis in MATLAB®.

You can create a stitcher function to combine these MAT-files into a single MAT-file that contains all the logged data points. An example of the MAT-file stitcher is provided. In the MATLAB® command window, enter the following command and refer the file to know more about its usage.

edit('Freedomk64f_MAT_stitcher.m');

Other things to try

In this example, a Scope block is used to log signals. Signals in a Simulink® model can also be logged using the To Workspace block or by logging the output(s) of the Simulink® model.

Explore signal logging using the other two techniques.

Summary

This example demonstrated the workflow involved in enabling MAT-file logging on NXP FRDM-K64F hardware.