User Input to the Dropdown menu in appdesigner

13 visualizaciones (últimos 30 días)
Harish M Y
Harish M Y el 19 de Jul. de 2021
Respondida: Akanksha el 10 de Jun. de 2025
Hi,
I am developing an application where I need to run some .m files. So, I created a dropdown menu (with 4 options say for eg: a,b,c,d) which when selected and run opens a directory window to select the corresponding .m file to select and run.
If in case in fututre, if the user wants one more option (eg: e) to be added to the dropdown menu list, I have a provision to write the name for dropdown as e and add that to the dropdown menu and run the model.
But, when I close the app and run the app again, the newly added dropdown menu (e) is not there. I need to store that into the database permanetaly. Any suggestions for this ?
%% Code to add the new dropdown name to the dropdown list
app.new = app.EditField2.Value;
if ~any(ismember(app.DropDown.Items,app.new))
app.DropDown.Items = [app.DropDown.Items app.new];
end
%% Code to run the corresponding model
if app.DropDown.Value == app.new
RunModel5; %% name of .m file
end
I want the Item added to be stored permanentaly and also the RunModel5 into the database. Please help me with this
  1 comentario
Harish M Y
Harish M Y el 19 de Jul. de 2021
Hey one more thing, the RunModel5 is the .m file which is also selected byy the user for first time and the RunModel5 is to be stored afterwards.

Iniciar sesión para comentar.

Respuestas (1)

Akanksha
Akanksha el 10 de Jun. de 2025
The code you provided adds a new item to the dropdown only in memory and does not save the new item or the associated .m file path anywhere permanent like a file or database.That’s the reason when the app is closed and reopened, the dropdown resets to its original state and the newly added item and its .m file are lost.
The following code ensures that previously added items are loaded every time the app starts:
if isfile('modelDatabase.mat')
data = load('modelDatabase.mat', 'modelMap');
app.modelMap = data.modelMap;
app.DropDown.Items = keys(app.modelMap);
else
app.modelMap = containers.Map;
end
Also, to save the new dropdown item and its associated .m file path permanently :
newItem=app.EditField.Value;
[file,path]=uigetfile('*.m','Select the corresponding .m file');
if~isKey(app.modelMap,newItem)
app.modelMap(newItem)=fullfile(path,file);
app.DropDown.Items=[app.DropDown.Items,newItem];
save('modelDatabase.mat','modelMap');
end
To run the .m file associated with the selected dropdown item :
selectedItem = app.DropDown.Value;
if isKey(app.modelMap, selectedItem)
run(app.modelMap(selectedItem));
end
Incorporating these code snippets will ensure the dropdown remains dynamic and retains its state.
Even after closing and reopening the app, the previously added model remains available in the dropdown.
PFA the links for further reference
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by