2011-05-27 113 views
1

嘿所有, 我正在構建一個GUI,有一個編輯框,等待用戶寫一個名字。在matlab GUI中的用戶輸入

目前我強迫用戶給出一個合法的名稱與此代碼:

NewPNUName = get(handles.nameOfNewPNU, 'String'); 
if (isempty(NewPNUName) ||... 
     strcmp(NewPNUName,'Enter the name for the new PNU')) 
    errordlg('Please enter a name for the new PNU.'); 
elseif (~ischar(NewPNUName(1))) 
    errordlg('The PNU name should start with a letter.'); 
else 
    handles.NewPNUName = NewPNUName; 
end 

if (~isempty(handles.NewPNUName)) 
% Do all the things needed if there is a legit name 
end 

它的作用是什麼,如果用戶沒有寫一個合法的名稱。 我想要做的是做一個帶編輯框的彈出框,要求用戶再次輸入想要的名字,直到它是一個合法的名字。

感謝您的幫助!

編輯: 以下@woodchips建議我糾正我的代碼到foloowing:

NewPNUName = get(handles.nameOfNewPNU, 'String'); 
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&... 
    ~strcmp(NewPNUName,'Enter the name for the new PNU'); 
while (~ValidName) 

    if (isempty(NewPNUName) ||... 
      strcmp(NewPNUName,'Enter the name for the new PNU')) 
     NewPNUName = char(inputdlg('Please enter a name for the new PNU.','No name entered')); 
    elseif (~isletter(NewPNUName(1))) 
     NewPNUName = char(inputdlg('The name of the new PNU should start with a letter. Please enter a new name',... 
      'Invalid name entered')); 
    else 
     allConds = 'are met' 
    end 

    ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&... 
     ~strcmp(NewPNUName,'Enter the name for the new PNU'); 
end 

回答

1

所以,把一個while循環周圍的代碼塊,產生一個inputdlg箱。將while循環的條件設置爲結果是有效的。

+0

謝謝!我現在相應地更新了我的代碼。 – Yarok 2011-05-27 11:56:21