Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Acquire Data Using NI Devices

This example shows how to acquire data from a National Instruments device available to MATLAB® from the command line using the Session based interface.

Discover Analog Input Devices

To discover a device that supports analog input subsystems, click the name of the device in the list in the Command window, or access the device in the array returned by daq.getDevices command. This example uses a NI 9201 device with ID 'cDAQ1Mod4'. This is a 8 channel analog input module and is in slot 4 of Chassis 'cDAQ1'.

devices = daq.getDevices
devices(4)
devices = 

Data acquisition devices:

index Vendor Device ID           Description           
----- ------ --------- --------------------------------
1     ni     cDAQ1Mod1 National Instruments NI 9205
2     ni     cDAQ1Mod2 National Instruments NI 9263
3     ni     cDAQ1Mod3 National Instruments NI 9234
4     ni     cDAQ1Mod4 National Instruments NI 9201
5     ni     cDAQ1Mod5 National Instruments NI 9402
6     ni     cDAQ1Mod6 National Instruments NI 9213
7     ni     cDAQ1Mod7 National Instruments NI 9219
8     ni     cDAQ1Mod8 National Instruments NI 9265
9     ni     Dev1      National Instruments PCIe-6363
10    ni     Dev2      National Instruments NI ELVIS II


ans = 

ni: National Instruments NI 9201 (Device ID: 'cDAQ1Mod4')
   Analog input subsystem 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 Session and Add an Analog Input Channel

Create a session, and use the addAnalogInputChannel function to add two analog input channels from this device to the session.

s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod4', 0, 'Voltage');
s.addAnalogInputChannel('cDAQ1Mod4', 1, 'Voltage');

Set Session Rate

By default the session is configured for 1000 scans/second. Change the scan rate to acquire at 8000 scans / second.

s.Rate = 8000
s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (8000 scans) at 8000 scans/second.
   Number of channels: 2
      index Type  Device   Channel MeasurementType      Range       Name
      ----- ---- --------- ------- --------------- ---------------- ----
      1     ai   cDAQ1Mod4 ai0     Voltage (Diff)  -10 to +10 Volts
      2     ai   cDAQ1Mod4 ai1     Voltage (Diff)  -10 to +10 Volts
   

Acquire a Single Scan

Use the inputSingleScan function to acquire a single scan. The result is an array of size M-by-1, where M corresponds to the number of input channels.

data = s.inputSingleScan
data =

   1.0e-03 *

    0.8147    0.9058

Start the Session in Foreground

You can acquire multiple scans using the startForeground function. This blocks MATLAB execution until all the data is acquired. The acquired data is returned in TIME-DATA pairs. TIME is a M-by-1 matrix, where M is the number of scans. DATA is a M-by-N matrix where M is the number of scans and N is the number of analog input channels in the session. We will now start a foreground operation.

[data,time] = s.startForeground;

Plot the Acquired Data

This session has now acquired 8000 scans in one second. Plot the acquired data for each channel.

plot(time,data);
xlabel('Time (secs)');
ylabel('Voltage')

Acquire data for a Specified Duration

By default the session runs for a duration of one second. Configure the session to run for a specified duration using the DurationInSeconds property.

Note, that Rate = DurationInSeconds/NumberOfScans. Changing the duration changes the number of scans in the session accordingly. The acquisition will now run for two seconds, acquiring 16,000 scans.

s.DurationInSeconds = 2;
s
[data,time] = s.startForeground;
plot(time,data);
xlabel('Time (secs)');
ylabel('Voltage')
s = 

Data acquisition session using National Instruments hardware:
   Will run for 2 seconds (16000 scans) at 8000 scans/second.
   Number of channels: 2
      index Type  Device   Channel MeasurementType      Range       Name
      ----- ---- --------- ------- --------------- ---------------- ----
      1     ai   cDAQ1Mod4 ai0     Voltage (Diff)  -10 to +10 Volts
      2     ai   cDAQ1Mod4 ai1     Voltage (Diff)  -10 to +10 Volts
   

Was this topic helpful?