general troubleshooting advice requested: preventing plot overwrites in loop.

I obtain erratic output when using 'plot' function in for...end loops. Some scripts work perfectly but others using identical syntax and the same data inputs overwrite all but the last plot. Am using 'figure' to create new plot frames within the loop process. I have fixed some previous problems but am not sure how. I am using timeseries (timetable) financial data from multiple sources. There is never a system error message accompanying the malfunctioning programs. Any advice from expert users on possible source(s) of the problem would be greatly appreciated. It's a big time waster.

1 件のコメント

John D'Errico
John D'Errico 2021 年 6 月 27 日
How can we help you on a question this general? Your code is written with bugs in it. Apparently it does not do exactly what you think it does, and some times it does something strange. The answer? Don't do something strange. Write better code, that will be easier to debug. If you seriously want better help, then you need to show the code that does strange stuff.
My guess is, that code will be convoluted, lengthy spaghetti. So better yet? LEARN TO USE THE DEBUGGER. Use it, tracing through your code to find where that something strange happens.

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

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 6 月 27 日

1 投票

I obtain erratic output when using 'plot' function in for...end loops. Some scripts work perfectly but others using identical syntax and the same data inputs overwrite all but the last plot.
If the data is the same, then the code is more different than you think. If your plotting is happening inside a for loop, then there are a couple things to be aware of.
  • caling plot without using figure will replace the previous figure with the new one. This is one way to end up with just the last plot.
  • calling plot with figure will create a new figure for each loop. The last figure will contain the last plot, but you wlil also have a figure for all the other plots.
  • Use hold on when you want to use multiple plot commands to add to a plot (but remember to have a corresponding hold off).
Ch 9 of MATLAB Onramp covers plotting fundamentals, including using hold.
In order to address what is happening in your specific case you will need to share your code.

3 件のコメント

cris
cris 2021 年 6 月 28 日
Cris--thank you for the input. Yes, I am plotting within the loop and using figure with plot. Have also used hold on just to make sure Matlab (i.e., my code) is retrieving the correct data files and affixing the variable names (it is). The debug feature has found no errors. No spaghetti code, either.
...the code is more different than you think.
This is one of the simplest loops I use. In one case, the plot loop was lifted out of a larger "parent" script and copied into a new .mlx file. It uses the same data from the same Matlab folder. This is when one of the overwrites appeared. It also stopped last night. Appreciate the response.
Cris LaPierre
Cris LaPierre 2021 年 6 月 28 日
編集済み: Cris LaPierre 2021 年 6 月 29 日
Share your plotting code. Speaking in abstract is not adding any clarity.
cris
cris 2021 年 6 月 29 日
Problem resolved. It wasn't a coding issue. Thanks for follow up.

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2012a

質問済み:

2021 年 6 月 27 日

編集済み:

2021 年 6 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by