assign operations for Drop down menu options

Here, I need to assign different operations for each drop down menu option. I have this code. I do not know how to connect the operation and the option. Can someone help me with this one? Thank you.
fig = uifigure;
dd = uidropdown(fig);
dd.Items = {'Alpha','Beta','Gamma'};
prompt = {'a :', 'b :', 'c :'};
dlgtitle = 'Parameters';
dims = [1 70];
definput = {'1','2 ','3'};
answer = inputdlg(prompt,dlgtitle,dims,definput)
a=str2double(answer{1});
b=str2double(answer{2});
c=str2double(answer{3});
%calculation for alpha
answer=a.^2+b.^2
%calculation for beta
answer=a.^2+c.^2

 採用された回答

Tommy
Tommy 2020 年 4 月 24 日

1 投票

You can access the selected option using the Value property of dd:
switch dd.Value
case 'Alpha'
%calculation for alpha
answer=a.^2+b.^2;
case 'Beta'
%calculation for beta
answer=a.^2+c.^2;
otherwise
answer=0;
end
If you want the input dialog box to show whenever the user changes the dropdown:
fig = uifigure;
dd = uidropdown(fig, 'ValueChangedFcn', @ddChanged);
dd.Items = {'Alpha','Beta','Gamma'};
ddChanged(dd);
function ddChanged(src, ~)
prompt = {'a :', 'b :', 'c :'};
dlgtitle = 'Parameters';
dims = [1 70];
definput = {'1','2 ','3'};
answer = inputdlg(prompt,dlgtitle,dims,definput);
a=str2double(answer{1});
b=str2double(answer{2});
c=str2double(answer{3});
switch src.Value
case 'Alpha'
%calculation for alpha
answer=a.^2+b.^2;
case 'Beta'
%calculation for beta
answer=a.^2+c.^2;
otherwise
answer=0;
end
disp(answer);
end

3 件のコメント

Dinu Th.
Dinu Th. 2020 年 4 月 24 日
Thank you for the answer. I think 1st one is more suitable for my case. Is there a way to close the dropdown menu after the selection. Next the input dialog box should appear. I have tried putting close command in between dropdown menu and the input box. But then I can not access dropdown box because both menu and box appear at that same time and the input box is on top of the drop down menu. Actually I wanted to include all of these things in a one window. But I did lots of searching and found out that I need to use GUI to do that. That is why I am trying to do it this way.
fig = uifigure;
dd = uidropdown(fig);
dd.Items = {'Alpha','Beta','Gamma'};
close %I need to close the drop down menu. Input box should appear next
prompt = {'a :', 'b :', 'c :'};
dlgtitle = 'Parameters';
dims = [1 70];
definput = {'1','2 ','3'};
answer = inputdlg(prompt,dlgtitle,dims,definput)
a=str2double(answer{1});
b=str2double(answer{2});
c=str2double(answer{3});
switch dd.Value
case 'Alpha'
%calculation for alpha
answer=a.^2+b.^2;
case 'Beta'
%calculation for beta
answer=a.^2+c.^2;
otherwise
answer=0;
end
Tommy
Tommy 2020 年 4 月 25 日
I'm not entirely sure what you mean. The dropdown should close after you select one of its options. As long as you create the input dialog box right after creating the uifigure/dropdown, it will appear and steal the focus from the figure/dropdown. To avoid this, I included the second piece of code above, where the input dialog box only shows after the user has selected an option from the dropdown. Using callbacks (i.e. functions) to control the behavior here is probably the easiest way to do what you want. When you create a uifigure and add a dropdown, you are essentially creating a GUI. What exactly are you trying to avoid?
Dinu Th.
Dinu Th. 2020 年 4 月 25 日
編集済み: Dinu Th. 2020 年 4 月 25 日
Sorry if it is confusing. I used the 1st code. Dropdown menu didnot disappear automatically after selecting an option. Instead it is running in the background. (But it does the work. calculations are correct.Thank you) Is there a way to automatically close the dropdown menu after selection. Or is there a way to put both dropdown menu and the dialog box in one window without using app designer. I have created a new question for that part.
Thank you.
Second code didn't work. It gave me this error.
Unrecognized function or variable 'ddChanged'.
Undefined function 'ddChanged' for input arguments of type 'matlab.ui.control.DropDown'.
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 378)
Error while evaluating DropDown PrivateValueChangedFcn.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

タグ

質問済み:

2020 年 4 月 23 日

編集済み:

2020 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by