MATLAB Answers

While Loop Errors with Opperators

3 views (last 30 days)
Paul Nadeau
Paul Nadeau on 27 Apr 2020
Commented: Paul Nadeau on 27 Apr 2020
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

  0 Comments

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 27 Apr 2020
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 Comments

Walter Roberson
Walter Roberson on 27 Apr 2020
BoatStyle = str2double(inputdlg('Please enter the length of your boat'));
while isnan(BoatStyle) || BoatStyle < 9 || BoatStyle > 61
Paul Nadeau
Paul Nadeau on 27 Apr 2020
Thanks, got it to work after some tweeking

Sign in to comment.


Deepak Gupta
Deepak Gupta on 27 Apr 2020
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 Comments

Paul Nadeau
Paul Nadeau on 27 Apr 2020
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 on 27 Apr 2020
I got it to work after some tweeking, thanks

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!