Traping Error user input

n=inputdlg('Please input a number more than or equal one:')
I would like to trap the user if they input anything other than numbers more than 1. I have tried loop,isnumeric but they all don't work overally.
Any ideas?
Thank you

 채택된 답변

Honglei Chen
Honglei Chen 2012년 4월 13일

0 개 추천

Is this what you want?
n=inputdlg('Please input a number more than or equal one:')
while isempty(str2num(n{1})) || str2num(n{1})<1
n=inputdlg('Please input a number more than or equal one:')
end

댓글 수: 5

Rooy
Rooy 2012년 4월 13일
This way doesn't really work
n =
'b'
Undefined function 'isnumeic' for input arguments of type 'cell'.
Error in Untitled23 (line 2)
while ~isnumeic(n) || n < 1
If I extract from the cell array it would still not work
Honglei Chen
Honglei Chen 2012년 4월 13일
I didn't realize you are using inputdlg and I had a typo. I updated the answer
Rooy
Rooy 2012년 4월 13일
Thank you it works.
Rooy
Rooy 2012년 4월 14일
What if I have more than one input
for instance
n=inputdlg({'Please input a number more than or equal one:';'P'})
while isempty(str2num(n{1})) || str2num(n{1})<1 || isempty(str2num(n{2})) || str2num(n{2})<1
h = errordlg;
waitfor(h)
n=inputdlg({'Please input a number more than or equal one:';'P'})
end
I can't just keep hard coding the loops based on the the amount of inputs. Is there another way
Honglei Chen
Honglei Chen 2012년 4월 15일
n=inputdlg({'Please input a number more than or equal one:';'P'})
while any(cellfun(@isempty,cellfun(@str2num,n,'UniformOutput',false))) || ...
any(cellfun(@lt,cellfun(@str2num,n,'UniformOutput',false),...
num2cell(ones(numel(n),1))))
h = errordlg;
waitfor(h)
n=inputdlg({'Please input a number more than or equal one:';'P'})
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 4월 13일

0 개 추천

Try this:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Initialize flag to keep going or not.
keepGoing = true;
userPrompt = 'Enter a number greater than 1';
defaultValue = 42;
while keepGoing
% Ask user for a number.
caUserInput = inputdlg(userPrompt, 'Enter the number',1,{num2str(defaultValue)});
usersValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(usersValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
break;
end
% See if it's > 1
if usersValue > 1
message = sprintf('Success!\n you entered %f.\n', usersValue);
keepGoing = false; % Bail out
else
message = sprintf('Try again. You entered %f.\n%s', ...
usersValue, userPrompt);
% Keep going.
end
uiwait(msgbox(message));
end

댓글 수: 4

Rooy
Rooy 2012년 4월 13일
It stalls when I enter the right input after entering a few bad ones.
Thank you
Rooy
Rooy 2012년 4월 13일
Where could I get to reprompt the user to enter again?
Thank you
Image Analyst
Image Analyst 2012년 4월 14일
It does not stall. It works fine. I just copied it and ran it again. I was able to enter lots of numbers less than 1 and it kept reprompting the user to enter a number >1. When I finally did enter a number > 1, it exited the while loop. It does exactly what you asked for. If it doesn't, then you must have altered it and broken it.
Rooy
Rooy 2012년 4월 17일
Thank you for all the help

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2012년 4월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by