
84 Accelerating MATLAB
®
Performance
• Use the persistent command. persistent enables access to the data within the
same function in which data is declared as persistent. persistent variables are
initialized to [] (the empty array), making it easy to differentiate between pre-
cached and post-cached data. Here is a simple usage example:
function myCallbackFunc(hObject,eventData)
persistent data
if isempty(data)
data = loadData(); % cache the data for later reuse
end
% …now do something useful with the data…
end % myCallbackFunc
Clearing such caches can easily be done, even from outside the function:
clear myCallbackFunc % clear all persistent vars ...