How to create an uifigure with gcf

7 vues (au cours des 30 derniers jours)
Markus Leuthold
Markus Leuthold le 14 Juin 2022
I'm looking for an gcf() equivalent for the new web-based uifigure(). Or in other words, I'm looking for this function:
  • If no uifigure exists, create a new one
  • If an uifigure already exists, use the existing one
Currently, if an uifigure exists and you call gcf, a new java-based figure() is created.
  2 commentaires
Joaquin Ambia
Joaquin Ambia le 31 Août 2023
This is not an answer at all.
The question does not ask for older versions of Matlab, on the contrary, is asking about functionality in the latest versions.
If the answers have been posted, can you add the link?

Connectez-vous pour commenter.

Réponses (2)

Voss le 31 Août 2023
"Currently, if an uifigure exists and you call gcf, a new java-based figure() is created."
That's not necessarily true. gcf() can return a handle to an existing uifigure. The reason you don't normally observe that behavior is that by default, uifigures have their HandleVisibility set to 'off', so they will not be found by gcf(). (figures default to HandleVisibility 'on', so they are found by gcf().)
Here's a function that does as you describe, without regard to HandleVisibility state:
function f_out = gcuif()
f = findall(0,'Type','figure');
for ii = 1:numel(f)
if matlab.ui.internal.isUIFigure(f(ii))
f_out = f(ii);
f_out = uifigure();

Nivedita le 1 Sep 2023
Hi Markus!
I understand that you are trying to look for an alternative to the “gcf” function for MATLAB “uifigure".
In MATLAB R2021b and later versions, the “gcf” function is used to get the current figure, which is typically a Java-based figure. If you want to achieve the equivalent behaviour for “uifigure”, you can implement it using a custom function. Here's a way to do it:
myFig = get_uifigure(); % calling the get_uifigure function
%Function to check for existing UIFigure and creating a new UIFigure if none exists
function fig = get_uifigure()
% Check if a UIFigure already exists
fig = findall(groot, 'Type', 'Figure', 'Tag', 'MyUIFigureTag');
if isempty(fig)
% Create a new UIFigure
fig = uifigure('Tag', 'MyUIFigureTag');
  • The “get_uifigure” function checks if a “uifigure” with a specific tag exists. If it does, it returns the existing one; otherwise, it creates a new “uifigure” with the specified tag. This way, you ensure that you always work with the same “uifigure” when calling the function multiple times.
  • The “findall” function used in the code returns the handles of all the visible and hidden uifigures with the tag “MyUIFigureTag”.
  • For more detailed information about “uifigure” and its properties, you can refer to the following MATLAB documentation: Create figure for designing apps - MATLAB uifigure
  • The code also uses the “findall” function to search for existing uifigures, information about which can be found in the documentation link here: Find all graphics objects - MATLAB findall
I hope this helps!


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




Community Treasure Hunt

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

Start Hunting!

Translated by