3

I have a script that creates multiple uifigures:

% create fig1
fig1 = uifigure('Name', 'Figure 1');

% create fig2
fig2 = uifigure('Name', 'Figure 2');

% set fig2 as the current figure
set(0, 'currentfigure', fig2);

% get the current figure
gcf

I want the second uifigure to be the current one, so I set it with the third command. However, when I then use gcf to get the current figure, it is still the first one. Here is the output from the command line:

ans = 

  Figure (1) with properties:

      Number: 1
        Name: ''
       Color: [0.9400 0.9400 0.9400]
    Position: [514 371 560 420]
       Units: 'pixels'

  Show all properties

What is the problem?

Wolfie
  • 27,562
  • 7
  • 28
  • 55
LukasFun
  • 171
  • 1
  • 4
  • 17
  • Does `set(groot, 'currentfigure', fig2)` work? – Andras Deak -- Слава Україні Jan 22 '21 at 00:15
  • For me, with Matlab R2019a, it works fine. Do you accidentally click on the first figure between the `set(0, 'currentfigure', fig2);` and `gcf` commands? – Matteo V Jan 22 '21 at 07:29
  • @MatteoV can confirm OP's issue running on 17b – Wolfie Jan 22 '21 at 08:28
  • Actually scratch that, I had only tested it with `figure`s rather than `uifigure`s. Using the proper commands, I get the same issue. It seems that `gcf` returns the handle to a `figure` 1 which it automatically creates. – Matteo V Jan 22 '21 at 08:40
  • Your MATLAB version is important here since `uifigure` and app-designer are relatively new (ish). For example, the `HandleVisibility` property, which is off by default and obscuring the `uifigure` handles from you is always off in `17b`, but in the latest [docs](https://uk.mathworks.com/help/matlab/ref/matlab.ui.figureappd-properties.html) it looks like you can optionally set it `'on'` which may help with this issue. Please [edit] your question to include the MATLAB version you are using. – Wolfie Jan 22 '21 at 08:49

2 Answers2

1

for uifigure the property HandleVisibility — Visibility of object handle is set to 'off', meaning that this property is read-only.

This property provides information about the visibility of the Figure object in its parent's list of children. Because this property value is always set to 'off', the Figure object is not visible in its parent’s list of children and it is not returned by functions that access objects by searching the object hierarchy or querying object properties. These functions include gcf, get, findobj, clf, and close.

Objects are valid even if their HandleVisibility property is set to 'off'. If you have access to an object, you can set and get its properties, and pass it to any function that operates on objects. However, gcf cannot return an App Designer figure...

bla
  • 25,846
  • 10
  • 70
  • 101
0

This code does what you want, creates two figures and puts the second figure on top

% create fig1
fig1 = uifigure('Name', 'MyFig 1');

% create fig2
fig2 = uifigure('Name', 'MyFig 2');

% set fig2 as the current figure
drawnow
figure(fig2)

You will see that figure 2 is on top instead of figure 1, which was the case with your original code. If you call gcf, it will create a new figure which will also be called "figure 1" since none of the figures created by uifigure have the "HandleVisibility" property set to "on". In my code, I changed the figure names slightly so that if you run gcf, you will see that it is creating a new figure called Figure 1, not switching to the first figure created by this code.

If you want gcf to work and see figure 2, you can turn HandleVisbility on when you create the figure. Replace the line above for creating figure 2 with this code:

fig2 = uifigure('Name', 'MyFig 2', 'handlevisibility', 'on');
Kenneth Boyd
  • 667
  • 2
  • 6