Main Content

Write Metadata to TDMS-File

This example shows how to write file properties, channel group properties, and channel properties to a TDMS-file.

For this example, you have measurents of revolution and electrical current of a circular saw in a scenario where it stops on detecting contact with skin. You can write these measurement to a new TDMS-file, update the default properties, and add custom properties to the file, channel group, and channels.

Set Up Workspace

Read the data into the workspace and define the TDMS-file channel group names.

load("sawstopper.mat")
fileName = "sawstopper.tdms";
channelGroup = "SawData";
channelRev = "Revolutions (1/min)";
channelCurrent = "Current (A)";

Create the TDMS-file, and then read and view its data.

tdmswrite(fileName, circular_saw_data, ChannelGroupName=channelGroup, TimeChannel="none");
stackedplot(tdmsread(fileName, TimeStep=milliseconds(0.0028)))

Figure contains an object of type stackedplot.

Write File Properties

Observe the default file properties.

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

    "sawstopper.tdms"        ""         "Saw Stopper Data"      ""      2022-04-26 13:14:31.675516000

Update the title property using tdmswriteprop, and add a new custom timestamp property set with the current datetime.

tdmswriteprop(fileName, "title", "Saw Stopper Data");
tdmswriteprop(fileName, "timestamp", datetime("now"));
tdmsreadprop(fileName)
ans=1×5 table
          name           description          title           author              timestamp          
    _________________    ___________    __________________    ______    _____________________________

    "sawstopper.tdms"        ""         "Saw Stopper Data"      ""      2022-04-26 13:15:23.350898000

Write Channel Group Properties

Observe the default channel group properties.

tdmsreadprop(fileName, ChannelGroupName=channelGroup)
ans=1×3 table
      name               description                          scenario               
    _________    ___________________________    _____________________________________

    "SawData"    "Circular Saw Measurements"    "Stop on detecting contact with skin"

Update the channel group property description. Then add a custom property scenario to the channel group.

tdmswriteprop(fileName, "description", "Circular Saw Measurements", ChannelGroupName=channelGroup);
tdmswriteprop(fileName, "scenario", "Stop on detecting contact with skin",ChannelGroupName=channelGroup);
tdmsreadprop(fileName, ChannelGroupName=channelGroup)
ans=1×3 table
      name               description                          scenario               
    _________    ___________________________    _____________________________________

    "SawData"    "Circular Saw Measurements"    "Stop on detecting contact with skin"

Write Channel Properties

Similarly, you can update the channel properties such unit_string by specifying the channel group name and channel name.

tdmsreadprop(fileName, ChannelGroupName=channelGroup, ChannelName=channelRev)
ans=1×8 table
            name                          description                  unit_string            wf_start_time            wf_start_offset    wf_increment    wf_samples            formula         
    _____________________    ______________________________________    ___________    _____________________________    _______________    ____________    __________    ________________________

    "Revolutions (1/min)"    "Rotational speed of the circular saw"       "rpm"       2022-04-19 14:18:32.304446999           0             2.8e-06         23572       "rpm = frequency(Hz)/60"

tdmswriteprop(fileName, "unit_string", "rpm", ChannelGroupName=channelGroup, ChannelName=channelRev);
tdmswriteprop(fileName, "formula", "rpm = frequency(Hz)/60", ChannelGroupName=channelGroup, ChannelName=channelRev);
tdmswriteprop(fileName, "description", "Rotational speed of the circular saw", ChannelGroupName=channelGroup, ChannelName=channelRev);
tdmsreadprop(fileName, ChannelGroupName=channelGroup, ChannelName=channelRev)
ans=1×8 table
            name                          description                  unit_string            wf_start_time            wf_start_offset    wf_increment    wf_samples            formula         
    _____________________    ______________________________________    ___________    _____________________________    _______________    ____________    __________    ________________________

    "Revolutions (1/min)"    "Rotational speed of the circular saw"       "rpm"       2022-04-19 14:18:32.304446999           0             2.8e-06         23572       "rpm = frequency(Hz)/60"

You can also specify arrays of property names and values. If the property values have different data types, use a cell array.

tdmsreadprop(fileName, ChannelGroupName=channelGroup, ChannelName=channelCurrent)
ans=1×7 table
        name                  description             unit_string            wf_start_time            wf_start_offset    wf_increment    wf_samples
    _____________    _____________________________    ___________    _____________________________    _______________    ____________    __________

    "Current (A)"    "Current in the circular saw"        "A"        2022-04-19 14:18:32.304446999           0             2.8e-06         23572   

tdmswriteprop(fileName, ["description" "unit_string"], ["Current in the circular saw" "A"], ...
    ChannelGroupName=channelGroup, ChannelName=channelCurrent);
tdmsreadprop(fileName, ChannelGroupName=channelGroup, ChannelName=channelCurrent)
ans=1×7 table
        name                  description             unit_string            wf_start_time            wf_start_offset    wf_increment    wf_samples
    _____________    _____________________________    ___________    _____________________________    _______________    ____________    __________

    "Current (A)"    "Current in the circular saw"        "A"        2022-04-19 14:18:32.304446999           0             2.8e-06         23572   

Examine the property settings in the ChannelList display.

info = tdmsinfo(fileName);
info.ChannelList
ans=2×8 table
    ChannelGroupNumber    ChannelGroupName      ChannelGroupDescription           ChannelName                   ChannelDescription              Unit     DataType    NumSamples
    __________________    ________________    ___________________________    _____________________    ______________________________________    _____    ________    __________

            1                "SawData"        "Circular Saw Measurements"    "Revolutions (1/min)"    "Rotational speed of the circular saw"    "rpm"    "Double"      47144   
            1                "SawData"        "Circular Saw Measurements"    "Current (A)"            "Current in the circular saw"             "A"      "Double"      47144