| Data Acquisition Toolbox | Search  Help Desk |
Examples: Using Action Properties and Functions
Examples showing how to create action functions and configure action-related properties are given below for a data-output action and a stop action.Specifying an Action Function for an Event
This example illustrates how you can generate data-output events. As shown below, the M-filedaqdoc5_4disp is executed every time the specified number of samples are output.
Initialization: Create the analog output object AO for a sound card. The installed adaptors and hardware ID's are found with daqhwinfo.
Configuration: Add two channels toAO = analogoutput('winsound');%AO = analogoutput('nidaq', 1);
AO, set the trigger to repeat four times, specify daqug4_4disp as the M-file to execute when 8000 samples are output, generate data to be queued, and queue the data with one call to putdata.
Execution: Display summary information, startchans = addchannel(AO, 1:2);%chans = addchannel(AO, 0:1);set(AO, 'SampleRate', 8000);ActualRate = get(AO, 'SampleRate');set(AO, 'RepeatOutput', 4); set(AO, 'SamplesOutputActionCount', 8000); freq = get(AO, 'SamplesOutputActionCount'); set(AO, 'SamplesOutputAction', 'daqdoc5_4disp'); data = sin(linspace(0, 2*pi, 3*freq))'; time = (length(data)/AO.SampleRate)*(AO.RepeatOutput + 1);putdata(AO, [data data]);
AO, and pause to update command line display from daqdoc5_4disp.
Termination: DeleteAOstart(AO);pause(0.1)
AO.
delete(AO)
The daqdoc5_4disp function is given below.
function daqdoc5_4disp(obj, event)
samp = obj.SamplesOutput;
disp([num2str(samp), ' samples have been output'])
Displaying Event Information with an Action Function
This example illustrates how the action function prototype allows you to easily display event information. The code below outputs five seconds of queued data to one sound card channel.
Initialization: Create the analog output object AO for a sound card. The installed adaptors and hardware ID's are found with daqhwinfo.
Configuration: Add one channel toAO = analogoutput('winsound');%AO = analogoutput('nidaq', 1);
AO, specify daqdoc5_5disp as the M-file to execute when the start, trigger, and stop events occur, generate data to be queued, and queue the data with one call to putdata.
Termination: deleteset(AO, 'SampleRate', 8000);chan = addchannel(AO, 1);%chan = addchannel(AO, 0);ActualRate = get(AO, 'SampleRate');set(AO, 'StartAction', 'daqdoc5_5disp'); set(AO, 'TriggerAction', 'daqdoc5_5disp'); set(AO, 'StopAction', 'daqdoc5_5disp'); data = sin(linspace(0, 2*pi, ActualRate)); data = [data data data]; time = (length(data)/AO.SampleRate)putdata(AO, data');Execution: Display summary information, startAO, and pause to update command line display from daqdoc5_5disp.
AO.
delete(AO)
The daqdoc5_5disp function is shown below.
function daqdoc5_5disp(obj, event)
EventType = event.Type;
EventData = event.Data;
EventDataTime = EventData.TimeStamp;
EventDataSample = EventData.SampleStamp;
disp([EventType,' event occurred at ',datestr(EventDataTime,13)])