Data Acquisition Toolbox

Getting Started with Session-Based Interface Using NI Devices

This example shows how to get started with NI devices from the command line using the Session based interface.

Discover Available Devices

Use the daq.getDevices command to display a list of devices available to your machine and MATLAB®.

daq.getDevices
ans = 

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

Learn Device Details

To learn more about an individual device, 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.

d = daq.getDevices;
d(2)
ans = 

ni: National Instruments NI 9263 (Device ID: 'cDAQ1Mod2')
   Analog output subsystem supports:
      -10 to +10 Volts range
      Rates from 0.6 to 100000.0 scans/sec
      4 channels ('ao0','ao1','ao2','ao3')
      'Voltage' measurement type
   
This module is in slot 2 of the 'cDAQ-9178' chassis with the name 'cDAQ1'.


Acquire Data

To acquire data:

  1. Create a session describing the operation to perform

  2. Add input channels to the session

  3. Start the acquisition

The daq.createSession command creates a session. The session contains information describing the hardware, scan rate, duration, and other properties associated with the acquisition. Create a session, and assign it to a variable.

s = daq.createSession('ni')
s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   No channels have been added.
   

By default, the session is configured for 1000 scans per second and a duration of one second. For the purposes of this example, accept these defaults.

The addAnalogInputChannel command attaches an analog input channel to the session. You can add more than one channel to a session. For the purposes of this example, add just one.

s.addAnalogInputChannel('cDAQ1Mod1',0,'Voltage');
s
s = 

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

The startForeground command starts the acquisition and returns the results.

data = s.startForeground();

You've acquired 1000 scans. Plot the acquired data:

plot(data)

Change Default Properties of the Acquisition

By default, sessions run for one second at 1000 scans per second. To acquire at a higher rate for a longer time, change the Rate and DurationInSeconds properties.

s.Rate = 5000;
s.DurationInSeconds = 2;
s
s = 

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

Run the acquisition and plot the acquired data:

data = s.startForeground();
plot(data)