While Loop Errors with Opperators

조회 수: 1 (최근 30일)
Paul Nadeau
Paul Nadeau 2020년 4월 27일
댓글: Paul Nadeau 2020년 4월 27일
Can someone please tell me why I keep getting the error "Undefined operator '<' for input arguments of type 'cell' as well as if it works, it goes on an infinite loop and has to be stopped instead of inputting again?
Boat = menu('Would you like a recommended setup?','Yes','No');
if Boat == 2
Setup = msgbox('Okay, have a good day! Remember to visit myfwc.com for license and species regulations and check your local fishing and weather reports!')
elseif Boat == 1
BoatStyle = menu('Please select the style the most describes your boat:','Center Console','Viking','Flat Bottom','Yacht','Pontoon');
if BoatStyle == 1
BoatStyle = inputdlg('Please enter the length of your boat')
while isempty(BoatStyle) || BoatStyle < 9 || BoatStyle > 61
msgbox('Error. Please input realistic boat dimentions between 10ft - 60ft.')
if BoatStyle >= 10 && BoatStyle <= 60
msgbox('This is a good size boat.')
end
end
end
end

답변 (2개)

Walter Roberson
Walter Roberson 2020년 4월 27일
inputdlg does not return numeric values: it returns a cell array of character vectors. You need to use str2double to get the numeric equivalent.
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 4월 27일
BoatStyle = str2double(inputdlg('Please enter the length of your boat'));
while isnan(BoatStyle) || BoatStyle < 9 || BoatStyle > 61
Paul Nadeau
Paul Nadeau 2020년 4월 27일
Thanks, got it to work after some tweeking

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


Deepak Gupta
Deepak Gupta 2020년 4월 27일
Hi Paul,
As Walter has mentioned in his answer, inputdig returns data in cells and not numeric values. And therefor cell values can't be directly used with logical operator. You need to convert this cell value to a numerical before applying logical operator.
Apart from that, you are using a while loop to check conditions and if condition true, loop will run. But there is no condition to stop this loop so it will run continuosly. I think in this case, an if loop is sufficient.
BoatStyle = menu('Please select the style the most describes your boat:','Center Console','Viking','Flat Bottom','Yacht','Pontoon');
if BoatStyle == 1
BoatStyle = str2num(cell2mat(inputdlg('Please enter the length of your boat')))
if isempty(BoatStyle) || BoatStyle < 9 || BoatStyle > 61
msgbox('Error. Please input realistic boat dimentions between 10ft - 60ft.')
if BoatStyle >= 10 && BoatStyle <= 60
msgbox('This is a good size boat.')
end
end
end
  댓글 수: 2
Paul Nadeau
Paul Nadeau 2020년 4월 27일
It still isn't solving my problem. When it runs two things either happen. 1, it gives me the same error, or 2, it brings up my error message that I put but infinitly keeps looping and I have to Ctr C it to stop.
Paul Nadeau
Paul Nadeau 2020년 4월 27일
I got it to work after some tweeking, thanks

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by