checking dialog box that gathers user input
5 次查看(过去 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 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!