Main Content

Read a Large TDMS File into MATLAB

This example shows how to use a TDMS datastore to read data from a large TDMS file into MATLAB® for analysis.

The example TDMS file contains measurement data of a sine wave amplitude and phase. It also contains a channel group with sporadic events triggered at random times. The example file itself is not large, but is used to show the techniques for handling a large file.

You can use a TDMS datastore object to read a large TDMS file into MATLAB by iteratively reading available chunks of data. The size of the data in each iteration might depend on a time interval of interest, an absolute number of samples, or other group definition.

Define the Datastore and View Its Channel Information

Given the name of the TDMS file, use the tdmsDatastore function to create a TDMS datastore object for a specified read size and channel group.

fileName = "SineWaveWithEvents.tdms";
readSize = 1000;
ds = tdmsDatastore(fileName, ReadSize=readSize, SelectedChannelGroup="Measured Data");

Examine the data store channel list to identify the channel group and channel names of interest.

ds.ChannelList
ans=4×8 table
    ChannelGroupNumber    ChannelGroupName    ChannelGroupDescription       ChannelName       ChannelDescription    Unit     DataType      NumSamples
    __________________    ________________    _______________________    _________________    __________________    ____    ___________    __________

            1             "Measured Data"               ""               "Amplitude sweep"            ""             ""     "Double"         11000   
            1             "Measured Data"               ""               "Phase sweep"                ""             ""     "Double"         11000   
            2             "Events"                      ""               "Time"                       ""             ""     "Timestamp"          5   
            2             "Events"                      ""               "Description"                ""             ""     "String"             5   

Read Data into MATLAB

Loop through the file to read 1000 samples at a time, until the end of the file. The datastore read size and selected channel group were defined when the datastore ds was created above with the tdmsDatastore function. The read function is constrained by these parameters. Use the hasdata function if you do not know the number of iterations required.

while(hasdata(ds))
    data = read(ds);     
        % This loop overwrites the last iteration data, 
        % so any analysis of individual data or cumulative
        % reduction operations must happen inside the loop.
end

Visualize the Most Recent Chunk of Data

You can visualize the last 1000 samples of data using a stacked plot on the first channel group.

stackedplot(data{1});

Examine Event Data

Read and display the channel group containing the event data. This requires redefining the datastore object with tdmsDatastore function for that channel group. Assuming it is not a large set of data, you can use the readall function to retrieve it all at once.

ds = tdmsDatastore(fileName, SelectedChannelGroup="Events");
data = readall(ds);
data{1}
ans=5×2 table
                Time                  Description 
    _____________________________    _____________

    2022-01-12 22:21:36.058876037    "Failure 123"
    2022-01-12 22:21:38.558219909    "Failure 123"
    2022-01-12 22:21:40.058685302    "Failure 123"
    2022-01-12 22:21:41.558692932    "Event 4711" 
    2022-01-12 22:21:44.559547424    "Failure 123"