External Interfaces/API |
Creating and Executing Callback Functions
You can specify the callback function to be executed when a specific event type occurs by including the name of the M-file as the value for the associated callback property. You can specify the callback function as a function handle or as a string cell array element. Function handles are described in the function_handle
reference pages.
For example, to execute the callback function mycallback
every time the terminator is read from your device
Alternatively, you can specify the callback function as a cell array.
M-file callback functions require at least two input arguments. The first argument is the serial port object. The second argument is a variable that captures the event information given in Table 8-10, Event Information,. This event information pertains only to the event that caused the callback function to execute. The function header for mycallback
is shown below.
You pass additional parameters to the callback function by including both the callback function and the parameters as elements of a cell array. For example, to pass the MATLAB variable time
to mycallback
time = datestr(now,0);s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn = {@mycallback,time};
Alternatively, you can specify the callback function as a string in the cell array.
The corresponding function header is
If you pass additional parameters to the callback function, then they must be included in the function header after the two required arguments.
Storing Event Information | Enabling Callback Functions After They Error |