简体   繁体   English

如何在inputdlg()中捕获用户

[英]How can I trap the user in inputdlg()

Here is my program. 这是我的计划。

clc
clear
ques='Yes';
while strcmp(ques,'Yes')
ns={'One','Two','Three','Four','Five'};
[selection ok]= listdlg('liststring',ns,'selectionmode','single');
while ok ==0
    msgbox('Please make a selection')
    [selection ok]= listdlg('liststring',ns,'selectionmode','single');
end
gradebook = {};
for d=1:selection
sinfo ={'Enter student name','Numerical grade for 1st exam (out of 100):','Numerical grade for 2nd exam (out of 100):','Numerical grade for 3rd exam (out of 100):'};
info=inputdlg(sinfo);
gradebook= [gradebook info];
end
for d=1:selection
    average(d)=mean(str2double(gradebook(2:end,d)));
end
[value where]=max(average);
name=gradebook {1,where};
msg=sprintf('%s has the highest average. Average grade is %.2f%%',name,value);
ok2=msgbox(msg);
waitfor(ok2)
ques=questdlg ('Do you want to repeat the program?');
end

My question is How can I redisplay the inputdlg() if the user press "cancel" instead of "ok"? 我的问题是如果用户按“取消”而不是“确定”,我如何重新显示inputdlg()?

Thank you very much! 非常感谢你! :) :)

You should give user a chance to cancel the program at any step. 您应该让用户有机会在任何步骤取消该程序。 May be with asking if he/she really wants to cancel and possibly loose the data entered. 可能会询问他/她是否真的要取消并可能丢失输入的数据。

Anyway here is how you do it: 无论如何这里是你如何做到的:

info = {};
while isempty(info)
    info=inputdlg(sinfo);
end

Also you don't need two listdlg statements: 此外,您不需要两个listdlg语句:

ok = 0;
while ok == 0
    [selection ok] = listdlg('liststring',ns,'selectionmode','single');
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM