| Graphics | ![]() |
Testing for Hold State
There are situations in which your M-file should change the visual appearance of the axes to accommodate new graphics objects. For example, if you want the M-file my_plot from the previous example to accept 3-D data, it makes sense to set the view to 3-D when the input data has z-coordinates.
However, to be consistent with the behavior of the MATLAB high-level routines, it is good practice to test whether hold is on before changing parent axes or figure properties. When hold is on, the axes and figure NextPlot properties are both set to add.
The M-file my_plot3 accepts 3-D data and also checks the hold state, using ishold, to determine whether it should change the view.
function my_plot3(x,y,z) cax = newplot; hold_state = ishold;% ishold tests the current hold stateLSO = ['-';'--';': ';'-.']; if nargin == 2 hlines = line(x,y,'Color','k'); if~hold_state% Change view only if hold is offview(2) end elseif nargin == 3 hlines = line(x,y,z,'Color','k'); if~hold_state% Change view only if hold is offview(3) end end ls = 1; for hindex = 1:length(hlines) if ls > length(LSO),ls = 1;end set(hlines(hindex),'LineStyle',LSO(ls,:)) ls = ls + 1; end
If hold is on when you call my_plot3, it does not change the view. If hold is off, my_plot3 sets the view to 2-D or 3-D, depending on whether there are two or three input arguments.
| Example -- Using newplot | Protecting Figures and Axes | ![]() |
© 1994-2005 The MathWorks, Inc.