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

5 views (last 30 days)
% 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

Answers (1)

Deepak
Deepak on 6 Dec 2024 at 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.

Categories

Find more on Tables 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!