How to create an uifigure with gcf
조회 수: 7 (최근 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.
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)
f_out = f(ii);
f_out = uifigure();
Nivedita 2023년 9월 1일
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');
% 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!