How to create an uifigure with gcf
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2022-6-14
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
2023-8-31
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?
回答(2 个)
Voss
2023-8-31
"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 个评论
Nivedita
2023-9-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!