Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to get gcf in base workspace from a GUI

Subject: How to get gcf in base workspace from a GUI

From: Yoshi

Date: 9 Jul, 2009 21:58:01

Message: 1 of 9

Hello,

I am trying to get a currently selected figure (gcf) created in the base workspace within a GUI function. For example, I just type "figure" in the Command Window and I get 1 as a handle to the figure.

I thought having

evalin('base', 'gcf')

in the GUI M-file would return 1 to me. But this only returns gcf in the GUI function workspace, not in the base workspace.

I also tried

get(0, 'CurrentFigure')

to see if I can get to gcf in the base by going through root (0), but only obtained again gcf in the GUI space. So, it appears that even the root is different, depending on whether it is queried from the base or GUI workspace.

Does anyone know how to accomplish my goal? Please let me know if you need more information or clarification of what I am trying to do.

Many thanks,
-Yoshi

Subject: How to get gcf in base workspace from a GUI

From: Matt Fig

Date: 9 Jul, 2009 22:21:01

Message: 2 of 9

I am not sure I understand what you want to do. If MATLAB is executing inside a GUI callback, then the current figure is the GUI no matter what figure was selected before the callback was called. I.e., by clicking on a uicontrol in the GUI, that GUI figure is made current.

Have I misunderstood your request.

Subject: How to get gcf in base workspace from a GUI

From: Yoshi

Date: 9 Jul, 2009 22:36:04

Message: 3 of 9

How stupid am I... That makes sense. Thanks very much for your answer.

Subject: How to get gcf in base workspace from a GUI

From: Matt Fig

Date: 9 Jul, 2009 22:45:04

Message: 4 of 9

"Yoshi " <matlab.20.yhidaka@9ox.net> wrote in message
> How stupid am I... That makes sense. Thanks very much for your answer.

This was not stupid, it is easy to overlook the obvious (or just not know something). I see experts on this NG do it all the time!


In fact I seem to remember doing the exact same thing with a GUI as you were trying to do, once upon a time!

Subject: How to get gcf in base workspace from a GUI

From: Yoshi

Date: 10 Jul, 2009 14:37:01

Message: 5 of 9

Thanks for your kind follow-up! That'll make me feel a lot better.

Subject: How to get gcf in base workspace from a GUI

From: Steven Lord

Date: 10 Jul, 2009 16:49:12

Message: 6 of 9


"Matt Fig" <spamanon@yahoo.com> wrote in message
news:h35qgd$oai$1@fred.mathworks.com...
>I am not sure I understand what you want to do. If MATLAB is executing
>inside a GUI callback, then the current figure is the GUI no matter what
>figure was selected before the callback was called. I.e., by clicking on a
>uicontrol in the GUI, that GUI figure is made current.
>
> Have I misunderstood your request.

That's not necessarily true.


function example3256
figure('Units', 'normalized', 'Position', [0.25 0.5 0.5 0.325]);
uicontrol('Style', 'pushbutton', 'Units', 'normalized', ...
    'Position', [0.25 0.25 0.5 0.5], 'Callback', @mycallback, ...
    'String', 'PUSH ME');

function mycallback(varargin)
figure('Units', 'normalized', 'Position', [0.25 0.125 0.5 0.325]);
uicontrol('Style', 'text', 'Units', 'normalized', ...
    'Position', [0.25 0.25 0.5 0.5], ...
    'String', sprintf('gcf = %d, gcbf = %d', gcf, gcbf));


If you want to be sure you're accessing the figure window containing the
object whose callback is executing, use GCBF. Similarly, if you want the
current object use GCO but if you want to be sure you're getting the handle
of the object whose callback is executing use GCBO.

--
Steve Lord
slord@mathworks.com

Subject: How to get gcf in base workspace from a GUI

From: Matt Fig

Date: 10 Jul, 2009 16:56:03

Message: 7 of 9

"Steven Lord" <slord@mathworks.com> wrote in message
> That's not necessarily true.
>
>
> function example3256
> figure('Units', 'normalized', 'Position', [0.25 0.5 0.5 0.325]);
> uicontrol('Style', 'pushbutton', 'Units', 'normalized', ...
> 'Position', [0.25 0.25 0.5 0.5], 'Callback', @mycallback, ...
> 'String', 'PUSH ME');
>
> function mycallback(varargin)
> figure('Units', 'normalized', 'Position', [0.25 0.125 0.5 0.325]);
> uicontrol('Style', 'text', 'Units', 'normalized', ...
> 'Position', [0.25 0.25 0.5 0.5], ...
> 'String', sprintf('gcf = %d, gcbf = %d', gcf, gcbf));
>
>
> If you want to be sure you're accessing the figure window containing the
> object whose callback is executing, use GCBF. Similarly, if you want the
> current object use GCO but if you want to be sure you're getting the handle
> of the object whose callback is executing use GCBO.
>
> --
> Steve Lord
> slord@mathworks.com
>

Yes, of course figure creation inside a callback would be an exception! Also I didn't say that the other (pre-existing) figures weren't accessible from the GUI callback, only that they weren't current if the callback was triggered by an event on the GUI (buttonpress, etc). They could be found by findobj, get(hand), etc, etc.

Subject: How to get gcf in base workspace from a GUI

From: Matt Fig

Date: 10 Jul, 2009 17:05:19

Message: 8 of 9

I did figure out how you could do what you wanted to do originally. Try this:

Open 3 figures:

figure,figure,figure

Now, do this:

get(0,'children')

Then select figure 1 as current, and again:


get(0,'children')

So from in the GUI, look at the second item in the list to get the figure which was current before the callback was triggered by an event on the GUI.

Subject: How to get gcf in base workspace from a GUI

From: Yoshi

Date: 10 Jul, 2009 17:33:02

Message: 9 of 9

Matt,
Thanks again. That is a clever and simple solution to what I need to do!

Steve,
The case you are considering does not apply to my current condition, but it will be a great tip to know in the future. Thanks.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us