Programming Previous page   Next Page

Specifying the Value of Callback Function Properties

You associate a callback function with a specific event by setting the value of the appropriate callback property. You can specify the callback function as a text string, cell array, or function handle. To access the object and event arguments, you must specify the function as a cell array or as a function handle. If your callback function accepts additional arguments, you must use a cell array.

The following table shows the syntax for several sample callback functions and describes how you call them.

Callback Function Syntax
How to Specify as a Property Value  
function myfile
set(h, 'StartFcn', 'myfile')
function myfile(obj, event)
set(h, 'StartFcn', @myfile)
function myfile(obj, event, arg1, arg2)
set(h, 'StartFcn', {'myfile', 5, 6})
function myfile(obj, event, arg1, arg2)
set(h, 'StartFcn', {@myfile, 5, 6})

This example illustrates several ways you can specify the value of timer object callback function properties, some with arguments and some without. To see the code of the callback function, my_callback_fcn, see Example: Writing a Callback Function.

  1. Create a timer object.
  2. Specify the value of the StartFcn callback. Note that the example specifies the value in a cell array because the callback function needs to access arguments passed to it.
  3. Specify the value of the StopFcn callback. The example specifies the callback function by its handle, rather than as a text string. Again, the value is specified in a cell array because the callback function needs to access the arguments passed to it.
  4. Specify the value of the TimerFcn callback. The example specifies the MATLAB commands in a text string.
  5. Start the timer object.
  1. The example outputs the following.

  1. Delete the timer object after you are finished with it.

Previous page  Creating Callback Functions Timer Object Execution Modes Next page

© 1994-2005 The MathWorks, Inc.