Effacer les filtres
Effacer les filtres

How do you set the Layout Option at construction for ui objects?

25 vues (au cours des 30 derniers jours)
Rohan Kadambi
Rohan Kadambi le 28 Juin 2024 à 16:03
Commenté : Rohan Kadambi le 28 Juin 2024 à 20:21
If I have a simple uifigure like:
fh = uifigure;
gl = uigridlayout(fh);
I can add a uicomponent (and specify where on the gridlayout it will sit) like:
btn = uibutton(gl, Text="Hello World");
btn.Layout.Row = 2;
btn.Layout.Column = 2;
Is it possible to set the layout options using the Layout property of the btn e.g.
btn = uibutton(gl, Text="Hello World", Layout=???);
Sending a struct like:
btn = uibutton(gl, Text="Hello World", Layout=struct('Row',2,'Column',2);
Gives the error:
Error setting property 'Layout' of class 'Button':
'Layout' value must be specified as a
matlab.ui.layout.LayoutOptions object.
But I can't seem to instantiate an instance of the class LayoutOptions as:
l = LayoutOption();
Gives the error:
Unrecognized function or variable 'LayoutOption'.

Réponse acceptée

Adam Danz
Adam Danz le 28 Juin 2024 à 17:52
Modifié(e) : Adam Danz le 28 Juin 2024 à 17:52

Plus de réponses (1)

Umar
Umar le 28 Juin 2024 à 16:12
Hi Rohan,
By using uilayout.GridLayoutOptions to create a LayoutOptions object, you can set the row and column properties before assigning it to the button's Layout property. Here's the corrected approach to set layout options for a button in a grid layout:
fh = uifigure;
gl = uigridlayout(fh);
% Create a LayoutOptions object
layoutOptions = uilayout.GridLayoutOptions;
layoutOptions.Row = 2; layoutOptions.Column = 2;
% Add a button with specified layout options
btn = uibutton(gl, 'Text', 'Hello World', 'Layout', layoutOptions);
Hope this answers your question.

Catégories

En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by