
Save the user input dropdown item permanently and set a call back function to run when dropdown option is selected
    6 ビュー (過去 30 日間)
  
       古いコメントを表示
    
Hi,
I am Creating a drop down list (option1,option2,option3,option4) each when selected and press the run button, corresponding .m files are called and executed. 
I want the user to add the dropdown option in app (eg: Option5) and run the corresponding .m file.
I am trying the code and is attached below. The problem is I can add dropdown  item but how to assign the corresponding .m file to run when i press the run button.
Also, I want to store that dropdown option added newly and the corresponding .m file in the app permanently.
Please help me with this
I am trying this code:
        function AddButtonPushed(app, event)
            new = app.EditField2.Value;
            if ~any(ismember(app.DropDown.Items,new))
                app.DropDown.Items = [app.DropDown.Items new];
            end
        end
0 件のコメント
回答 (1 件)
  Akanksha
 2025 年 6 月 10 日
        You are facing the problem with A mapping structure (like a containers.Map) to associate each dropdown item with a .m file. 
Create a Mapping Between Dropdown Items and .m Files 
% In properties section 
properties (Access = private) 
FileMap % Map to store dropdown item -> .m file mapping 
end 
% In startupFcn or initialization 
app.FileMap = containers.Map(... 
    {'Option1', 'Option2', 'Option3'}, ... 
    {'file1.m', 'file2.m', 'file3.m'}); 
Add New Option and Associate It with a File : 
function AddButtonPushed(app, event) 
newOption = app.EditField2.Value;      % Dropdown label 
newFile = app.EditField3.Value;        % Corresponding .m file 
if ~any(strcmp(app.DropDown.Items, newOption)) 
    app.DropDown.Items = [app.DropDown.Items, newOption]; 
    app.FileMap(newOption) = newFile;  % Associate label with file 
    end 
end 
Run the File Associated with the Selected Option : 
function RunButtonPushed(app, event) 
selectedOption = app.DropDown.Value; 
if isKey(app.FileMap, selectedOption) 
    run(app.FileMap(selectedOption));  % Run the mapped file 
    else 
        uialert(app.UIFigure, 'No file mapped to this option.', 'Error'); 
        end 
end 
Save the Mapping to a File (for Persistence) : 
% Save to MAT file for persistence 
save('filemap.mat', 'newOption', 'newFile', '-append'); 
Load the Mapping When the App Starts : 
if isfile('filemap.mat') 
    S = load('filemap.mat'); 
    if isfield(S, 'newOption') && isfield(S, 'newFile') 
        app.FileMap = containers.Map(S.newOption, S.newFile); 
        app.DropDown.Items = keys(app.FileMap); 
        end 
else 
    % Default options 
    app.FileMap = containers.Map(... 
        {'Option1', 'Option2'}, ... 
        {'file1.m', 'file2.m'}); 
    app.DropDown.Items = keys(app.FileMap); 
end 
This will ensure to permanently store the dropdown option and its associated .m file. 
Adding these code snippets will make sure that you are able assign the .m file and run it as and when required. 

Kindly go through the following links for further reference: 
Hope this helps! 
0 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Database Toolbox についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

