필터 지우기
필터 지우기

Figure identity Question with Loops

조회 수: 14 (최근 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).
  댓글 수: 1
Jason
Jason 2017년 7월 17일
Thanks Geoff, Im using 2017a so will give it a go. J

댓글을 달려면 로그인하십시오.


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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by