How to create an uifigure with gcf

조회 수: 7 (최근 30일)
Markus Leuthold
Markus Leuthold . 2022년 6월 14일
답변: Nivedita . 2023년 9월 1일
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
Joaquin Ambia
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);
f_out = uifigure();

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');
  • 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!


Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by