2012-12-29 101 views
2

如何避免輸入對話框直接由用戶關閉而不輸入任何值?對於「菜單」功能,我們可以使用選項== 0來選擇未選擇的選項,但輸入對話框怎麼樣?如何防止輸入對話框在matlab中關閉?

prompt = {'Enter gain:','Enter range:'};

dlg_title ='輸入值';

num_lines = 1;

def = {'20','256'}; %默認

答案= inputdlg(提示,dlg_title,NUM_LINES,DEF);

%%%獲得兩個輸入值%%%%

%A = getfield命令(回答,{1}); %第一輸入字段

A = str2double(回答{1});

%B = getfield(answer,{2}); %第二個輸入字段

B = str2double(answer {2});

假設我有一個輸入對話框,如圖,我怎麼可以把它用在完整地循環

回答

1

不能防止它被封閉的模式,但你可以使用一個while循環重新打開它,直到用戶已經輸入了有用的值。

done = false; 
while ~done 
    a=inputdlg('enter a number') 
    num = str2double(a{1}); %# will turn empty and strings into NaN 
    if isnumeric(num) 
     done = true; 
    else 
     %# keep running while loop 
     %# you can pop up an errordlg box here to tell the user what was wrong. 
     %# It would be nice if you were to set the inputs that passed the test 
     %# as defaults for the next call of inputdlg. Nothing sucks as much 
     %# as having to completely re-fill a form because of a small typo 
    end 
end 
+0

我還不是很清楚如何創建循環。我編輯了我的問題,你可以給我一個援助之手..謝謝你這麼多........ @Jonas – green

+0

在我怎麼能夠提示有關對話框中的輸入不符合要求的錯誤消息一定的條件並阻止整個對話框被執行,直到所有的輸入滿足一定的條件@Jonas – green