Hold on - best practice question
Afficher commentaires plus anciens
I wonder if there is any real difference between two following versions of 'hold on' application:
- Case 1 - 'hold on' is used directly after the 'figure' declaration
- Case 2 - 'hold on' is used after the first plot
% DATA
x = 1:10;
y1 = x.^2;
y2 = x.^3;
% PLOTS
% Case 1 - 'hold on' after 'figure' declaration
figure
hold on;
plot(x, y1);
plot(x, y2);
% Case 2 - 'hold on' after 1st plot
figure
plot(x, y1);
hold on;
plot(x, y2);
- Is there any performance gain/loss between these two cases?
- Or it is entirely down to the preference of the person writing the code?
I'm asking as I noticed that many people use 'Case 1' style, which I personally find more elegant. but on the other hand, all the examples featured in the Matlab documentation use the 'Case 2' style.
To add a bit more into the mix, I came across this thread, where Jan Simon abstains from using 'hold on' function altogether in favour of using handles:
So, what is the 'best practice' in this regard?
3 commentaires
Rik
le 20 Déc 2017
I see hold and cla as being halfway between a hack and a patch job. Sometimes it is just not worth it to invest the extra time to explicitly program the parent axes and delete plots when you don't need them anymore.
I didn't even know you could call hold before creating an axis, so I wouldn't even have used case 1.
Putting hold on before you have plotted anything has often led to unwanted results (though I can't remember the circumstances or whether it was just older versions of Matlab) when I have done it so I would never do that personally.
Also I always use the axes handle explicitly as with any plotting instruction - i.e.
hold( hAxes, 'on' )
where hAxes is your axes handle.
@Pawel Jastrzebski: if you are really interested in "best practice", then the most significant change you could make to your code is to obtain and use all graphics handles explicitly. This means getting the figure, axes, line, patch, image, etc. handles and always supplying them (e.g. as parent property) when calling all graphics functions.
When you get to the point of asking about hold best-practice, then you are at a good point to move away from the unreliable practice of assuming that the current figure/axes/... are always the correct ones to be accessing.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Graphics Object Properties dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
