Create an array of UI objects

13 次查看(过去 30 天)
Khalil Khalil
Khalil Khalil 2021-4-5
I'm building an app in MatLab's App designer and I need the number of some UI Elements to be variable, depending on the user's input. I want to create an array of these objects (which are of the same type/class), so that I can cycle through them, accessing and changing their properties.
This is an example of the callback function that is called when the user confirms his input by pressing a button:
% get number of layers from input field
layerNum = app.layersFld.Value;
% define and create grid
gridHeight = 2+layerNum;
gridWidth = 6+layerNum;
configGrid = uigridlayout(app.ConfigPanel, [gridHeight, gridWidth]);
% create array of max needed size
% dd = ??(1,gridHeight) --------- Which array constructor to use here?
% create array of dropdown elements
for i=1:gridHeight
dd(1,i) = uidropdown(configGrid);
dd(1,i).Layout.Row = i;
dd(1,i).Layout.Column = i;
end
A notification tells me "The variable 'dd' appears to change size on every loop iteration. Consider preallocating for speed.". How would I go about doing this? I tried differenct array classes/constructors but I get error codes everytime. Is there a constructor for arrays containing UI elements/objects?
BTW: This code snippet works as is, the app runs as expected and checking the outcome of the for-loop in a seperate m-file showed me that a horizontal vector with dropdown elements in each position is created... But I'd like to learn doing it the right way, optimizing my app and learn more about how MatLab handles objects.

回答(1 个)

jean-luc collette
jean-luc collette 2023-11-23
You just have to add this line before the loop :
dd=uicontrol();

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by