How to create an uifigure with gcf
12 views (last 30 days)
Show older comments
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 Comments
Jan
on 14 Jun 2022
This requires from clutter if you want to support older Matlab versions. When I search for corresponding details in the forum, I find your answers and comments there already.
Joaquin Ambia
on 31 Aug 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?
Answers (2)
Voss
on 31 Aug 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);
return
end
end
f_out = uifigure();
end
0 Comments
Nivedita
on 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');
end
end
- 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!
0 Comments
See Also
Categories
Find more on Develop uifigure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!