Data Acquisition Toolbox | ![]() ![]() |
Examples: Using Action Properties and Functions
This section provides examples that show you how to create action functions and configure action properties.
Displaying Event Information with an Action Function
This example illustrates how action functions allow you to easily display event information. The example uses daqaction
to display information for trigger, run-time error, and stop events. The default SampleRate
and SamplesPerTrigger
values are used, which results in a 1-second acquisition for each trigger executed.
You can run this example by typing daqdoc5_6
at the MATLAB command line.
1. Create a device object - Create the analog input object AI
for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo
.
AI = analoginput('winsound');
%AI = analoginput('nidaq',1);
%AI = analoginput('cbi',1);
2. Add channels - Add one hardware channel to AI
.
chan = addchannel(AI,1); %chan = addchannel(AI,0); % For NI and CBI
3. Configure property values - 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, run-time error, or stop event occurs.
set(AI,'TriggerRepeat',3)
time = (AI.SamplesPerTrig/AI.SampleRate)*(AI.TriggerRepeat+1);set(AI,'TriggerAction','daqaction')
set(AI,'RuntimeErrorAction','daqaction')
set(AI,'StopAction','daqaction')
4. Acquire data - Start AI
and wait for it to stop running. The pause
command allows the output from daqaction
to be displayed.
start(AI)
pause(time+1)
5. Clean up - When you no longer need AI
, you should remove it from memory and from the MATLAB workspace.
delete(AI) clear AI
Passing Additional Parameters to an Action Function
This example illustrates how additional arguments are passed to the action function. Timer events are generated every 0.5 second to display data using the action function daqdoc5_7plot
.
You can run this example by typing daqdoc5_7
at the MATLAB command line.
1. Create a device object - Create the analog input object AI
for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo
.
AI = analoginput('winsound');
%AI = analoginput('nidaq',1);
%AI = analoginput('cbi',1);
2. Add channels - Add one hardware channel to AI
.
chan = addchannel(AI,1);
%chan = addchannel(AI,0);
% For NI and CBI
3. Configure property values - Define a 10-second acquisition and execute the M-file daqdoc5_7plot
every 0.5 seconds. Note that the variables bsize
, P
, and T
are passed to the action function.
duration = 10; % Ten second durationset(AI,'SampleRate',22050)
ActualRate = get(AI,'SampleRate');
set(AI,'SamplesPerTrigger',duration*ActualRate)
set(AI,'TimerPeriod',0.5) bsize = (AI.SampleRate)*(AI.TimerPeriod); figure P = plot(zeros(bsize,1)); T = title(['Number of action functions calls: ', num2str(0)]); xlabel('Samples'), ylabel('Signal (Volts)') grid on set(gcf,'doublebuffer','on') set(AI,'TimerAction',{'daqdoc5_7plot',bsize,P,T})
4. Acquire data - Start AI
. The drawnow
command in daqdoc5_7plot
forces MATLAB to update the display.
start(AI)
pause(duration+0.5)
5. Clean up - When you no longer need AI
, you should remove it from memory and from the MATLAB workspace.
delete(AI) clear AI
![]() | Creating and Executing Action Functions | Linearly Scaling the Data: Engineering Units | ![]() |