Figure open on every run

3 views (last 30 days)
max muster
max muster on 25 Mar 2024
Commented: Benjamin Kraus on 25 Mar 2024
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 Comments
Manikanta Aditya
Manikanta Aditya on 25 Mar 2024
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 on 25 Mar 2024
its not an function. Its all in one script

Sign in to comment.

Accepted Answer

Voss
Voss on 25 Mar 2024
Edited: Voss on 25 Mar 2024
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 Comments
Voss
Voss on 25 Mar 2024
@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 on 25 Mar 2024
@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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!