Why do I always need to run plot twice?

Every time I run a script that has a series of plot calls, it will always come out wrong the first time (only one line where there should be more, etc.) and I need to run the whole script again and then it works as expected. except because of this it is impossible to make a correct legend because despite the line not being visable it's still stored as a line in the plot and thus the legend lists it first.
The way I see it, I either need to fix how plots launch on my machine or have a way to always apply the right legend to the right plot.

7 件のコメント

millercommamatt
millercommamatt 2022 年 9 月 8 日
Are you creating your own figure objects? Are you using figure handles ,axes handles , etc...?
Torfi
Torfi 2022 年 9 月 8 日
No, just running hold on; plot() legend() xlabel() etc
Steven Lord
Steven Lord 2022 年 9 月 8 日
Can you show a small working example that illustrates the problem? I have some suspicions about what may be happening, but seeing a few lines of actual code we can run would help narrow down the list of potential root causes more quickly than us suggesting something, hearing back from you whether or not that worked, us suggesting something else, etc. If you show us code that feedback loop can be accelerated quite a bit because we can see whether our suggestions improved the code immediately.
Walter Roberson
Walter Roberson 2022 年 9 月 8 日
Do you have any third party toolboxes installed, such as SPM12 or dfield?
Torfi
Torfi 2022 年 9 月 8 日
No, this is a fresh install with nothing extra.
P.S. working on getting an example. This consistently happens on my working script but simpler examples seem to work as expected. May have to do with having lots of data in memory?
Steven Lord
Steven Lord 2022 年 9 月 8 日
My suspicion is that you're calling plot twice then turning hold on. In that case the first time this runs the second plot call would erase the first plot, but the second time you run it hold is already enabled so both the plots created before the hold would remain.
Torfi
Torfi 2022 年 9 月 8 日
I'm afraid not, I always call hold on; berfore any plot()s and call hold off; at the end.

サインインしてコメントする。

 採用された回答

Torfi
Torfi 2022 年 9 月 9 日

0 投票

I figured it out.
If you call figure() or subplot() inside hold on; Then this happens.

1 件のコメント

Walter Roberson
Walter Roberson 2022 年 9 月 9 日
hold only affects the current axis. figure() with no argument creates a new figure and so a new axis. subplot() with a combination of arguments that has not been used before creates a new axis.
However if you figure() passing in an existing figure number then no new axis will be created, and subplot with a combination of parameters that has been used before will not create a new axis. If you left hold on in an axis and then reuse the axis then you can run into issues... and if you had an existing axis and turn hold on first then you can run into issues

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 8 日

0 投票

When you call hold on before you do any plotting, then the xlim and ylim will default to 0 1 and will not change unless you call xlim or ylim. The limits are one of the things held.

カテゴリ

製品

リリース

R2022a

タグ

質問済み:

2022 年 9 月 8 日

コメント済み:

2022 年 9 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by