Data Acquisition Toolbox | ![]() ![]() |
Examples: Using Callback Properties and Functions
This section provides examples that show you how to create callback functions and configure callback properties.
Displaying Event Information with a Callback Function
This example illustrates how callback functions allow you to easily display event information. The example uses daqcallback
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.
AI
for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo
.
AI
.
daqcallback
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,'TriggerFcn',@daqcallback)
set(AI,'RuntimeErrorFcn',@daqcallback)
set(AI,'StopFcn',@daqcallback)
AI
and wait for it to stop running. The waittilstop
function blocks the MATLAB command line, and waits for AI
to stop running.
AI
, you should remove it from memory and from the MATLAB workspace.
Passing Additional Parameters to a Callback Function
This example illustrates how additional arguments are passed to the callback function. Timer events are generated every 0.5 second to display data using the local callback function daqdoc5_7plot
(not shown below).
You can run this example by typing daqdoc5_7
at the MATLAB command line.
AI
for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo
.
AI
.
daqdoc5_7plot
every 0.5 seconds. Note that the variables bsize
, P
, and T
are passed to the callback 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 callback function calls: ', num2str(0)]); xlabel('Samples'), ylabel('Signal (Volts)') grid on set(gcf,'doublebuffer','on') set(AI,'TimerFcn',{@daqdoc5_7plot,bsize,P,T})
AI
. The drawnow
command in daqdoc5_7plot
forces MATLAB to update the display. The waittilstop
function blocks the MATLAB command line, and waits for AI
to stop running.
AI
, you should remove it from memory and from the MATLAB workspace.
![]() | Creating and Executing Callback Functions | Linearly Scaling the Data: Engineering Units | ![]() |