Main Content

Work with Unfinalized and Unsorted MDF Files

This example shows how to work with unfinalized and unsorted MDF files. The unfinalized MDF file used in this example, MDFUnfinalized.MF4, was recorded by a CANedge2 CAN bus data logger from CSS Electronics.

Introduction to Unfinalized and Unsorted MDF Files

Sometimes an MDF file creator tool can experience a premature termination caused by an unexpected power-down or an application error. In such cases, the MDF file might be left in an unfinalized state, possibly violating certain format rules of the ASAM MDF standard or causing data loss during read operations.

In general, a data group can be either sorted or unsorted. Some recording tools write unsorted MDF files without sorting them after the recording completes. A sorted data group cannot contain more than one channel group, while an unsorted data group may contain several channel groups. If all data groups in an MDF file are sorted, the MDF file is sorted; if at least one data group is unsorted, the entire MDF file is unsorted.

An unfinalized MDF file can be either sorted or unsorted. Conversely, an unsorted MDF file can be either finalized or unfinalized.

Use Unfinalized MDF Files in MATLAB

Because unfinalized files can contain format issues and lead to unreliable operations, an error is thrown when attempting to access metadata of an unfinalized MDF file using the mdfInfo function.

try
    info = mdfInfo("MDFUnfinalized.MF4")
catch ME
    disp(ME.message)
end
Cannot perform operation on unfinalized file. Use mdfFinalize to create a finalized file.

You can finalize an unfinalized MDF file using the function mdfFinalize. If the MDF file is both unfinalized and unsorted, mdfFinalize also attempts to sort the file as part of the finalization process.

Use Finalized but Unsorted MDF Files in MATLAB

If an MDF file is finalized but unsorted, you can access the file metadata using the mdfInfo function, but an error might occur if you try to read data from the unsorted file using the mdfRead function.

You can sort a finalized but unsorted MDF file using function mdfSort. If the unsorted MDF file is also unfinalized, using mdfSort on the file causes an error. Instead, use mdfFinalize to finalize and sort the file at the same time.

This example continues to demonstrate the use of mdfFinalize with unfinalized MDF files. However, you can follow a similar workflow to use the mdfSort function on finalized but unsorted MDF files.

Finalize an MDF File In-Place

The mdfFinalize function allows you to finalize an unfinalized MDF file in place by overwriting the source file with a finalized copy.

For demonstration purposes, make a copy of the original file using copyfile, and use the extra copy MDFFinalizedInPlace.MF4 in the subsequent finalization operation.

copyfile("MDFUnfinalized.MF4", "MDFFinalizedInPlace.MF4")

Use mdfFinalize with only the source file name MDFFinalizedInPlace.MF4 specified to create a finalized copy that overwrites itself. The function returns the full path of the finalized file.

finalizedPath1 = mdfFinalize("MDFFinalizedInPlace.MF4")
finalizedPath1 = 
"/tmp/Bdoc24a_2528353_1101874/tp8faccd1d/vnt-ex16754708/MDFFinalizedInPlace.MF4"

MDFFinalizedInPlace.MF4 is now finalized and can be accessed using the mdfInfo function. You can specify the full path returned by mdfFinalize. Alternatively, specify the file name if it is located on MATLAB® path.

info1 = mdfInfo(finalizedPath1)
info1 = 
  MDFInfo with properties:

   File Details
                  Name: "MDFFinalizedInPlace.MF4"
                  Path: "/tmp/Bdoc24a_2528353_1101874/tp8faccd1d/vnt-ex16754708/MDFFinalizedInPlace.MF4"
                Author: ""
            Department: ""
               Project: ""
               Subject: ""
               Comment: ""
               Version: "4.11"
      InitialTimestamp: 2021-04-12 10:06:43.000000000

   Creator Details
     ProgramIdentifier: "CE     "
     CreatorVendorName: ""
       CreatorToolName: "CE"
    CreatorToolVersion: "01.04.01"
       CreatorUserName: ""
        CreatorComment: "Creation and logging of data."

   File Contents
            Attachment: [0x7 table]
     ChannelGroupCount: 8

Get information about all the channel groups in the finalized file using the mdfChannelGroupInfo function. Inspect the Sorted value of each channel group. Note that all channel groups are sorted now.

chanGrpInfo1 = mdfChannelGroupInfo(finalizedPath1)
chanGrpInfo1=8×13 table
    GroupNumber       AcquisitionName         Comment      NumSamples    DataSize    Sorted    SourceName    SourcePath    SourceComment    SourceType    SourceBusType    SourceBusChannelNumber    SourceSimulated
    ___________    _____________________    ___________    __________    ________    ______    __________    __________    _____________    __________    _____________    ______________________    _______________

         1         LIN_TransmissionError    <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         2         LIN_SyncError            <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         3         LIN_TransmissionError    <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         4         LIN_Frame                <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         5         LIN_ChecksumError        <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         6         CAN_RemoteFrame          <undefined>           0            0     true         CAN           CAN             CAN            Bus             CAN                   0                    false     
         7         CAN_ErrorFrame           <undefined>           0            0     true         CAN           CAN             CAN            Bus             CAN                   0                    false     
         8         CAN_DataFrame            <undefined>      118037      2596814     true         CAN           CAN             CAN            Bus             CAN                   0                    false     

When the MDF file is finalized and sorted, you can proceed to use all MDF functionaly, such as extracting data using the mdfRead function.

Finalize an MDF File Out-of-Place

The mdfFinalize function also allows you to finalize an unfinalized MDF file out-of-place by creating a separate finalized copy. Call the function specifying both the source file name and a destination file name.

finalizedPath2 = mdfFinalize("MDFUnfinalized.MF4", "MDFFinalizedOutOfPlace.MF4")
finalizedPath2 = 
"/tmp/Bdoc24a_2528353_1101874/tp8faccd1d/vnt-ex16754708/MDFFinalizedOutOfPlace.MF4"

MDFFinalizedOutOfPlace.MF4 is a newly created finalized copy and can be accessed using the mdfInfo function.

info2 = mdfInfo(finalizedPath2)
info2 = 
  MDFInfo with properties:

   File Details
                  Name: "MDFFinalizedOutOfPlace.MF4"
                  Path: "/tmp/Bdoc24a_2528353_1101874/tp8faccd1d/vnt-ex16754708/MDFFinalizedOutOfPlace.MF4"
                Author: ""
            Department: ""
               Project: ""
               Subject: ""
               Comment: ""
               Version: "4.11"
      InitialTimestamp: 2021-04-12 10:06:43.000000000

   Creator Details
     ProgramIdentifier: "CE     "
     CreatorVendorName: ""
       CreatorToolName: "CE"
    CreatorToolVersion: "01.04.01"
       CreatorUserName: ""
        CreatorComment: "Creation and logging of data."

   File Contents
            Attachment: [0x7 table]
     ChannelGroupCount: 8

Get information about all the channel groups in the finalized file using the mdfChannelGroupInfo function. Inspect the Sorted value of each channel group. Note that all channel groups are sorted now.

chanGrpInfo2 = mdfChannelGroupInfo(finalizedPath2)
chanGrpInfo2=8×13 table
    GroupNumber       AcquisitionName         Comment      NumSamples    DataSize    Sorted    SourceName    SourcePath    SourceComment    SourceType    SourceBusType    SourceBusChannelNumber    SourceSimulated
    ___________    _____________________    ___________    __________    ________    ______    __________    __________    _____________    __________    _____________    ______________________    _______________

         1         LIN_TransmissionError    <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         2         LIN_SyncError            <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         3         LIN_TransmissionError    <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         4         LIN_Frame                <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         5         LIN_ChecksumError        <undefined>           0            0     true         LIN           LIN             LIN            Bus             LIN                   0                    false     
         6         CAN_RemoteFrame          <undefined>           0            0     true         CAN           CAN             CAN            Bus             CAN                   0                    false     
         7         CAN_ErrorFrame           <undefined>           0            0     true         CAN           CAN             CAN            Bus             CAN                   0                    false     
         8         CAN_DataFrame            <undefined>      118037      2596814     true         CAN           CAN             CAN            Bus             CAN                   0                    false     

When the MDF file is finalized and sorted, you can proceed to use all MDF functionality, such as extracting data using the mdfRead function.

Delete Created MDF Files

Delete the MDF files created in this example to clean up the working directory.

delete MDFFinalizedInPlace.MF4 MDFFinalizedOutOfPlace.MF4

Conclusion

Similar to mdfFinalize, the mdfSort function supports sorting operations both in-place and out-of-place. You can apply the same workflow to sort unsorted MDF files.

To summarize:

  • If an MDF file is finalized and sorted, its file information can be accessed using mdfInfo and its data can be read using mdfRead.

  • If an MDF file is finalized and unsorted, its file information can be accessed but its data cannot be read. Use mdfSort to sort the file.

  • If an MDF file is unfinalized, neither its file information nor its data can be accessed. Use mdfFinalize to finalize the file.