Main Content

Work with MDF Events

This example shows how to read events from MDF files and filter data using events.

View All Events in MDF File

View metadata and events of an MDF file using mdfInfo by specifying the file name.

fileInfo = mdfInfo("VehicleDataWithEvents.mf4")
fileInfo = 
  MDFInfo with properties:

   File Details
                  Name: "VehicleDataWithEvents.mf4"
                  Path: "C:\Users\komar\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\komar.Bdoc.DOC\vnt-ex41269135\VehicleDataWithEvents.mf4"
                Author: ""
            Department: ""
               Project: ""
               Subject: ""
               Comment: "Example file demonstrating workflows of reading events."
               Version: "4.10"
      InitialTimestamp: 2024-04-23 18:35:45.000000000

   Creator Details
     ProgramIdentifier: "MDF4Lib"
     CreatorVendorName: ""
       CreatorToolName: ""
    CreatorToolVersion: "1.0"
       CreatorUserName: ""
        CreatorComment: "created"

   File Contents
            Attachment: [0×7 table]
     ChannelGroupCount: 2
                 Event: [8×8 eventtable]

The Event property contains an eventtable of all the events within the MDF file.

fileEvents = fileInfo.Event
fileEvents = 8×8 eventtable
  Event Labels Variable: Name
  Event Lengths Variable: <instantaneous>

      Time            Name                                  Comment                                    Type           Cause      ScopeType      GroupNumber    RangeType    LinkAddress
    ________    ________________    ________________________________________________________    __________________    ______    ____________    ___________    _________    ___________

    0 sec       "USER COMMENT"      "Logging Vehicle Data 12/24/23"                             Marker                User      File                NaN          Point       ""        
    1 sec       "REC BEGIN"         "Recording has started successfully."                       Recording             Tool      File                NaN          Begin       "0x1C80"  
    1.2 sec     "REC INTERRUPT"     "Recording was interrupted."                                RecordingInterrupt    Script    ChannelGroup          1          Point       ""        
    8.5 sec     "USER REC BEGIN"    "User-initiated subrecording start."                        Recording             User      ChannelGroup          1          Begin       "0x1EC8"  
    10 sec      "TIME TRIGGER"      "Time min threshold trigger of 10 seconds has been hit."    Trigger               Script    ChannelGroup          2          Point       ""        
    11.5 sec    "USER REC END"      "User-initiated subrecording end."                          Recording             User      ChannelGroup          1          End         "0x1EC8"  
    14.1 sec    "RPM TRIGGER"       "RPM max threshold trigger of 1500 has been hit."           Trigger               Script    ChannelGroup          1          Point       ""        
    15 sec      "REC END"           "Recording has stopped successfully."                       Recording             Tool      File                NaN          End         "0x1C80"  

Each event has a timestamp and 8 metadata values.

  • The ScopeType value represents whether the event applies to a particular channel group or the entire file.

  • The GroupNumber value represents the particular channel group the event is scoped to.

  • Events can occur either at a point in time or over a range of time. The RangeType value represents if the event occurs at a point of time, marks the beginning of a range, or marks the end of a range.

  • The LinkAddress value is used to link corresponding range events. A pair of beginning and ending range events share the same LinkAddress value, and that value represents the address of the beginning event.

Read All Data and Events from MDF File

The easiest way to read all data and events from an MDF file is to call the mdfRead function with just the file name. Each timetable represents data read from the corresponding channel group. Each timetable contains an eventtable which represents file level events and channel group level events scoped to the corresponding channel group.

data = mdfRead("VehicleDataWithEvents.mf4")
data=2×1 cell array
    {160×3 timetable}
    { 16×3 timetable}

data{1}
ans=160×3 timetable
                        t       EngineRPM    Throttle    OutputTorque
                     _______    _________    ________    ____________

    USER COMMENT     0 sec          41            7           34     
                     0.1 sec        10          9.5           25     
                     0.2 sec        98            9           14     
                     0.3 sec        94          6.5           48     
                     0.4 sec       121            9           15     
                     0.5 sec       141          7.5           47     
                     0.6 sec        87            9           47     
                     0.7 sec        74          5.5           10     
                     0.8 sec       172            6           29     
                     0.9 sec       106         12.5           54     
    REC BEGIN        1 sec         147           11           31     
                     1.1 sec       148         14.5           23     
    REC INTERRUPT    1.2 sec       187           15           47     
                     1.3 sec       224          9.5           24     
                     1.4 sec       162           10           37     
                     1.5 sec       214          8.5           26     
      ⋮

Retrieve the events attached to the timetable by accessing its Events property.

chanGrp1Events = data{1}.Properties.Events
chanGrp1Events = 7×8 eventtable
  Event Labels Variable: Name
  Event Lengths Variable: <instantaneous>

      Time            Name                               Comment                                Type           Cause      ScopeType      GroupNumber    RangeType    LinkAddress
    ________    ________________    _________________________________________________    __________________    ______    ____________    ___________    _________    ___________

    0 sec       "USER COMMENT"      "Logging Vehicle Data 12/24/23"                      Marker                User      File                NaN          Point       ""        
    1 sec       "REC BEGIN"         "Recording has started successfully."                Recording             Tool      File                NaN          Begin       "0x1C80"  
    1.2 sec     "REC INTERRUPT"     "Recording was interrupted."                         RecordingInterrupt    Script    ChannelGroup          1          Point       ""        
    8.5 sec     "USER REC BEGIN"    "User-initiated subrecording start."                 Recording             User      ChannelGroup          1          Begin       "0x1EC8"  
    11.5 sec    "USER REC END"      "User-initiated subrecording end."                   Recording             User      ChannelGroup          1          End         "0x1EC8"  
    14.1 sec    "RPM TRIGGER"       "RPM max threshold trigger of 1500 has been hit."    Trigger               Script    ChannelGroup          1          Point       ""        
    15 sec      "REC END"           "Recording has stopped successfully."                Recording             Tool      File                NaN          End         "0x1C80"  

data{2}
ans=16×3 timetable
                      t       VehicleSpeed    Brake    Gear
                    ______    ____________    _____    ____

    USER COMMENT    0 sec          82           0       1  
    REC BEGIN       1 sec          97           1       2  
                    2 sec          45           1       1  
                    3 sec          58           1       0  
                    4 sec          42           0       3  
                    5 sec         146           1       1  
                    6 sec         155           1       0  
                    7 sec         130           0       0  
                    8 sec         110           0       3  
                    9 sec         161           1       3  
    TIME TRIGGER    10 sec        153           0       2  
                    11 sec        134           0       3  
                    12 sec        160           1       2  
                    13 sec        159           0       1  
                    14 sec        198           0       1  
    REC END         15 sec        157           1       0  

chanGrp2Events = data{2}.Properties.Events
chanGrp2Events = 4×8 eventtable
  Event Labels Variable: Name
  Event Lengths Variable: <instantaneous>

     Time          Name                                 Comment                               Type       Cause      ScopeType      GroupNumber    RangeType    LinkAddress
    ______    ______________    ________________________________________________________    _________    ______    ____________    ___________    _________    ___________

    0 sec     "USER COMMENT"    "Logging Vehicle Data 12/24/23"                             Marker       User      File                NaN          Point       ""        
    1 sec     "REC BEGIN"       "Recording has started successfully."                       Recording    Tool      File                NaN          Begin       "0x1C80"  
    10 sec    "TIME TRIGGER"    "Time min threshold trigger of 10 seconds has been hit."    Trigger      Script    ChannelGroup          2          Point       ""        
    15 sec    "REC END"         "Recording has stopped successfully."                       Recording    Tool      File                NaN          End         "0x1C80"  

Filter Data Using Events

To filter MDF data based on events, use an eventfilter. Create a subset of channel group 1 data containing samples that share a timestamp with an event.

EF = eventfilter(data{1})
EF = 
  eventfilter with no constraints and no selected variables

    <unconstrained>

  VariableNames: Time, Name, Comment, Type, Cause, ScopeType, GroupNumber, RangeType, LinkAddress

dataFilteredToEventTimestamps = data{1}(EF, :)
dataFilteredToEventTimestamps=7×3 timetable
                         t        EngineRPM    Throttle    OutputTorque
                      ________    _________    ________    ____________

    USER COMMENT      0 sec           41            7           34     
    REC BEGIN         1 sec          147           11           31     
    REC INTERRUPT     1.2 sec        187           15           47     
    USER REC BEGIN    8.5 sec        896         44.5          121     
    USER REC END      11.5 sec      1158         59.5          160     
    RPM TRIGGER       14.1 sec      1503         75.5          172     
    REC END           15 sec        1519           80          176     

Create a subset of channel group 1 data containing samples between the user-initiated recording start event and the user-initiated recording end event.

TR = timerange(EF.Name == "USER REC BEGIN", EF.Name == "USER REC END")
TR = 
	timetable timerange subscript:

		Select timetable rows with event times in the half-open interval:
		  Starting at, including:   Name == "USER REC BEGIN"
		  Ending at, but excluding: Name == "USER REC END"

	See Select Times in Timetable.

dataFilteredToUserRecEventTimeRange = data{1}(TR, :)
dataFilteredToUserRecEventTimeRange=30×3 timetable
                         t       EngineRPM    Throttle    OutputTorque
                      _______    _________    ________    ____________

    USER REC BEGIN    8.5 sec       896         44.5          121     
                      8.6 sec       935           46          105     
                      8.7 sec       912         47.5          103     
                      8.8 sec       961           52          110     
                      8.9 sec       941         45.5          138     
                      9 sec         957           51          132     
                      9.1 sec       999         50.5          103     
                      9.2 sec       920           46           95     
                      9.3 sec       999         47.5          131     
                      9.4 sec       941           56           99     
                      9.5 sec       973         49.5          130     
                      9.6 sec      1042           53          134     
                      9.7 sec       996         55.5          104     
                      9.8 sec      1012           51          117     
                      9.9 sec      1044         50.5          138     
                      10 sec       1076           59          118     
      ⋮

Visualize Data with Events

Use stackedplot to visualize MDF data with events marked as vertical lines.

stackedplot(data{1})

Figure contains an object of type stackedplot.