Main Content

Merge Multiple TDMS Files

This example shows how to merge multiple TDMS files by appending their data into a single TDMS file.

In this example you have vibration data of a rotating shaft recorded into a collection of TDMS files. The goal is to read each file and merge its data into an aggregate TDMS file for analysis. You can merge the data either by creating separate channel groups or by appending the data to a single channel group. This example shows both approaches.

fileMultipleChannelGroup = "RotatingShaftAnalysis_MultipleChannelGroup.tdms";
fileSingleChannelGroup = "RotatingShaftAnalysis_SingleChannelGroup.tdms";

Append Data by Creating New Channel Groups

Create a datastore with which you can iteratively read the collection of TDMS files in the folder RotatingShaftAnalysis, one file at a time.

ds = tdmsDatastore("RotatingShaftAnalysis", ReadSize="file");

You can write data to an existing TDMS file by adding new channel groups. By default, the tdmswrite function creates auto incrementing channel group names.

while(hasdata(ds))
    data = read(ds); %read one file.
    tdmswrite(fileMultipleChannelGroup, data);
end

On inspecting the contents of the written file you see there are 3 channel groups that map to 3 files in the folder.

info = tdmsinfo(fileMultipleChannelGroup);
info.ChannelList
ans=9×8 table
    ChannelGroupNumber    ChannelGroupName    ChannelGroupDescription    ChannelName    ChannelDescription    Unit    DataType    NumSamples
    __________________    ________________    _______________________    ___________    __________________    ____    ________    __________

            1             "ChannelGroup1"               ""               "Pulse"                ""             ""     "Double"      20000   
            1             "ChannelGroup1"               ""               "Sensor_X"             ""             ""     "Double"      20000   
            1             "ChannelGroup1"               ""               "Sensor_Y"             ""             ""     "Double"      20000   
            2             "ChannelGroup2"               ""               "Pulse"                ""             ""     "Double"      20000   
            2             "ChannelGroup2"               ""               "Sensor_X"             ""             ""     "Double"      20000   
            2             "ChannelGroup2"               ""               "Sensor_Y"             ""             ""     "Double"      20000   
            3             "ChannelGroup3"               ""               "Pulse"                ""             ""     "Double"      20000   
            3             "ChannelGroup3"               ""               "Sensor_X"             ""             ""     "Double"      20000   
            3             "ChannelGroup3"               ""               "Sensor_Y"             ""             ""     "Double"      20000   

Append Data to a Single Channel Group

You can append the data from all the TDMS files to a single channel group. By specifying a particular channel group name, the data is append to that channel group.

The channel names map to the table variable names, therefore the data is appended to all the channels in the channel group. If there is a table variable that does not already exist as channel, a new channel gets added to the channel group.

reset(ds);
while(hasdata(ds))
    tdmswrite(fileSingleChannelGroup, read(ds), ChannelGroupName="Dataset")
end

On inspecting the contents of the file, the NumSamples property of ChannelList increased to 3 × 20000, indicating that the data is appended to the file.

info = tdmsinfo(fileSingleChannelGroup);
info.ChannelList
ans=3×8 table
    ChannelGroupNumber    ChannelGroupName    ChannelGroupDescription    ChannelName    ChannelDescription    Unit    DataType    NumSamples
    __________________    ________________    _______________________    ___________    __________________    ____    ________    __________

            1                "Dataset"                  ""               "Pulse"                ""             ""     "Double"      60000   
            1                "Dataset"                  ""               "Sensor_X"             ""             ""     "Double"      60000   
            1                "Dataset"                  ""               "Sensor_Y"             ""             ""     "Double"      60000