acquire data using ni devices -凯发k8网页登录
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 the daqlist
command. 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 the rate
property (the default is 1000 scans per second), and add analog input channels using addinput
.
dq = daq("ni"); dq.rate = 8000; addinput(dq, "cdaq1mod4", "ai0", "voltage"); addinput(dq, "cdaq1mod4", "ai1", "voltage");
acquire a single scan as a table
use read
to acquire a single scan. the result is a table with two data columns because two input channels are used to acquire the scan.
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
use read
to acquire a single scan. 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
use read
to 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.
% acquire data for one second at 8000 scans per second.
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)")