Figure open on every run

3 次查看(过去 30 天)
Hello i want my figure only open once and not on very new compile. I send the code but it still opens everytime i click run. I added the if equations but it didnt changed: Can you pls help me?
if ~exist('fig', 'var') || ~isvalid(fig)
fig = uifigure('Name', 'test', 'Position', [300, 300, 800, 600]);
% UI-Komponenten erstellen
layout = uigridlayout(fig, [1, 2]);
label1 = uilabel(fig, 'Position', [20, 500, 300, 60], 'Text', ['hello'],'FontWeight', 'bold');
label2 = uilabel(fig, 'Position', [130, 500, 300, 60], 'Text', ['hello'],'FontWeight', 'bold');
layout.ColumnWidth = {'1x', '1x'};
label3 = uilabel(fig, 'Position', [20, 350, 300, 60], 'Text', ['hello']);
label4 = uilabel(fig, 'Position', [20, 320, 300, 22], 'Text', ['hello']);
end
  2 个评论
Manikanta Aditya
Manikanta Aditya 2024-3-25
Hi,
I think the issue is mostly due to the scope of fig variable. If you’re running this code in a function, the fig variable is local to that function. This means that every time you run the function, it doesn’t know about the fig from the last run.
max muster
max muster 2024-3-25
its not an function. Its all in one script

请先登录,再进行评论。

采纳的回答

Voss
Voss 2024-3-25
编辑:Voss 2024-3-25
You should use ishandle, ishghandle, or isgraphics instead of isvalid.
Also, make sure you don't have a clear or clear all statement or a close all statement in your script anywhere before the line where you check the existence and validity of fig.
  6 个评论
Voss
Voss 2024-3-25
@Benjamin Kraus: Thank you for the recommendation and explanation.
To be honest, I had never heard of isvalid or isgraphics before today. Good to know about isgraphics being the new name for ishghandle.
What you say makes sense, and I'd emphasize the caveat "that fig is already a handle to a Figure object". If this assumption is violated - which can easily happen since @max muster's code is a script - then an error may be thrown when checking for fig using isvalid, which wouldn't happen using isgraphics.
% some other script defines fig as a double for whatever reason:
fig = pi;
% then my script checks for fig using isgraphics, and it works:
if ~exist('fig','var') || ~isgraphics(fig)
disp('creating a new uifigure')
end
creating a new uifigure
% as opposed to: my script checks for fig using isvalid, and an error is thrown:
if ~exist('fig','var') || ~isvalid(fig)
disp('creating a new uifigure')
end
Incorrect number or types of inputs or outputs for function isvalid.
For that reason, I would say isgraphics is a more robust choice than isvalid in this situation.
Benjamin Kraus
Benjamin Kraus 2024-3-25
@Voss: I agree with your conclusions, and the "extra work" preformed by isgraphics is trivial compared to everything else, so yeah, isgraphics is probably the best choice.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by