Detect if variable is a GUI container
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi!
For my code I'm using quite a lot of classes and functions, and some of them require or produce a GUI object. Sometimes i also want that they work inside a supplied GUI container (it can be a figure, a tab, a panel or whatever).
Now, supposing to always have proper inputs is not so good, so I want to check them using something like validateattributes.
I can insert all the possibilities, but it would be better (and more flexible) to use some sort of built in check, maybe thre one used for the "parent" properties of other objects, but I cannot find this function.
Is there anybody that know it?
Thanks,
Jacopo
Réponse acceptée
Adam
le 3 Déc 2018
Modifié(e) : Adam
le 3 Déc 2018
I tend to validate most of my function inputs and often write classes that involve plotting something (either on a supplied axes or creating one if the supplied axes is empty) or adding a UI component to a parent, but I tend to take each case as it comes. e.g.
validateattributes( hParent, { 'matlab.ui.container.internal.UIContainer', 'matlab.ui.Figure' }, { 'scalar' } )
is what I am currently using to test the handle of a parent object that I am going to attach e.g. a uiccontrol or a uipanel to.
try
validateattributes( hAxes, { 'matlab.graphics.axis.Axes' }, { 'scalar' } )
if ~ishghandle( hAxes )
error( 'hAxes must be a valid axes handle' )
end
catch e
throwAsCaller(e);
end
is what I use for validating an axes handle that I want to plot something on, etc etc. So I'm not convinced a 'one size fits all' validation of anything and everything makes too much sense.
It's also useful to note that if you ever want to know the class type to test for you can just create one of the elements in question and class class on it.
e.g.
figure; hAxes = gca;
class( hAxes );
and then just paste the answer into the validateattributes call.
0 commentaires
Plus de réponses (1)
Jan
le 30 Nov 2018
Modifié(e) : Jan
le 30 Nov 2018
Perhaps you mean:
ishghandle(Variable)
or
isgraphics(Variable, 'panel') % To detect a panel object
% [EDITED] This means:
isgraphics(Variable, 'uipanel') % To detect a uipanel object
13 commentaires
Jan
le 3 Déc 2018
Dear Jacopo: No problem, I do never feel upset in the forum. I'm happy if your problem is solved. It was the demand for "new containers that will be added in future version" which let me think, that the compatibility between Matlab versions is important.
Thhis code checks for UIContainers and figures:
validateattributes(h, ...
{'matlab.ui.container.internal.UIContainer', 'matlab.ui.Figure'}, ...
{'scalar'})
You asked for "a figure, a tab, a panel or whatever" and "(ui)figure, uitab, uipanel, gridLayouts". Of course you have to add the further elements here also, so 'matlab.ui.container.internal.UIContainer' does not catch all objects you have asked for - as isgraphics does not also. I'd still prefer:
isgraphics(h, 'uicontainer')
because it does not depend on the (as fas as I know not documented) type 'matlab.ui.container.internal.UIContainer'.
Nevertheless, if it is the solution of your question, everything is fine. Then I simply do not understand the question, but this is not a problem which needs a solution ;-)
Adam
le 4 Déc 2018
Yeah, I originally posted mine as a comment (as I tend to nowadays with 80-90% of my contributions since I don't like things being posted as answers that do not fully answer the question, but if the questioner requests it then I do) because it was never meant as a final solution to the problem, more just a suggestion of 2 examples of what I do, copy-pasted from the functions I use. In my case I maybe want fewer things to act as parents - e.g. I never add uicontrols directly to a tab. when I am doing a programmatic UI I am always using the GUI Layout toolbox so I always have things either in a layout (uix.HBox or uix.VBox) which is where the selection of things in my validation comes from. I do sometimes add panels directly to a figure though which is why I guess I have that included.
My axes validation is quite old so there are other useful functions too for that, though I generally use validateattributes because I don't want the boolean return type, I just want an error thrown. Obviously in this case, since I wrapped it up in its own validateAxes function (I didn't include that bit) I could just as easily have captured the boolean and thrown the error all in one go anyway!
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!