checking dialog box that gathers user input
조회 수: 6 (최근 30일)
이전 댓글 표시
I want to create a dialog box for user to input at the beginning of my GUI (not showing the GUI page). When the user input is integer, GUI page will show and go on. When the user input is not integer, a warning dialog will show and require user to input again until he input the integer. My code is here:
prompt = {'Enter number of Day:','Enter number of Month:'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {' ',' '};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
if ~all(ismember(answer, '.1234567890'))
uiwait(warndlg('Please enter an integer!','!! Warning !!'))
end
Where should I put these coding? As I put them before "Begin initialization code", the dialog will occur repeatedly when I execute other button in GUI. I also can not let user input again after the warning occurred. Can you please tell me why???
댓글 수: 0
채택된 답변
Image Analyst
2016년 10월 16일
Lucky for you, I already have a code snippet for you.
% Ask user for one integer number.
defaultValue = 45;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nTry replacing the user.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
Feel free to adapt as needed.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!