Data Acquisition Toolbox
This example shows how to output data using an immediate and manual trigger. The various triggering properties are explored relative to each trigger type. The trigger properties used include: TriggerType and RepeatOutput.
See also ANALOGOUTPUT, ADDCHANNEL, DAQDEVICE/SET, DAQDEVICE/GET, PUTDATA, DAQDEVICE/START, STOP, PROPINFO, DAQHELP.
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)) stop(daqfind) end
An analog output trigger is defined as an event that initiates the output of data.
An analog output object can output data using an immediate trigger or a manual trigger. The trigger type is indicated by the TriggerType property. An immediate trigger is the default trigger type.
In this example, we are going to explore the properties related to triggering, and output data using an immediate and manual trigger.
An immediate trigger automatically occurs just after the START command is issued.
Let's create the analog output object ao and add two channels to it. This will allow the winsound device to run in stereo mode. The analog output object will be configured to output 8000 samples per second.
Note: The ao object is configured to output data immediately.
ao = analogoutput('winsound'); addchannel(ao, [1 2]); ao.SampleRate = 8000; ao.TriggerType = 'Immediate';
Now Let's define the data to output, which is nothing more than a sine wave with a frequency of 500 Hz.
y = sin(500*(0:(2*pi/8000):2*pi))';
Outputting data involves functions that queue the data in the data acquisition engine and functions that execute the analog output object.
For data to be output, you must first queue it in the data acquisition engine with the PUTDATA command.
data is an m-by-n matrix where m is the number of samples to be output and n is the number of output channels.
putdata(ao, [y y]);
As soon as the analog output object is started, the trigger will occur. When the trigger executes, the data queued in the data acquisition engine will be output to the sound card. 8000 samples are being output at 8000 samples per second.
It should take approximately one second to output the data. The output occurs in the background, while MATLAB® continues executing. The WAIT command is set to pause until either the data output is complete or the two second timeout expires (whichever occurs first). If the timeout expires, an error will be generated.
start(ao) tic wait(ao,2); toc
Elapsed time is 0.986394 seconds.
You can configure the analog output object to output the queued data repeatedly with the RepeatOutput property. If RepeatOutput is greater than 0, then all data queued before the START command is issued will be re-queued the specified number of times.
Let's configure the analog output object, ao, to output the queued data three times.
ao.RepeatOutput = 2; putdata(ao, [y y]);
The analog output object is started, then we wait up to 4 seconds for the output to complete. It should take about three seconds to output the data.
start(ao) tic wait(ao,4); toc
Elapsed time is 3.014145 seconds.
A manual trigger begins outputting data after you manually issue the TRIGGER command.
Let's configure the analog output object, ao, to output data at 8000 samples per second with one manual trigger.
ao.SampleRate = 8000; ao.TriggerType = 'manual'; ao.RepeatOutput = 0; putdata(ao, [y y]);
The analog output object is started with the START command.
The data acquisition engine will be running as soon as the START command is issued.
However, the data samples will not be output to the sound card until the TRIGGER command is issued. Therefore, the number of samples output from the data acquisition engine will be zero.
start(ao); ao.Running ao.SamplesOutput
ans = On ans = 0
Let's execute the manual trigger and the data will be output to the sound card, and then wait up to 2 seconds for the output to complete.
Finally, delete the analog output object.