Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Configuring Callback Properties

This example explains how callback functions work and shows how to use them.

Callback functions are executed when an associated event occurs. To configure a callback to execute for a particular event, configure one of the video input object's callback properties:

  • ErrorFcn

  • FramesAcquiredFcn

  • StartFcn

  • StopFcn

  • TimerFcn

  • TriggerFcn

This tutorial uses a callback function, found in the

  • MATLABROOT\toolbox\imaq\imaqdemos\helper

directory, that displays the N'th frame, where N is provided as an input argument to the callback function.

% Add the callback function directory to the MATLAB® path.
utilpath = fullfile(matlabroot, 'toolbox', 'imaq', 'imaqdemos', 'helper');
addpath(utilpath);

Select a device to use for acquisition and configure it to acquire data upon executing a manual trigger.

% Access an image acquisition device.
vidobj = videoinput('winvideo', 1);

% Acquire an infinite number of frames when manually triggered.
triggerconfig(vidobj, 'manual');
vidobj.FramesPerTrigger = inf;

Configure the video input object to execute a callback function when the acquisition is stopped.

% Specify the N'th frame the callback function will display.
frameNumber = 3;

% Have the callback function executed when the acquisition ends.
set(vidobj, 'StopFcn', {'util_showframe', frameNumber});

% Initiate the acquisition.
start(vidobj)

Upon triggering the image acquisition device, a tennis ball is dropped within the camera's view.

% Trigger the object for logging and acquire data for a few seconds.
trigger(vidobj)
pause(5);

When the acquisition is stopped, it will cause the callback function to execute and display the N'th frame.

% Stop the acquisition.
stop(vidobj)

Once the video input object is no longer needed, delete it and clear it from the workspace.

delete(vidobj)
clear vidobj

% Remove the callback function directory from the MATLAB path.
rmpath(utilpath);
Was this topic helpful?