Main Content

Get Started Writing a TDMS File

This example shows how to write data from MATLAB® to a TDMS file.

For this example, the file weather.mat contains a regional weather report from 3 December 1998 to 30 Nov 2000.

Set Up the Workspace

Load the data to be written to a TDMS file, and define the TDMS file, channel group, and channel names. Later you can add some custom attributes, such as title and units, to the TDMS file.

load("weather.mat");
fileName = "weather.tdms";
group = "ChannelGroup1";
channel = "T_min";
whos
  Name            Size             Bytes  Class     Attributes

  channel         1x1                150  string              
  fileName        1x1                166  string              
  group           1x1                166  string              
  weather       729x19            116009  table               

The variable weather is a table that holds the data.

Write the Table of Data to a New TDMS File

Use the tdmswrite function to write the table of weather data to a TDMS file from MATLAB.

tdmswrite(fileName, weather)

Inspect the contents of the file using tdmsinfo.

info = tdmsinfo(fileName)
info = 
  TdmsInfo with properties:

           Path: "C:\Users\rkoshy\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\rkoshy.ExampleManager\daq-ex72949341\weather.tdms"
           Name: "weather.tdms"
    Description: ""
          Title: "Weather Report"
         Author: ""
        Version: "2.0"
    ChannelList: [38×8 table]

View channel groups and channels in the TDMS file.

info.ChannelList
ans=38×8 table
    ChannelGroupNumber    ChannelGroupName    ChannelGroupDescription          ChannelName           ChannelDescription    Unit     DataType      NumSamples
    __________________    ________________    _______________________    ________________________    __________________    ____    ___________    __________

            1             "ChannelGroup1"               ""               "Date"                              ""            ""      "Timestamp"       729    
            1             "ChannelGroup1"               ""               "T_min"                             ""            "°C"    "Double"          729    
            1             "ChannelGroup1"               ""               "T_max"                             ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "Precipitation"                     ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "T_6h"                              ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "Index"                             ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "T_min_Lin"                         ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "T_max_Lin"                         ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "T_mittel_Lin"                      ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "AvergeMinimumTemp"                 ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "AverageMaximumTemp"                ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "AverageTemp"                       ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "RealTemperatureDiff"               ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "AverageTemperatureDiff"            ""            ""      "Double"          729    
            1             "ChannelGroup1"               ""               "Month"                             ""            ""      "Timestamp"       729    
            1             "ChannelGroup1"               ""               "Tm_min"                            ""            ""      "Double"          729    
      ⋮

Use tdmsread to examine the data written into the new file.

rData = tdmsread(fileName);
stackedplot(rData)

Inspect file default properties using tdmsreadprop.

tdmsreadprop(fileName)
ans=1×5 table
         name         description         title          author              timestamp          
    ______________    ___________    ________________    ______    _____________________________

    "weather.tdms"        ""         "Weather Report"      ""      2022-04-21 19:25:30.357063999

Inspect channel group default properties using tdmsreadprop.

tdmsreadprop(fileName, ChannelGroupName=group) 
ans=1×2 table
         name          description
    _______________    ___________

    "ChannelGroup1"        ""     

Inspect channel default properties using tdmsreadprop.

tdmsreadprop(fileName, ChannelGroupName=group, ChannelName=channel)
ans=1×3 table
     name      description    unit_string
    _______    ___________    ___________

    "T_min"        ""            "°C"    

Modify the TDMS File Metadata

To update the file properties, channel group properties, or channel properties of an existing TDMS file, use tdmswriteprop.

You can inspect the updated properties using tdmsreadprop.

Update the file property Title.

tdmswriteprop(fileName, "title", "Weather Report")
tdmsreadprop(fileName)
ans=1×5 table
         name         description         title          author              timestamp          
    ______________    ___________    ________________    ______    _____________________________

    "weather.tdms"        ""         "Weather Report"      ""      2022-04-21 19:25:30.357063999

Also add a custom file property called timestamp, and set its value to the current date and time.

tdmswriteprop(fileName, "timestamp", datetime("now"))
tdmsreadprop(fileName)
ans=1×5 table
         name         description         title          author              timestamp          
    ______________    ___________    ________________    ______    _____________________________

    "weather.tdms"        ""         "Weather Report"      ""      2022-04-21 19:34:37.310101999

Finally, update the units of a channel, which is specified by the default property unit_string.

tdmswriteprop(fileName, "unit_string", "°C", ChannelGroupName=group, ChannelName=channel)
tdmsreadprop(fileName, ChannelGroupName=group, ChannelName=channel)
ans=1×3 table
     name      description    unit_string
    _______    ___________    ___________

    "T_min"        ""            "°C"