| Data Acquisition Toolbox | Search  Help Desk |
Examples: Using Action Properties and Functions
Examples that show you how to create action functions and configure action-related properties are given below.Displaying Event Information with an Action Function
This example illustrates how the action function prototype allows you to easily display event information. The example shown below usesdaqaction to display information for trigger, runtime error, and stop events. The default SampleRate and SamplesPerTrigger values are used, which results in a one second acquisition for each trigger issued.
Initialization: Create the analog input object AI for a sound card. The available adaptors are found with daqhwinfo.
Configuration: Add one hardware channel toAI = analoginput('winsound');%AI = analoginput('nidaq', 1);
AI, repeat the trigger three times, find the time for the acquisition to complete, and define daqaction as the M-file to execute when a trigger, runtime error, or stop event occurs.
Execution: Startchan = addchannel(AI, 1);%chan = addchannel(AI, 0);set(AI, 'TriggerRepeat', 3);time = (AI.SamplesPerTrig/AI.SampleRate)*(AI.TriggerRep + 1);set(AI, 'TriggerAction', 'daqaction');set(AI, 'RuntimeErrorAction', 'daqaction');set(AI, 'StopAction', 'daqaction');
AI and wait for it to stop running. The pause command allows the output from daqaction to be displayed.
start(AI)
pause(time + 1)
Termination: Delete AI.
delete(AI)
The output is shown below.
Trigger event occurred at 10:17:57 Trigger event occurred at 10:17:58 Trigger event occurred at 10:17:59 Trigger event occurred at 10:18:00 Stop event occurred at 10:18:01
Specifying a Toolbox Function as an Action
You can specify thestart, stop, or trigger toolbox functions as actions. This example illustrates how you can specify a toolbox function as an action. As shown in the example code below, when an over-range condition occurs on the sound card channel, the acquisition is stopped.
AI = analoginput('winsound');set(AI, 'SampleRate', 8000);duration = 10; % Ten second durationchan = addchannel(AI, 1);ActualRate = get(AI, 'SampleRate');set(AI, 'SamplesPerTrigger', duration*ActualRate);set(AI, 'InputOverRangeAction', 'stop'); if strcmp(AI.Running, 'On') endstart(AI)
Passing Additional Parameters to an Action Function
This example illustrates how additional arguments are passed to the action function, and generates timer events to display data. The M-file to execute is calleddaqdoc4_8plot and this function is executed every 0.5 seconds.
Initialization: Create the analog input object AI for a sound card. The installed adaptors and hardware ID's are found with daqhwinfo.
Configuration: Add one hardware channel toAI = analoginput('winsound');%AI = analoginput('nidaq', 1);
AI, define a ten second acquisition, and execute the M-file daqug3_8plot every 0.5 seconds. Note that the variables size, P, and T are passed to the action function.
Execution: Startchan = addchannel(AI, 1);%chan = addchannel(AI, 0);set(AI, 'SampleRate', 8000);duration = 10; % Ten second durationActualRate = get(AI, 'SampleRate');set(AI, 'SamplesPerTrigger', duration*ActualRate);set(AI, 'TimerPeriod', 0.5); size = (AI.SampleRate)*(AI.TimerPeriod); figure P = plot(zeros(size,1)); T = title([sprintf('daqdoc4\\_8\nNumber of action functions calls: '), num2str(0)]); xlabel('Samples'); ylabel('Signal (Volts)') grid on set(gcf, 'doublebuffer', 'on'); set(AI, 'TimerAction', {'daqdoc4_8plot', size, P, T});
AI. The drawnow command in daqdoc4_8plot forces MATLAB to update the display.
start(AI)
pause(duration + 0.5)
Termination: Clear the action function from the workspace and delete AI.
clear daqdoc4_8plot delete(AI)
The function daqdoc4_8plot is given below.
function daqdoc4_8plot(obj, event, blocksize, plothandle,
titlehandle)
persistent index
if isempty(index)
index = 0;
end
index = index + 1;
if ~ishandle(titlehandle)
title([sprintf('daqdoc4\\_8\nNumber of action function
calls: '), num2str(index)])
data = getdata(obj, blocksize);
else
set(titlehandle, 'String',[sprintf('daqdoc4\\_8\nNumber of
action function calls: '), num2str(index)])
end
data = getdata(obj, blocksize);
size(data);
set(plothandle, 'ydata', data)
drawnow
Note:
stop command. You can verify this by removing the semicolon from the size(data) command and running daqdoc4_8.