Using inputdlg and isnan

조회 수: 21 (최근 30일)
Gregory Hind
Gregory Hind 2020년 5월 2일
댓글: Gregory Hind 2020년 5월 2일
I need a user input using inputdlg, I need it to be a number and I need it to keep assking until I get a number. So far, it just gets stuck in a loop.
Please help, this should be easy!
nof=inputdlg('Enter number of floors:');
NOF=str2double(nof);
while isempty(NOF)||isnan(NOF)
disp('error')
nof=inputdlg('Enter number of floors:');
uiwait;
end
  댓글 수: 1
Gregory Hind
Gregory Hind 2020년 5월 2일
that's excellent, thank you

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

채택된 답변

Tommy
Tommy 2020년 5월 2일
In your code, NOF never changes inside the loop. Therefore, if the loop enters, it won't ever exit because the exit condition will never be true. Make sure you are calling str2double within the loop:
res=inputdlg('Enter number of floors:');
nof=str2double(res);
while isempty(res) || isnan(nof)
% ^ user canceled ^ input was not numeric
disp('error')
res=inputdlg('Enter number of floors:');
nof=str2double(res);
end
If you want to let the user use the command line:
opts.WindowStyle = 'normal';
res=inputdlg('Enter number of floors:', '', 1, {''}, opts);
nof=str2double(res);
while isempty(res) || isnan(nof)
disp('error')
res=inputdlg('Enter number of floors:', '', 1, {''}, opts);
nof=str2double(res);
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by