To illustrate how to use callbacks, this section presents a simple example that creates an image acquisition object and associates a callback function with the start event, trigger event, and stop event. For information about all the event callbacks supported by the toolbox, see Event Types.
The example uses the default callback function provided with the toolbox, imaqcallback. The default callback function displays the name of the object along with information about the type of event that occurred and when it occurred. To learn how to create your own callback functions, see Creating and Executing Callback Functions.
This example illustrates how to use the default callback function.
Create an image acquisition object — This example creates a video input object for a Matrox® image acquisition device. To run this example on your system, use the imaqhwinfo function to get the object constructor for your image acquisition device and substitute that syntax for the following code.
vid = videoinput('matrox',1);
set(vid,'StartFcn',@imaqcallback) set(vid,'TriggerFcn',@imaqcallback) set(vid,'StopFcn',@imaqcallback)
For this example, specify the amount of data to log.
Start the image acquisition object — Start the image acquisition object. The object executes an immediate trigger, acquires 100 frames of data, and then stops. With the three callback functions enabled, the object outputs information about each event as it occurs.
start(vid) Start event occurred at 14:38:46 for video input object: M_RS170-matrox-1. Trigger event occurred at 14:38:46 for video input object: M_RS170-matrox-1. Stop event occurred at 14:38:49 for video input object: M_RS170-matrox-1.
delete(vid) clear vid