Main Content

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 that displays the N'th frame, where N is provided as an input argument to the callback function.

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.
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