| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Data Acquisition Toolbox |
| Contents | Index |
| Learn more about Data Acquisition Toolbox |
| On this page… |
|---|
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.
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.
Create a device object — Create the analog input object ai for a sound card.
ai = analoginput('winsound');Add channels — Add two hardware channels to ai.
addchannel(ai,1:2);
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)
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)
Clean up — When you no longer need ai, you should remove it from memory and from the MATLAB workspace.
delete(ai) clear ai
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.
Create a device object — Create the analog output object ao for a sound card.
ao = analogoutput('winsound');Add channels — Add two hardware channels to ao.
addchannel(ao,1:2);
Configure property values — Configure the sampling rate to 44.1 kHz for each channel.
set(ao,'SampleRate',44100)
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)
Clean up — When you no longer need ao, you should remove it from memory and from the MATLAB workspace.
delete(ao) clear ao
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.
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');Add lines — Add eight hardware lines to dio, and configure them for output.
addline(dio,0:7,'out');
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);
Clean up — When you no longer need dio, you should remove it from memory and from the MATLAB workspace.
delete(dio) clear dio
Note Digital line values are usually not transferred at a specific rate. Although some specialized boards support clocked I/O, the Data Acquisition Toolbox software does not support this functionality. |
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.
![]() | Toolbox Components | Understanding the Toolbox Capabilities | ![]() |

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 |