Hold on - best practice question

조회 수: 6 (최근 30일)
Pawel Jastrzebski
Pawel Jastrzebski 2017년 12월 20일
댓글: Jan 2017년 12월 21일
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
Adam
Adam 2017년 12월 20일
편집: Adam 2017년 12월 20일
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.
Stephen23
Stephen23 2017년 12월 21일
편집: Stephen23 2017년 12월 21일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 12월 20일
편집: Jan 2017년 12월 20일
Especially inside loops I do not like to call hold('on') repeatedly. Therefore I avoid this command in general, but set the property of the axes manually:
AxesH = axes('NextPlot', 'add');
This is what happens inside hold also. I admit that this looks a little bit "low level" and hold('on') might look easier. But this has the addition advantage, that a plot command does not skip formerly defined axes properties like limits or tickmarks.
And as code: Compare:
for k = 1:10
plot(1:10, rand(1, 10));
hold('on');
end
with:
AxesH = axes('NextPlot', 'add');
for k = 1:10
plot(AxesH, 1:10, rand(1, 10));
end
  댓글 수: 2
Pawel Jastrzebski
Pawel Jastrzebski 2017년 12월 21일
Having looked at the documentation a bit more, I discovered that there is a Figure property which is exactly the same as the one for axes:
  • ('NextPlot','add').
But I didn't work for me when I tried it:
f = figure('NextPlot','add')
b(1) = bar([1 2 3])
b(2) = bar([4 4]) % only this chart gets plotted
  • Do you know why there is the same property for Axis and Figure?
  • How are they different (application-wise) - this will probably explain why it didn't work in the example above?
Jan
Jan 2017년 12월 21일
Do you want to modify the figure or the axes? For the later, axes('NextPlot' 'add') is relevant, not figure('NextPlot', 'add'). As far as I understand the posted documentation, the property of figure controls, if a new figure is created for a new axes object, or if the existing figure is re-used.
See the newplot function also.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by