Products & Services Solutions Academia Support User Community Company

Learn more about Data Acquisition Toolbox   

Accessing Your Hardware

Connecting to Your Hardware

Perhaps the most effective way to get started with Data Acquisition Toolbox software is to connect to your hardware, and input or output data.

Each example illustrates a typical data acquisition session. The data acquisition session comprises all the steps you are likely to take when acquiring or outputting data using a supported hardware device. You should keep these steps in mind when constructing your own data acquisition applications.

Note that the analog input and analog output examples use a sound card, while the digital I/O example uses a National Instruments PCI-6024E board. If you are using a different supported hardware device, you should modify the adaptor name and the device ID supplied to the creation function as needed.

If you want detailed information about any functions that are used, refer to Functions — Alphabetical List. If you want detailed information about any properties that are used, refer to Base Properties — Alphabetical List.

Acquiring Data

If you have a sound card installed, you can run the following example, which acquires 1 second of data from two analog input hardware channels, and then plots the acquired data.

You should modify this example to suit your specific application needs. If you want detailed information about acquiring data, refer to Doing More with Analog Input.

  1. Create a device object — Create the analog input object ai for a sound card.

    ai = analoginput('winsound');
  2. Add channels — Add two hardware channels to ai.

    addchannel(ai,1:2);
  3. Configure property values — Configure the sampling rate to 44.1 kHz and collect 1 second of data (44,100 samples) for each channel.

    set(ai,'SampleRate',44100)
    set(ai,'SamplesPerTrigger',44100)
  4. Acquire data — Start the acquisition. When all the data is acquired, ai automatically stops executing.

    start(ai)
    wait(ai,2)
    data = getdata(ai);
    plot(data)
  5. Clean up — When you no longer need ai, you should remove it from memory and from the MATLAB workspace.

    delete(ai)
    clear ai

Outputting Data

If you have a sound card installed, you can run the following example, which outputs 1 second of data to two analog output hardware channels.

You should modify this example to suit your specific application needs. If you want detailed information about outputting data, refer to Analog Output.

  1. Create a device object — Create the analog output object ao for a sound card.

    ao = analogoutput('winsound');
  2. Add channels — Add two hardware channels to ao.

    addchannel(ao,1:2);
  3. Configure property values — Configure the sampling rate to 44.1 kHz for each channel.

    set(ao,'SampleRate',44100)
  4. Output data — Create 1 second of output data, and queue the data in the engine for eventual output to the analog output subsystem. You must queue one column of data for each hardware channel added.

    data = sin(linspace(0,2*pi*500,44100)');
    putdata(ao,[data data])

    Start the output. When all the data is output, ao automatically stops executing.

    start(ao)
  5. Clean up — When you no longer need ao, you should remove it from memory and from the MATLAB workspace.

    delete(ao)
    clear ao

Reading and Writing Digital Values

If you have a supported National Instruments board with at least eight digital I/O lines, you can run the following example, which outputs digital values, and then reads back those values.

You should modify this example to suit your specific application needs. If you want detailed information about reading and writing digital values, refer to Digital Input/Output.

  1. Create a device object — Create the digital I/O object dio for a National Instruments PCI-6024E board with hardware ID 1.

    dio = digitalio('nidaq','Dev1');
  2. Add lines — Add eight hardware lines to dio, and configure them for output.

    addline(dio,0:7,'out');
  3. Read and write values — Create an array of output values, and write the values to the digital I/O subsystem. Note that reading and writing digital I/O line values typically does not require that you configure specific property values.

    pval = [1 1 1 1 0 1 0 1];
    putvalue(dio,pval)
    gval = getvalue(dio);
  4. Clean up — When you no longer need dio, you should remove it from memory and from the MATLAB workspace.

    delete(dio)
    clear dio

Acquiring Data in a Loop

To make multiple acquisitions using a single analog input object, create a single object and execute the acquisition in a loop. Delete the object at the end of the loop.

% Create the object outside of the loop.
ai = analoginput('nidaq', 'Dev1');
addchannel(ai, 0);
% Execute acquisition.
for ii = 1:num_iterations
		start(ai);
    wait(ai, 2)
    data = getdata(ai);
    plot(data);  
end
% Delete the object out of the loop.
delete(ai)
clear ai

If you are creating the object within the loop, you must delete the object within the loop as well.

% Execute acquisition.
for ii = 1:num_iterations
    % Create the object within the loop.
		ai = analoginput('nidaq', 'Dev1');
    addchannel(ai, 0);
    start(ai);
    wait(ai, 2)
    data = getdata(ai);
    plot(data); 
    % Delete the object within the loop.
		delete(ai)
end
clear ai

For more information about cleaning up the MATLAB workspace, refer to Cleaning Up.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS