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 commentaires

Manikanta Aditya
Manikanta Aditya le 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 le 25 Mar 2024
its not an function. Its all in one script

Connectez-vous pour commenter.

 Réponse acceptée

Voss
Voss le 25 Mar 2024
Modifié(e) : Voss le 25 Mar 2024

0 votes

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 commentaires

Example of the difference between isvalid and ishandle:
% fig exists and is a graphics object
fig = figure();
ishandle(fig)
ans = logical
1
isvalid(fig)
ans = logical
1
% fig exists but is not a graphics object
fig = pi;
ishandle(fig)
ans = logical
0
isvalid(fig)
Incorrect number or types of inputs or outputs for function isvalid.
max muster
max muster le 25 Mar 2024
thx i had clc and clear all at begin of my script. Now its not opening several times. Thats nice.
If i change something and click run i have to close and then run again ( so it will open new) to see the changes. Possible to see changes without close before?
Voss
Voss le 25 Mar 2024
Modifié(e) : Voss le 25 Mar 2024
You're welcome!
If this solves the problem please "Accept" this answer. Thanks!
Benjamin Kraus
Benjamin Kraus le 25 Mar 2024
@Voss: I would recommend either isvalid or isgraphics over either ishandle or ishghandle.
  • isgraphics and ishghandle are exactly the same function. isgraphics is the new name for ishghandle, but they are otherwise functionally identical (they share an implementation). I prefer isgraphics because it is the new name, and we've been discouraging the use of "hg" has an abbreviation in our documented APIs.
  • If you know the input object is an MCOS object (and not a double), then isgraphics is basically equivalent to isa(h, 'matlab.graphics.Graphics') & isvalid(h). So, in the case for @max muster, they know that fig is already a handle to a Figure object, so checking isvalid is a perfectly reasonable approach (and what I would recommend) rather than checking isgraphics (which will do more work but return the same result). There is one small exception, which is gobjects(1) will register as valid as far as isvalid is concerned, but will return false for isgraphics, but that distinction doesn't apply in this case.
  • ishandle is defined as "Test for valid graphics or Java object handle", which is too broad for this use-case, so I wouldn't recommend using that function.
If I were @max muster I would use isvalid in this situation.
@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 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by