Effacer les filtres
Effacer les filtres

While Loop Errors with Opperators

1 vue (au cours des 30 derniers jours)
Paul Nadeau
Paul Nadeau le 27 Avr 2020
Commenté : Paul Nadeau le 27 Avr 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

Réponses (2)

Walter Roberson
Walter Roberson le 27 Avr 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 commentaires
Walter Roberson
Walter Roberson le 27 Avr 2020
BoatStyle = str2double(inputdlg('Please enter the length of your boat'));
while isnan(BoatStyle) || BoatStyle < 9 || BoatStyle > 61
Paul Nadeau
Paul Nadeau le 27 Avr 2020
Thanks, got it to work after some tweeking

Connectez-vous pour commenter.


Deepak Gupta
Deepak Gupta le 27 Avr 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 commentaires
Paul Nadeau
Paul Nadeau le 27 Avr 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 le 27 Avr 2020
I got it to work after some tweeking, thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by