Adding dropdown option from GUI in App Designer
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Is there a way to permanently add another option to a dropdown menu without having to go to the code? I will be getting objects periodically that I need to add to the list of options and I wonder if there is a way to add them from the app.
I was thinking about creating a character vector that can be modified through the gui and then make this the input to dropdown.Items. The problem is that from the codeview of my app, that is not a line of code I can modify.
The other option I thought of was activating the "allow users to type in text" function. But this won't remember the options that are typed in for future uses of the dropdown, which is something I would need.
Does anybody know how I can do it?
Thanks!
채택된 답변
TADA
2019년 7월 11일
I would put the options in a text file. Then load it during startup
댓글 수: 7
You can take it one step further
According to the documentation the ValueChangedData has a property that indicates if the value change was due to user editing.
So you can set a callback function in ValueChangedFcn, and check for user edited values, then append this option to the file if it doesn't exist already.
I never tried that, but if adding new options happens often, it would improve your user experience dramatically as opposed to having to update the text file every time
Thanks! But after I load the text file, how do I asign it to my dropdown.Items?
For the sake of this example, let's make it a fully extensible settings struct and not just a list of strings for your drop down options
That can be saved in XML or JSON formats very easily (let's use the leaner JSON format):
That's the content of the settings file ("settings.json") which is saved in the same folder as the app:
{"Options":["opt1","opt2","opt3"]}
it translates to a struct with one field using jsondecode, the same as writing this line of code:
struct('Options', {{'opt1', 'opt2', 'opt3'}})
ans =
struct with fields:
Options: {'opt1' 'opt2' 'opt3'}
I was curious if it would work, so I cooked up this small app:
classdef AppExample < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DropDownLabel matlab.ui.control.Label
DropDown matlab.ui.control.DropDown
Settings struct
SettingsFile = fullfile(fileparts(which('AppExample')), 'settings.json');
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% load settings
settingsStr = fileread(app.SettingsFile);
app.Settings = jsondecode(settingsStr);
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create DropDownLabel
app.DropDownLabel = uilabel(app.UIFigure);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [80 306 66 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.DropDown = uidropdown(app.UIFigure);
app.DropDown.Editable = true;
app.DropDown.Position = [161 306 100 22];
% listen to edit event
app.DropDown.ValueChangedFcn = @app.onDropDownValueChanged;
% bind options to drop down
app.DropDown.Items = app.Settings.Options;
end
function onDropDownValueChanged(app, src, edata)
% if this value changed is due to user editing the field
if edata.Edited
% add new option
app.addNewOption(edata.Value);
end
% handle value changed normally
end
function addNewOption(app, value)
% if this option did not exist before, add it to the settings struct and save it
if ~ismember(value, app.Settings.Options)
% add item to settings
app.Settings.Options{numel(app.Settings.Options)+1} = value;
% bind drop down elements
app.DropDown.Items = app.Settings.Options;
% save edited settings
settingsText = jsonencode(app.Settings);
fid = fopen(app.SettingsFile, 'w');
fwrite(fid, settingsText(:));
fclose(fid);
end
end
end
methods (Access = public)
% Construct app
function app = AppExample
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Now this app would load the options from the settings file at startup, but would also update the file whenever the user edits the options list
This is great! Thanks so much
TADA
2019년 7월 14일
Cheers
Hi there,
I am new to appdesigner, and for some reason I can't edit the grayed out code in the code view of my MatLab app to make the changes above. Any idea how I can edit the code? Thank you.
TADA
2020년 6월 12일
I think it would be best if you post a new question with this whole new problem
추가 답변 (0개)
카테고리
도움말 센터 및 File 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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
