Figure identity Question with Loops

6 次查看(过去 30 天)
Jason
Jason 2017-7-13
评论: Adam 2017-7-18
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
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
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
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.
In later versions of MATLAB, the input to figure can be a number (see input n) where
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
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 个评论
Jason
Jason 2017-7-18
Thanks Jan & Adam, I will take a look. Just out of interest, Walter suggested this sometime ago to close figures:
fig1h = findall(0,'type','figure','Tag','figure1'); %Keep this open as its the main gUI figure
figh = findall(0,'type','figure');
other_figures = setdiff(figh, fig1h);
delete(other_figures)
Adam
Adam 2017-7-18
close all
will close all figures that aren't GUIDE figures too.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by