Main Content

Import MAT-Files into MATLAB

After logging is complete, you can import MAT-files and open MAT-files in MATLAB®, and use them for further analysis. Since the data points are stored in MAT files, you can directly open the files in MATLAB without converting them into any other format.

The files are named as <modelname>_<runnumber>_<indexnumber>.mat. The name of your Simulink® model is modelname. runnumber is the number of times the model is run. runnumber starts with 1 and is incremented by one for every successive run. indexnumber is the MAT-file number in a run. indexnumber starts with 1 and is incremented by one for every new file that is created in the same run.

Suppose that the name of the model is sdcard. In the first run, Simulink creates sdcard_1_1.mat file and starts logging data in this file. After the logging in the first file is completed, Simulink creates sdcard_1_2.mat file and continues logging data in this file. Likewise, the logging continues in multiple MAT-files until the Simulation stop time is elapsed. If the same model is run again, the new files are named as sdcard_2_1.mat, sdcard_2_2.mat, and so on.


Data loss occurs when:

  • The SD card logging mechanism executes in a background task. If the main Simulink algorithm overruns without enough time for the background task, then data loss occurs as the background task gets no time for execution.

  • The signal logging rate is faster than the SD card writing speed. Therefore, it is important that you use an SD card with good write speeds.

Use getMATFilesFromPixhawk to Retrieve MAT-files

To retrieve MAT-files from the SD Card, first connect the Pixhawk Series hardware board (with the SD Card still inserted) to the host computer.

Use the getMATFilesFromPixhawk function to retrieve the MAT-files. The MAT Files are copied to the current folder in MATLAB.

For details, see getMATFilesFromPixhawk.

The getMATFilesFromPixhawk function provides two options as name-value pairs:

  • ExtractFilesFromAllRuns -

    • Set this value to true to retrieve all MAT-files corresponding to the model name.

    • Set this value to false to retrieve MAT-files corresponding to the model name, but only related to the latest run (only the MAT-files that are updated after you last clicked Build, Deploy & Start)

  • DeleteAfterRetrieval - Set this value to true to delete the MAT Files in the SD card after retrieving the same to the host computer.


    It is recommended that you set this value to true to ensure faster retrieval of MAT-Files from SD card in next runs.

Combine MAT-files Using px4MATFilestitcher and Analyze Variables

You need to combine all the MAT-files obtained from the SD card into a single MAT-file by using the px4MATFilestitcher function, and then analyze the variables for logged data.

For details, see px4MATFilestitcher.

The px4MATFilestitcher function combines all the MAT-files starting with the same name into a single file. The order of the stitching is based on the numeric characters found at the end of the file name. The name of the stitched file ends with __stitched.mat.

You can use the px4MATFilestitcher function at the MATLAB command prompt in these ways:

  • To run the stitcher function for all files in the current folder in MATLAB, use px4MATFilestitcher() format.

  • To run the stitcher function on a specific folder, use px4MATFilestitcher('folder_name') format, where folder_name is the full path of name of the directory in which the MAT-files are present.

Load the stitched MAT-file. For example, run the below command if the name of the stitched file is sdcard_1__stiched.mat:


To view the logged values, double-click the corresponding variable that appears in the Workspace window.