MATLAB: Checking dialog box that gathers user input

checkdialogintegerwarning

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???

Best Answer

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.
Related Question