MATLAB Answers

Yet another create, numbered, figures with visible off

17 views (last 30 days)
nanren888
nanren888 on 11 Jul 2020
Edited: Adam Danz on 14 Jul 2020 at 14:31
I have a loop.
reuses figures, as matlab grinds to a halt with too many figures open.
Currently by numbering
inside for loop
figure(3)
plot,
print
end loop
.
Now sick of figures poping up, would like to get on with other things, want to make "visible","off"
figure(k,'Visible",'Off');
--> Numeric figure handles not supported with parameter-value pairs.
Message is clear enough. Annoying enough. Seems no particular reason for not allowing it.
.
Can obviously create handles, save handles and reuse handles, but that's way more messy than just numbering.
Any elegant suggestions?

  3 Comments

nanren888
nanren888 on 11 Jul 2020
Ok, throughly confused.
fH = figure('Visible','off'): - works
fH =
Figure (8) with properties:
Number: 8
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [1640 1638 560 420]
Units: 'pixels'
Show all properties
.
figure(fH): - makes ift visble again - ok
but
figure(fH,'Visible','off'):
==>
Error using figure
Numeric figure handles not supported with parameter-value pairs.
How do you return to a figure without making it visible?
madhan ravi
madhan ravi on 11 Jul 2020
“How do you return to a figure without making it visible?”
By its handle.
nanren888
nanren888 on 12 Jul 2020 at 19:16
Well thanks for the informative response. These forums are made or broken on the friendliness and usefulness of responses.

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 11 Jul 2020
Edited: Adam Danz on 12 Jul 2020 at 21:08
You can use a 2-liner.
f = figure(1);
f.Visible = 'off';
Or, you could put it into a function so that you can use a 1-liner (that references a 2-lined function).
function h = invisibleFig(n)
% n is a positive integer
h = figure(n);
h.Visible = 'off';
end
now create a new figure with
invisibleFig(1)
Alternatively, don't specify the figure number and use the figure handle instead. This is almost always the better approach.
h = figure('Visible','off');
Recap
Try each section below to become familiar with these methods. The key is to use the figure handle (h, in my lines below).
To create a figure,
h = figure();
To create an invisible figure,
h = figure('Visible','off');
To set the visibility on,
h.Visible = 'on'; % = 'off' to turn back off
% or
set(h, 'Visible', 'on') % or 'off' to turn back off
To make a figure current,
figure(h)
% or
set(0,'CurrentFigure',h)
If you want to control the figure number and the visibility you must do that in two lines. However, it's rarely necessary to control the figure number. Use the figure handle instead.
h = figure(1);
h.Visible = 'off'; % or 'on' to turn back on
Lastly, and importantly, use the figure handle to specify a parent of an axes or any other object being added to the figure.
Example:
h = figure();
ax = axes(h);

  2 Comments

nanren888
nanren888 on 12 Jul 2020 at 19:26
Thanks for your reponse.
each time you use figure() it makes it visible again. Hiding it again, after visbile might work, I guess if the system didn't drawnow();
Use a handle?
I think that's what I did, when I tried, as in the comment.
The question is how do I return to it, to reuse it?
As above
fH = figure('Visible','off'): - works & returns a handle
But
figure(fH): - makes it visble again - so can't select the figure this way
And
figure(fH,'Visible','off'):
==>
Error using figure
Numeric figure handles not supported with parameter-value pairs.
So can't return to the figure this way.
.
I may be missing something obvious: hence the question. Happy to have someone tell me this.
.
I guess
(1) Maybe no way to do it elegantly, using figure(), eg need such as plot(ax, ....) which means changing every plot call :(
(2) set(0,'CurrentFigure',fH) ?
OK set(0,'CurrentFigure',fH) appears to select the figure, but not make it visible.
Adam Danz
Adam Danz on 12 Jul 2020 at 21:09
Sounds like you're working in circles and missing the point :)
I added a "recap" section to my answer to walk you through several options to manipulate the visibility of figures and control which figure is current. If there's something I'm missing, I'd be glad to help further.

Sign in to comment.


nanren888
nanren888 on 12 Jul 2020 at 19:26
set(0,'CurrentFigure',fH) appears to select the figure, but not make it visible.

  0 Comments

Sign in to comment.

Products


Release

R2020a