How do I index correctly from my questdlg command to my if statement? I am trying to direct the user into different code based on their questdlg responses. I have many more in this code but I am stuck HELP!

8 ビュー (過去 30 日間)
quest = 'What type of rod are you using?';
qtitle = 'Axial load problem';
a = {'Circular', 'Rectangular'};
resp=questdlg(quest,qtitle,a{1},a{2},a{1});
h=msgbox(sprintf('You selected %s',resp));
respa = str2num('resp');
waitfor(h);
if respa == 1
% create a user prompted space to calculate the axial load problem for a
% circular rod
dlg_prompts = {'Axial load','Radius','Inner Radius',...
'Length','Young''s Modulus E'};
dlg_title = 'Axial Load';
dlg_defaults = {'0','0','0','0','0'};
opts.Resize = 'on'; % a structure
dlg_ans = inputdlg(dlg_prompts,dlg_title,1,dlg_defaults,opts);
P = str2double(dlg_ans(1));
r = str2double(dlg_ans(2));
ir = str2double(dlg_ans(3));
L = str2double(dlg_ans(4));
E = str2double(dlg_ans(5));
else
% create a user prompted space to calculate the axial load problem for a
% rectangular rod
dlg_prompts = {'Axial load','Base','Height','Inner Base','Inner Height',...
'Length','Young''s Modulus E'};
dlg_title = 'Axial Load';
dlg_defaults = {'0','0','0','0','0','0','0'};
opts.Resize = 'on'; % a structure
dlg_ans = inputdlg(dlg_prompts,dlg_title,1,dlg_defaults,opts);
P = str2double(dlg_ans(1));
b = str2double(dlg_ans(2));
h = str2double(dlg_ans(3));
ib = str2double(dlg_ans(4));
ih = str2double(dlg_ans(5));
L = str2double(dlg_ans(6));
E = str2double(dlg_ans(7));
end%if

採用された回答

Kelly Kearney
Kelly Kearney 2013 年 12 月 5 日
Since you know the possible strings in advance, a switch/case block might be better:
quest = 'What type of rod are you using?';
qtitle = 'Axial load problem';
a = {'Circular', 'Rectangular'};
resp=questdlg(quest,qtitle,a{1},a{2},a{1});
switch resp
case 'Circular'
% circular code here
case 'Rectangular'
% rect code here
end
Or if you must number it...
[tf, respa] = ismember(resp, a)
  1 件のコメント
Sean
Sean 2013 年 12 月 5 日
Thank you so much!! I am still very much a novice and would have never thought of a switch/case block.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by