Main Content

Acquire Data Using NI Devices

This example shows how to acquire data from a National Instruments device.

Discover Analog Input Devices

To discover a device that supports input measurements, access the device in the table returned by thedaqlistcommand. This example uses an NI 9201 module in a National Instruments® CompactDAQ Chassis NI cDAQ-9178. This is an 8 channel analog input device and is module 4 in the chassis.

d = daqlist("ni")
d = 12×4 table DeviceID Description Model DeviceInfo ___________ __________________________________ _____________ ____________________ "cDAQ1Mod1" "National Instruments NI 9205" "NI 9205" [1×1 daq.DeviceInfo] "cDAQ1Mod2" "National Instruments NI 9263" "NI 9263" [1×1 daq.DeviceInfo] "cDAQ1Mod3" "National Instruments NI 9234" "NI 9234" [1×1 daq.DeviceInfo] "cDAQ1Mod4" "National Instruments NI 9201" "NI 9201" [1×1 daq.DeviceInfo] "cDAQ1Mod5" "National Instruments NI 9402" "NI 9402" [1×1 daq.DeviceInfo] "cDAQ1Mod6" "National Instruments NI 9213" "NI 9213" [1×1 daq.DeviceInfo] "cDAQ1Mod7" "National Instruments NI 9219" "NI 9219" [1×1 daq.DeviceInfo] "cDAQ1Mod8" "National Instruments NI 9265" "NI 9265" [1×1 daq.DeviceInfo] "Dev1" "National Instruments PCIe-6363" "PCIe-6363" [1×1 daq.DeviceInfo] "Dev2" "National Instruments NI ELVIS II" "NI ELVIS II" [1×1 daq.DeviceInfo] "Dev3" "National Instruments PCIe-6363" "PCIe-6363" [1×1 daq.DeviceInfo] "Dev4" "National Instruments PCIe-6363" "PCIe-6363" [1×1 daq.DeviceInfo]
deviceInfo = d{4,"DeviceInfo"}
deviceInfo = ni: National Instruments NI 9201 (Device ID: 'cDAQ1Mod4') Analog input supports: -10 to +10 Volts range Rates from 0.6 to 500000.0 scans/sec 8 channels ('ai0' - 'ai7') 'Voltage' measurement type This module is in slot 4 of the 'cDAQ-9178' chassis with the name 'cDAQ1'.

Create a DataAcquisition and Add Analog Input Channels

Create a DataAcquisition, set theRateproperty (the default is 1000 scans per second), and add analog input channels usingaddinput.

dq = daq("ni"); dq.Rate = 8000; addinput(dq,"cDAQ1Mod4","ai0","Voltage"); addinput(dq,"cDAQ1Mod4","ai1","Voltage");

Acquire a Single Scan as a Table

Useread进行一次扫描。结果是一个具有两个数据列的表,因为使用两个输入通道来获取扫描。

tabledata = read(dq)
tabledata = 1×2 timetable Time cDAQ1Mod4_ai0 cDAQ1Mod4_ai1 _____ _____________ _____________ 0 sec 0.00081472 0.00090579

Acquire a Single Scan as a Matrix

Useread进行一次扫描。The result is an array of size 1x2 because two input channels are used to acquire the scan.

matrixdata = read(dq,"OutputFormat","Matrix")
matrixdata = 1.0e-03 * 0.1270 0.9134

Acquire Data For a Specified Duration

Usereadto acquire multiple scans, blocking MATLAB execution until all the data requested is acquired. The acquired data is returned as a timetable with width equal to the number of channels and height equal to the number of scans.

%以每秒8000扫描获取一秒钟的数据。data = read(dq, seconds(1));

Plot the Acquired Data

plot(data.Time, data.Variables); ylabel("Voltage (V)")

Acquire Specified Number of Scans

data = read(dq, 2*dq.Rate); plot(data.Time, data.Variables); ylabel("Voltage (V)")