Main Content

Log Live Data to TDMS File

This example shows how to log data from an NI™ device to a TDMS file for analysis. You use two thermocouples to measure the temperature in degrees Celsius of two liquid coolants.

Create a DataAcquisition object dq, configure the sample rate needed for logging, and add channels to the device.

sampleRate = 2;
dq = daq("ni");
dq.Rate = sampleRate;
addinput(dq,"cDAQ1Mod6","ai0","Thermocouple");
addinput(dq,"cDAQ1Mod6","ai1","Thermocouple");

Set the LogToDisk property to enable logging. By default data is logged to the file recording.tdms in append mode, but you can customize this by setting the properties LogFileName and LogFileMode.

dq.LogToDisk = true;

fileName = "TemperatureRecording.tdms";
dq.LogFileName = fileName;

Read the data from the device into MATLAB® using the read function. The acquisition shown here runs in the foreground, but logging can also be performed in the background. Data is logged to the TDMS file as it is being acquired.

readData = read(dq,seconds(500));

Use the tdmsread function to read the logged data from the TDMS file into MATLAB for visual analysis.

loggedData = tdmsread(fileName, SampleRate=sampleRate);
loggedData = loggedData{1};

Plot the directly acquired data and the logged data, to confirm that the data from the device is the same as that logged in the TDMS file.

stackedplot(loggedData, readData, CombineMatchingNames=false);

Plots of logged data and read data