Newsletters

  • Contact sales

Tips and Tricks - Simplifying Measurement and Timer Callbacks with Nested Functions - New Online Support Features

By Michelle Hirsch, MathWorks

When building an automated application using Data Acquisition, Instrument Control, and Image Acquisition Toolboxes or the timer and serial port objects in MATLAB, the best approach is to write callback functions for any code that should execute continuously. As these objects run independently of the main MATLAB execution thread, you gain the best performance and keep MATLAB available for other tasks.

Writing callback functions for objects like these introduces the challenge of sharing data across multiple functions. Nested functions, introduced in MATLAB 7, can make your code easier to write, read, and maintain. They can also make code more efficient by letting you share data among functions without making additional copies.

The code shown here illustrates how nested functions can be used to program object callbacks. Using a timer object, this code displays an image that gets brighter every second for 10 seconds. At every timer event (TimerFcn), the image is scaled and redisplayed. This task requires sharing the image data between the main function and the callback function. Without nested functions, it is typically accomplished by storing the data in the UserData field of the timer object. This approach can affect the performance of large arrays and result in nonintuitive code. With nested functions, the data is automatically shared by each function.

Notice how simple MyTimerFcn is when written as a nested function. Performance enthusiasts will be pleased to learn that there is no need to create extra copies of Im to share it between functions. The code for this example is available for download on the File Exchange at MATLAB Central.

function nested_callback_ex
% Create and configure timer object
t = timer('ExecutionMode','fixedRate', ... % Run continuously
'TasksToExecute',10, ... % Runs 10 times
'TimerFcn',@MyTimerFcn); % Run MyTimerFcn ateach timer event
% Load and display a grayscale image
Im = imread('street1.jpg');imagesc(Im);
% Start the timer
start(t)
function MyTimerFcn(obj,event)
% Scale and display image
Im = Im*1.1; % Make brighter
imagesc(Im) % Display updated image
end
end % function nested_callback_ex

New Online Support Features

Track your Support Requests

The new online support-request tracking system lets you submit and track support requests, communicate with MathWorks support staff, attach files and documents, and receive e-mail updates on the status of your request.

Access Bug Reports

The Bug Report system provides a regularly updated list of existing and recently corrected issues in MathWorks software, starting with Release 14 Service Pack 2. Created in response to customer requests, the report offers a streamlined process for reviewing known bugs in all products and for reporting new bugs.

To access your support requests or view bug reports you must log in using your MathWorks Account name and password. Creating a MathWorks Account is free and does not require a MathWorks product license. To create an account, visit www.mathworks.com/res/account.

Published 2006

Receive the latest MATLAB and Simulink technical articles.

Related Resources

Latest Blogs