23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10 Scheduling Program Execution with Timers<br />

Note When you specify the callback commands directly as the value of the<br />

callback function property, the commands are evaluated in the <strong>MATLAB</strong><br />

workspace.<br />

Putting Commands in a Callback Function<br />

Instead of specifying <strong>MATLAB</strong> commands directly as the value of a callback<br />

property, you can put the commands in an M-file and specify the M-file as the<br />

value of the callback property.<br />

When you create a callback function, the first two arguments must be a<br />

handle to the timer object and an event structure. An event structure contains<br />

two fields: Type and Data. TheType field contains a text string that identifies<br />

the type of event that caused the callback. The value of this field can be any of<br />

the following strings: 'StartFcn', 'StopFcn', 'TimerFcn', or'ErrorFcn'.<br />

The Data field contains the time the event occurred.<br />

In addition to these two required input arguments, your callback function can<br />

accept application-specific arguments. To receive these input arguments, you<br />

must use a cell array when specifying the name of the function as the value<br />

of a callback property. For more information, see “Specifying the Value of<br />

Callback Function Properties” on page 10-17.<br />

Example: Writing a Callback Function<br />

This example implements a simple callback function that displays the type<br />

of event that triggered the callback and the time the callback occurred. To<br />

illustrate passing application-specific arguments, the example callback<br />

function accepts as an additional argument a text string and includes this text<br />

string in the display output. To see this function used with a callback property,<br />

see“SpecifyingtheValueofCallbackFunction Properties” on page 10-17.<br />

function my_callback_fcn(obj, event, string_arg)<br />

txt1 = ' event occurred at ';<br />

txt2 = string_arg;<br />

event_type = event.Type;<br />

event_time = datestr(event.Data.time);<br />

10-16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!