Figure identity Question with Loops

6 vues (au cours des 30 derniers jours)
Jason
Jason le 13 Juil 2017
Commenté : Adam le 18 Juil 2017
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 commentaires
Adam
Adam le 17 Juil 2017
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 le 17 Juil 2017
@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.

Connectez-vous pour commenter.

Réponses (2)

Geoff Hayes
Geoff Hayes le 13 Juil 2017
Modifié(e) : Geoff Hayes le 13 Juil 2017
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 commentaire
Jason
Jason le 17 Juil 2017
Thanks Geoff, Im using 2017a so will give it a go. J

Connectez-vous pour commenter.


Jan
Jan le 17 Juil 2017
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 commentaires
Jason
Jason le 18 Juil 2017
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 le 18 Juil 2017
close all
will close all figures that aren't GUIDE figures too.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by