필터 지우기
필터 지우기

Can you place ui-components in uigridlayout using Name-Value pair input?

조회 수: 48 (최근 30일)
chicken vector
chicken vector 2023년 6월 2일
댓글: Les Beckham 2024년 6월 28일 20:21
uif = uifigure;
uig = uigridlayout('Parent', uif, ...
'ColumnWidth', repmat({'1x'}, 1, 5), ...
'RowHeight', repmat({'1x'}, 1, 3));
uib = uibutton('Parent', uig, ...
'Text', 'I want to be placed in one line')
% Put this lines inside 'uibutton' initialisation:
uib.Layout.Column = 3;
uib.Layout.Row = 2;
I searched for something able to create an instance of GridLayoutOptions, but i can't find anything.
An alternative would be to understand how matlab.ui.layout.LayoutOptions would accept inputs.

채택된 답변

David
David 2024년 1월 4일
이동: Adam Danz 2024년 6월 28일 17:52
I ran into this same problem today, and found a solution. The above answer is a little misleading. You can place an obect in a uigridlayout in one line. For instance using your code to start, I place the uibutton on Row 2 and Column 3 of the uigridlayout in one line.
uif = uifigure;
uig = uigridlayout('Parent', uif, ...
'ColumnWidth', repmat({'1x'}, 1, 5), ...
'RowHeight', repmat({'1x'}, 1, 3));
uib = uibutton('Parent', uig, ...
'Text', 'I am being placed in one line','Layout', ...
matlab.ui.layout.GridLayoutOptions('Row',2,'Column',3));
Here, I use matlab.ui.layout.GridLayoutOptions to create a GridLayoutOptions object and assign it to the 'Layout' property of the uibutton. Then inside the call for matlab.ui.layout.GridLayoutOptions, I just use name value arguments to assign the 'Row' and 'Column' property to match your original code. GridLayoutOptions objects are normally places indes the 'Layout' property of these types of objects, so this should work fine.

추가 답변 (2개)

Vijeta
Vijeta 2023년 6월 14일
편집: Vijeta 2023년 6월 14일
`GridLayoutOptions` is a specific subclass of `LayoutOptions` that applies to grid layouts, so you can create an instance of `GridLayoutOptions` as follows:
% Create a grid layout and a button
uif = uifigure;
uig = uigridlayout(uif);
uib = uibutton(uig, 'Text', 'I want to be placed in one line');
% Create and set GridLayoutOptions properties
glo = matlab.ui.layout.GridLayoutOptions;
% Set row span width
glo.RowSpan = [1 1]; % default
% Set column span width
glo.ColumnSpan = [1 5];
% Assign GridLayoutOptions object to button
uib.Layout.Row = 2;
uib.Layout.Column = 1;
uib.Layout.GridLayoutOptions = glo;
Here, `glo` is a `GridLayoutOptions` object that specifies the row span and column span of the button, and is assigned to the `GridLayoutOptions` property of the button's layout. You could modify the `RowSpan` and `ColumnSpan` properties of `glo` to suit your layout needs.
Alternatively, you could use the `LayoutOptions` object directly without creating a `GridLayoutOptions` object, as follows:
uib.Layout.Row = 2;
uib.Layout.Column = 1;
uib.Layout.RowSpan = [1 1];
uib.Layout.ColumnSpan = [1 5];
Here, the `RowSpan` and `ColumnSpan` properties of `LayoutOptions` are set directly.
Refer to the following resources
I hope this helps you customize the layout options of your app! Let me know if you have any further questions.
  댓글 수: 1
chicken vector
chicken vector 2023년 6월 16일
From your answer I understand there is no way to place an object in a uigridlayout in one line, right?

댓글을 달려면 로그인하십시오.


Eden Winga
Eden Winga 2024년 5월 3일
This is a similar question, but I'm trying to create a 2048 game in the GUI app designer, but I can't seem to figure out how to access a GridLayout component by its handle or name. I have label components in a 4x4 gridlayout and want to change the text in the label from 0 to 2 or 4 randomly. How would i make a for loop that goes through every element in the 4x4 grid and randomly generate a 2 or 4?
  댓글 수: 1
Les Beckham
Les Beckham 2024년 6월 28일 20:21
Perhaps this will get you started (adapted from David's answer above). Although this doesn't use app designer.
uif = uifigure;
uig = uigridlayout('Parent', uif, ...
'ColumnWidth', repmat({'1x'}, 1, 4), ...
'RowHeight', repmat({'1x'}, 1, 4));
values = randi(2,1,16) * 2;
for i = 1:numel(values)
[row, col] = ind2sub([4 4], i);
uib = uilabel('Parent', uig, ...
'Text', num2str(values(i)), 'Layout', ...
matlab.ui.layout.GridLayoutOptions('Row', row, 'Column' , col), ...
'HorizontalAlignment', 'center');
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Testing Framework에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by