Create an array of UI objects
13 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
jean-luc collette
on 23 Nov 2023
You just have to add this line before the loop :
dd=uicontrol();
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!