How to use figure(1) in MATLAB APP

조회 수: 13 (최근 30일)
Kyle
Kyle 2025년 6월 20일
편집: Benjamin Kraus 2025년 6월 25일
I have a script built in MATLAB editor, and I want to easily transfer it to a MATLAB app.
I have many instances in which I call "figure(1)" without pre-defining anything. This works fine in MATLAB editor, but in MATLAB app designer it thinks that I cam calling a variable that is not yet defined. Is there an easy work around to this?
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 6월 20일

note that in app designer you should be working to convert to uifigure().

Note that as of R2025a, the difference between figure and uifigure got much smaller, with both being built on WebGL; in previous versions, figure was built on OpenGL

Matt J
Matt J 2025년 6월 20일
편집: Matt J 2025년 6월 20일
but in MATLAB app designer it thinks that I cam calling a variable that is not yet defined
That shouldn't be happening, so we probably need to see more of the code. The only potential problem with calling figure() in an app designer app is that the figure will open in its own window, whereas often you want the figure graphic you are plotting to be displayed somewhere in the app window itself.

댓글을 달려면 로그인하십시오.

답변 (1개)

Benjamin Kraus
Benjamin Kraus 2025년 6월 25일
편집: Benjamin Kraus 2025년 6월 25일
I'm curious why you are calling figure(1)?
I can think of three reasons you may be calling figure(1):
  1. You want to put the figure with the ID 1 on top of all other windows.
  2. You want to make the figure with the ID 1 the "current figure".
  3. You want to get a handle to the figure with the ID 1.
A fundamental difference between App Designer (and uifigure) vs. a regular figure is the HandleVisibility and the IntegerHandle.
Within App Designer (and when you create a figure using the uifigure command):
  • The default HandleVisibility is 'off'. This means that your figure will never be the "Current Figure".
  • The default IntegerHandle is 'off'. This means that your figures will not get integer numbers (such as "1") assigned to them.
This means that none of those three bullets above are going to work in an App Designer app, unless you reenable HandleVisibility and IntegrHandle. However, that is not recommended.
Consider this code:
figure % Create a figure and make it the "Current Figure"
axes % Create a new axes in the "Current Figure" and make it the "Current Axes"
plot(1:10) % Add a line to the "Current Axes"
hold on % Enable hold on the "Current Axes"
bar(1:10) % Add a bar graph to the "Current Axes"
title('My data') % Add a title to the "Current Axes"
In the code above, all but the first line of code relied on the "Current Figure" and/or the "Current Axes". This code pattern is discouraged in App Designer.
If you were to rewrite this code in App Designer, you would write it like this:
f = app.UIFigure; % Get a handle to the figure used by the App.
ax = axes(f); % Create an axes in the figure specified.
plot(ax,1:10); % Create a line in the axes specified.
hold(ax, 'on'); % Enable hold on the axes specified.
bar(ax,1:10); % Create a bar graph in the axes specified.
title(ax, 'My data'); % Add a title to the axes specified.
With this new code pattern, you are explicitly specifying the parent (or target) for every command, so you are no longer counting on the Current Figure or the Current Axes. This makes reason 2 above unnecessary.
Also, with this new code pattern, you are using a variable (f) to refer to your figure. This is a replacement for using an integer (such as 1). Using an integer to refer to figures is an outdated and discouraged code pattern. In R2014b we introduced graphics objects to represent figures, and this is the preferred method. This makes reason 3 above unnecessary.
If your reason for calling figure(1) is to put the figure on top of all other windows, then you want to use f instead:
figure(f)
You can get f by using a property on your app, or by storing the output from the figure (or uifigure) command.
However, while leveraging integer handles and the current figure/axes is discouraged in App Designer, it is still possible to do. What you need to do is set both HandleVisibility and IntegerHandle to 'on' within your App. Note, however, that your figure is not guaranteed to be assigned the integer 1, it will pick the next available unused integer.
This may allow you to contiue you using your script in App Designer, with only minor modifications, but it should be viewed as a temporary workaround until you have a chance to update the script to use the recommended pattern of storing and passing around graphics objects, and stop using integers to refer to figures.

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by