2012-03-23 33 views
0

這是我的程序。如何捕捉用戶inputdlg()

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 

我的問題是如何重新顯示inputdlg()如果用戶按下「取消」,而不是「正常」?

非常感謝! :)

回答

2

您應該讓用戶有機會在任何步驟取消該程序。可能是詢問他/她是否真的想取消並可能丟失輸入的數據。

反正這裏是你如何做到這一點:

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

而且你沒有需要兩個listdlg聲明:

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

現在可以完美運行!非常感謝你!!! – user1279248 2012-03-23 21:56:31