Saving Handles in M-Files
About Saving Handles
Graphics M-files frequently use handles to access property values
and to direct graphics output to a particular target. MATLAB software
provides utility routines that return the handles to key objects (such
as the current figure and axes). In M-files, however, these utilities
might not be the best way to obtain handles because:
Querying MATLAB for the handle of an object or
other information is less efficient than storing the handle in a variable
and referencing that variable.
The current figure, axes, or object might change during
M-file execution because of user interaction.
Back to Top
Save Information First
It is good practice to save relevant information about the MATLAB state
in the beginning of your M-file. For example, you can begin an M-file
with
cax = newplot;
cfig = get(cax,'Parent');
hold_state = ishold;
rather than querying this information each time you need it.
Remember that utility commands like ishold obtain
the values they return whenever called. (The ishold command
issues a number of get commands and string compares
(strcmp) to determine the hold state.)
If you are temporarily going to alter the hold state within
the M-file, save the current values of the NextPlot properties
so you can reset them later:
ax_nextplot = lower(get(cax,'NextPlot'));
fig_nextplot = lower(get(cfig,'NextPlot'));
.
.
.
set(cax,'NextPlot',ax_nextplot)
set(cfig,'NextPlot',fig_nextplot)
Back to Top
 | The Figure Close Request Function | | Properties Changed by Built-In Functions |  |
Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
Get the Interactive Kit