Accessing National Instruments® M Series and NI-DAQmx Hardware

This example shows how to communicate with National Instruments M Series data acquisition devices as well as other National Instruments hardware that uses the NI-DAQmx software interface.

For a complete list of hardware supported by the toolbox, visit the Data Acquisition Toolbox homepage at our website

Note: This can only be run using the 32-bit version of MATLAB® and Data Acquisition Toolbox™. To learn more about using data acquisition devices on other platforms, see this example

First, find any running data acquisition objects and stop them. This stops all running data acquisition objects from interfering with this example. This code is usually not necessary outside this example unless there are multiple data acquisition objects running.

if (~isempty(daqfind))

Determine What National Instruments Hardware Is Installed

This example shows how to acquire data from a National Instruments device available to your system. To use NI devices, you must have the latest version of NI-DAQmx installed on your computer. Use the National Instruments Measurements & Automation Explorer to verify that the device is working properly before you start MATLAB®.

Display your installed hardware that can be accessed using the both the NI-DAQmx and Traditional NI-DAQ interfaces.

hw = daqhwinfo('nidaq')
hw = 

           AdaptorDllName: [1x73 char]
        AdaptorDllVersion: '3.4 (R2013b Prerelease)'
              AdaptorName: 'nidaq'
               BoardNames: {'PCI-6024E'  'USB-9263'}
        InstalledBoardIds: {'Dev1'  'Dev2'}
    ObjectConstructorName: {2x3 cell}

daqhwinfo returns information about the hardware that is installed, and the IDs that the National Instruments Measurement & Automation Explorer has assigned to these devices. For devices using the NI-DAQmx, these IDs are strings such as 'Dev6'. Traditional NI-DAQ devices appear as a numeric value, such as '1'.

List the IDs and names of the boards that are installed

ans = 

    'Dev1'    'Dev2'

ans = 

    'PCI-6024E'    'USB-9263'

Notice that the 'PCI-4472' board appears in the list twice. Like all E-series and S-series devices, this device is available through both the new NI-DAQmx interface and the Traditional NI-DAQ interface. See the National Instruments section of the Troubleshooting chapter of the Data Acquisition Toolbox documentation for more details about choosing between NI-DAQmx and Traditional NI-DAQ.

Set Up an Acquisition

Create an analog input object associated with one of the devices found. This object represents the hardware configuration. The ID you supply is same as the one returned in the InstalledBoardIds field by daqhwinfo.

% Create an analog input object
ai = analoginput('nidaq','Dev6');
Warning: The device adaptor is not in the expected location.
Expected: C:\work\Atm_030513_MCC\matlab
An adaptor from the wrong release may be in use. To correct this problem
Then exit and restart MATLAB. 
Error using analoginput (line 142)
The specified device ID is invalid. Use DAQHWINFO(adaptorname) to determine 
valid device IDs.

Error in demodaq_nidaqmx (line 64)
ai = analoginput('nidaq','Dev6');

Use addchannel to specify what channels to use in an acquisition.

% Data will be acquired from hardware channels 1 and 3
addchannel(ai, 1);
addchannel(ai, 3);

Review the basic configuration of the acquisition by typing the name of the variable.


Discover and Configure the Properties of the Acquisition

To determine the list of configurable properties, use the set function. See the Data Acquisition Toolbox documentation for complete information about the various configurable properties.


For a complete listing of all object properties, with their current settings, use the get function.


National Instruments devices allow the analog input lines to be configured as single ended or differential. For more information on input types, consult the documentation for the hardware. To discover the options available for a specific property, use the set function with the property name:

% Configure the analog input for differential mode
ai.InputType = 'Differential';

To learn more about a specific property, use daqhelp:


Set the Acquisition Rate and Number of Samples to Acquire

Configure the analog input object to acquire more samples at a greater rate than the default.

  • Data will be acquired at a rate of 10,000 samples per second

  • 10,000 samples will be acquired

ai.SampleRate = 10000;
ai.SamplesPerTrigger = 10000;

Acquire the Data

Review the basic configuration of the acquisition.


Start the analog input object.


The toolbox acquires data in the background while MATLAB continues executing. Your code is free to execute other functions while the acquisition is taking place. Data is stored in the toolbox until you bring it into MATLAB.

In order to pause MATLAB, use wait to allow the acquisition to complete before attempting to retrieve the data. If the acquisition completes before the wait timeout expires, MATLAB will continue.

wait(ai,2);  %wait up to 2 seconds for the acquisition to complete.

Now that the acquisition is complete, transfer the data from the toolbox into MATLAB using the getdata function, store it in the workspace variable data, and plot it using the plot function.

data = getdata(ai);

Clean Up

This completes the introduction to analog input objects. Because the analog input object is no longer needed, you should:

  • Stop the analog input object from running using the stop function.

  • Delete it with the delete function to free memory and other system resources.

Was this topic helpful?