Create drop downs dynamically from file input

5 次查看(过去 30 天)
I am working on creating an application in app designer and part of it requires me to read a set of data from a file and create dropdowns from the content of the files. So, the file contains what the dropdown should be and what the dropdown options should be. Is it possible to do something like this...without knowing the drop downs or how many there is going to be until the file is read?
I was trying to do something like this where I store the drop down items inside an array, but it doesn't seem to work. I get an error that dot indexing is not supported for variable of thsi type. And looking in the workspace, app.presetDropdown(i) isn't even a drop down item, but a double...which is confusing.
for i=1:length(A)
app.presetDropdown(i) = uidropdown;
app.presetDropdown(i).FontSize = 16;
...

回答(1 个)

Vedant Shah
Vedant Shah 2025-6-18
The MATLAB App Designer application here requires the dynamic generation of dropdown menus based on file input. The file typically specifies both the number of dropdowns and their respective options, which are not known beforehand.
In dynamic creation of dropdown components in MATLAB App Designer, ensure that the storage structure is compatible with UI objects. If app.presetDropdown is not explicitly declared as a cell array or object array, MATLAB may default to interpreting it as a numeric array, resulting in errors such as:
"Dot indexing is not supported for variables of this type."
To correctly store and manage multiple dropdowns, a cell array should be used, as UI components are object handles. The following code snippet demonstrates an approach for the same:
app.presetDropdown = cell(1, length(A));
for i = 1:length(A)
dd = uidropdown(app.UIFigure);
dd.FontSize = 16;
dd.Items = A{i};
dd.Position = [100, 300 - (i-1)*50, 150, 22];
app.presetDropdown{i} = dd;
end
This approach ensures that each dropdown is created and positioned distinctly. It also assumes that the variable A contains a cell array of item lists, one for each dropdown.
For more information, refer to the following documentations:

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by