How to add drop down items based on a categorical array the user inputs in matlab gui app?

5 次查看(过去 30 天)
% Value changed function: SELECTTEAMDropDown
function SELECTTEAMDropDownValueChanged(app, event)
teamname = string(app.SELECTTEAMDropDown.Value)
disp(app.station_names)
disp(app.trucklist)
% for i=1:12
% val{i}={app.station_names{i}}
% end
class((app.station_names))
app.SELECTSTATIONDropDown.Items=app.station_names
end

回答(1 个)

Deepak
Deepak 2024-12-6,8:59
Hi Prajwal,
To resolve the issue of populating dropdown items in a MATLAB GUI app based on a user-provided categorical array, first convert the categorical array to a cell array of strings using the “cellstr” function. This conversion allows to assign the array to the “Items” property of dropdown, ensuring the GUI can display the options correctly. Check if the array is categorical using “iscategorical” before conversion, and if it is already a string or cell array, assign it directly.
Below is the MATLAB code to achieve the same:
% Value changed function: SELECTTEAMDropDown
function SELECTTEAMDropDownValueChanged(app, event)
teamname = string(app.SELECTTEAMDropDown.Value);
disp(app.station_names);
disp(app.trucklist);
% Convert categorical array to cell array of strings
if iscategorical(app.station_names)
stationNamesCellArray = cellstr(app.station_names);
else
stationNamesCellArray = app.station_names; % Assume it's already a cell array or string array
end
% Update the dropdown items
app.SELECTSTATIONDropDown.Items = stationNamesCellArray;
end
Please find attached the documentation of functions used for reference:
I hope this will help in resolving the issue.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by