MATLAB Examples

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');

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.
vidobj.StopFcn = {'util_showframe', frameNumber};

% Initiate the acquisition.

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.

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

% Stop the acquisition.

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

clear vidobj

% Remove the callback function directory from the MATLAB path.