Figure identity Question with Loops
6 次查看(过去 30 天)
显示 更早的评论
Hi, I am creating a figure on each pass in a loop.
On this figure I do several plots (20 of them) but always plot to figure(1) and so don't want to go and replace my code. So obviously all the plots gets overwritten each time the loop is executed
Is it possible to add at the end of the loop a way to change the current figure so that on the next loop, the current one will be figure(1)?
Thanks Jason
2 个评论
Adam
2017-7-17
It isn't obvious what the problem is with replacing code rather than doing some coding gymnastics to try to force figure(1) to be something different each time. Why can't you just use figure(n) to plot to?
Stephen23
2017-7-17
@Jason: often in these kind of situations it turns out to be better to just go through the code and do it properly: fix it at the source rather than try to bandage it up later.
回答(2 个)
Geoff Hayes
2017-7-13
编辑:Geoff Hayes
2017-7-13
Jason - it may depend upon which version of MATLAB that you are using. In (at least) R2014a, the input parameter to figure is a handle so figure(1) will always refer to the figure whose handle is 1...and so you wouldn't be able to change the handle of a figure so that the newest figure is of handle 1.
figure(n) finds a figure in which the Number property is equal to n, and makes it the current figure. If no figure exists with that property value, MATLAB® creates a new figure and sets its Number property to n.
With that in mind, you might be able to change the Number property of the current figure and then set the Number property of the new figure to 1 so that figure(1) refers to that new figure. My version of MATLAB doesn't support this property, but perhaps someone else can comment on this.
An alternative, would be to pass the handle of your current figure into your function so that it gets updated rather than figure(1).
Jan
2017-7-17
As far as I understand, your code always plot to figure(1). This is a bad idea and changing the code is the best solution. Let Matlab open a new figure for each diagram simply by omitting the number in the figure command. This is clean and easy, while addressing the figure(1) explicitely is the wrong approach, if you definitely do not want the figure(1) to be drawn in. Using some code to advance the figure number magically at the end of the loop, is too indirect to be clean.
11 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!