Main Content

Get Started Reading a TDMS File

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

The example TDMS file contains measurement data of a sine wave amplitude and phase. The measurements are in two channels, in the same channel group.

Inspect the TDMS File Contents

Use the tdmsinfo function to obtain channel group and channel names in the TDMS file.

fileName = "SineWave.tdms";
info = tdmsinfo(fileName);
info.ChannelList
ans=2×8 table
    ChannelGroupNumber    ChannelGroupName    ChannelGroupDescription       ChannelName       ChannelDescription    Unit    DataType    NumSamples
    __________________    ________________    _______________________    _________________    __________________    ____    ________    __________

            1             "Measured Data"               ""               "Amplitude sweep"            ""             ""     "Double"       3500   
            1             "Measured Data"               ""               "Phase sweep"                ""             ""     "Double"       3500   

Read Data Properties from the TDMS File

Use the tdmsreadprop function to view data properties from the file.

tdmsreadprop(fileName)
ans=1×7 table
       name       description              datetime               author                title                datestring     timestring
    __________    ___________    _____________________________    _______    ___________________________    ____________    __________

    "SineWave"        ""         2022-01-13 10:03:31.000000000    "Admin"    "Amplitude And Phase Sweep"    "01/13/2022"    "10:03:31"

Specify a ChannelGroupName and ChannelName arguments to view properties of a specific channel.

group = "Measured Data";
channel = "Amplitude sweep";
tdmsreadprop(fileName, ChannelGroupName=group, ChannelName=channel)
ans=1×19 table
          name           description    unit_string     datatype      displaytype        monotony        NI_ChannelName    NI_ExpIsRelativeTime        NI_ExpStartTimeStamp                NI_ExpTimeStamp           NI_ExpXDimension       novaluekey       wf_increment    wf_samples    wf_start_offset            wf_start_time            wf_time_pref    wf_xname    wf_xunit_string
    _________________    ___________    ___________    ___________    ___________    ________________    ______________    ____________________    _____________________________    _____________________________    ________________    ________________    ____________    __________    _______________    _____________________________    ____________    ________    _______________

    "Amplitude sweep"        ""             ""         "DT_DOUBLE"     "Numeric"     "not calculated"        "Sine"                 1              2022-01-13 08:38:35.674852848    2022-01-13 08:38:35.674852848          "t"           "not calculated"       0.001           3500              0           1904-01-01 05:21:10.000000000     "relative"      "Time"           "s"      

Read Timetable Data from the TDMS File into MATLAB

To read data into a timetable, derive the start time and time step, typically contained in the channel properties.

timeStep = tdmsreadprop(fileName, ChannelGroupName=group, ChannelName=channel, PropertyNames="wf_increment")
timeStep=table
    wf_increment
    ____________

       0.001    

startTime = tdmsreadprop(fileName, ChannelGroupName=group, ChannelName=channel, PropertyNames="wf_start_time")
startTime=table
            wf_start_time        
    _____________________________

    1904-01-01 05:21:10.000000000

Using the start time and time step as arguments to the tdmsread function, read the data into MATLAB as a cell array of timetables. View some of the data from first channel group.

data = tdmsread(fileName, StartTime=startTime.wf_start_time, TimeStep=seconds(timeStep.wf_increment));
ttData = data{1};
head(ttData)
                Time                 Amplitude sweep    Phase sweep
    _____________________________    _______________    ___________

    1904-01-01 05:21:10.000000000           0                   0  
    1904-01-01 05:21:10.001000000           0            0.063418  
    1904-01-01 05:21:10.002000000           0             0.12658  
    1904-01-01 05:21:10.003000000           0             0.18923  
    1904-01-01 05:21:10.004000000           0             0.25112  
    1904-01-01 05:21:10.005000000           0               0.312  
    1904-01-01 05:21:10.006000000           0             0.37163  
    1904-01-01 05:21:10.007000000           0             0.42975  

Use a stacked plot to visualize the relationship between the data of different channels.

stackedplot(ttData);

Amplitude and Phase Sweep