2012-12-28 71 views
0

假設我有以下代碼:需要幫助的澄清MATLAB的輸入對話框的

%%input dialog box%%% 
prompt = {'Enter gain:','Enter range:'}; 
dlg_title = 'Enter values'; 
num_lines= 1; 
def  = {'20','256'};  %default 
answer = inputdlg(prompt,dlg_title,num_lines,def); 

%%%to get the two entered values%%%% 
A = getfield(answer,{1}); %first input field 
A = str2double(A); 
B = getfield(answer,{2}); %second input field 
B = str2double(B); 

是什麼意思用「與結構,而不是getfield動態字段名」?

如何在輸入值複雜且小於零的情況下使用循環,從而向用戶請求其他兼容輸入?

我試過下面的循環,但它不起作用。爲什麼?

while isnan(A) || ~isreal(A) || A<0 
    prompt = {'Enter gain:'%'Enter range:'}; 
    dlg_title = {'undefine!!'}; 
    num_lines= 1; 
    def  = {'',''}%{'20','256'};  %default 
    answer = inputdlg(prompt, dlg_title, num_lines, def); 
    A = getfield(answer,{1}); %first input field 
    A = str2double(A); 
    %A = str2double(input('Enter the value of module(mm) : ', 's')); 
end 

回答

0

getfield的調用不是必需的,坦白說沒有任何意義。變量answercell陣列不是struct,這樣你就不需要使用getfield粘貼代碼下半年可以替換爲:

A = str2double(answer{1}); 
B = str2dobule(answer{2}); 

至於循環,直到你得到有效的輸入。您可以使用boolean標誌,while,環路和function(讓叫它areInputsValid())如果輸入有效

validInput = false; 

while (~validInput) 
    % input dialog box 
    % code here 

    % get the two entered values 
    & code here 

    validInput = areInputsValid(A, B); 
end 
+0

我不是很清楚與循環你能提供更具體的例子,則返回true ?非常感謝你@slayton – green