Why is "hold all" not working in this instance?

10 次查看(过去 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
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.
Matt
Matt 2016-3-15
Hi Stephen - sound advice, thank you very much. I will read into both!
I'm very new to Matlab and old habits from when I started out have stuck. While my ability has evolved, obviously my methods haven't evolved enough!
Thanks very much.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2016-3-14
Matt - according to hold, use of
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
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.
Matt
Matt 2016-3-15
I called it Hold_Plots for that reason when I coded it.
Thanks for the reminder on strcmp... I really should stop sticking to my ways and must try strcmp/strcmpi.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by