主要内容

Log Live Data to TDMS File

This example shows how to log data from an NI™ device to a TDMS file for analysis.

Consider an engine cooling system with two liquid coolants. Use thermocouples to measure the temperature of each coolant in degrees Celsius. Connect the thermocouples to an NI™ CompactDAQ device, which acquires the signals for logging and analysis.

Create and Configure DataAcquisition Object

Create a DataAcquisition object dq, configure the sample rate needed for logging. This example uses an NI 9219 module in a National Instruments® CompactDAQ Chassis NI cDAQ-9179.

sampleRate = 2;
dq = daq("ni");
dq.Rate = sampleRate;

Add input channels to the device with the measurement type as Thermocouple . Before acquiring input from these channels, set the channel property to determine the thermocouple type. For more information, see Thermocouple Measurement.

ch0 = addinput(dq,"cDAQ1Mod2","ai0","Thermocouple");
ch1 = addinput(dq,"cDAQ1Mod2","ai1","Thermocouple");

ch0.ThermocoupleType = "K";
ch1.ThermocoupleType = "K";

Acquire and Log Data

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(100));

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};

Visualize Data

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);

See Also

Functions

Topics