Why is "hold all" not working in this instance?
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi All,
I have attached a project I am working on at the moment. I want to hold any plots I create (currently two) at the end of a while loop, so that when the user choses to run the program again (and repeat the loop) the previous plots are held for comparison.
I have tried hold on, and hold all on line 396 where I specify what to do if the user wants to re-run the simulation and hold the plots.
I have also tried commenting out the clear variables command (and providing exclusions) at the start of the while loop, to no avail, thinking that clearvars was clearing the hold setting.
Leaving hold on on line 15 seems to have the desired effect, but gives an immediate blank plot on screen which I do not want on first run. I added a for loop to close all if the loop run-counter == 1 but this prevents new plots being plotted on the original axes with hold on again somehow...!
As this worked I even tried setting Hold = True at the bottom of the loop if the user wants to hold the plots, and setting 'hold on' if Hold = True at the start of the loop... as expected, didn't work.
This is driving me to insanity. Please, if you can, tell me what have I done wrong?!
I just want to hold all plots, not just the current one (which it seems to be doing unless I place the hold command in line 15) - I can't seem to find an answer to this, other than 'hold all' earlier, which i've since read achieves the same thing?
Thanks in advance,
Matt
댓글 수: 5
Stephen23
2016년 3월 14일
Honestly for code this complex there are two changes you should consider, which will help you to avoid this kind of problem:
- Write functions instead of scripts!
- Call every graphics command with explicit handles (e.g. as parent, or to clear or add lines, etc).
These changes make your code mode robust, and avoid exactly the kind of errors what you are experiencing now.
채택된 답변
Geoff Hayes
2016년 3월 14일
hold all
is equivalent to
hold on
and is to be removed in a future version of MATLAB. Did you think that calling this function would apply hold on to all figures?
Typically, when I use hold on, I do so once I have drawn something on my axes. In your case, I would remove the hold at line 15 and do instead
% FINAL LAP VELOCITY GRAPH
% =========================
Time_For_Lap_Plot = 1:(numel(Final_Velocity));
figure(1)
plot(Time_For_Lap_Plot, Final_Velocity(Time_For_Lap_Plot));
hold on; % <-------- Note the hold here after the axes has been updated
title('Lap Velocity vs. Time');
xlabel('Time (milliseconds)');
ylabel('Velocity (m/s)');
When I run with the above, the subsequent plot is drawn to the axes (the previous one is retained) and is coloured in a different colour (green) from the initial blue one.
For every figure where you wish to retain the previous plot, you will need to call hold on just like in the above.
댓글 수: 3
Geoff Hayes
2016년 3월 15일
편집: Geoff Hayes
2016년 3월 15일
Glad it worked out, Matt. I would consider renaming the variable Hold just to avoid confusion with the function hold. Actually, I can't remember how you obtain Hold and the comparison above is unclear. Is it a string or a bool? I think that you were using an question dialog to get the user's response to whether hold should be enabled or not. In that case, the response from the user would be a string and when comparing strings, it is good practice to use either the strcmp or strcmpi function.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!