Error when using isempty in a while loop
33 views (last 30 days)
Hi, I'm trying to make a while loop using two conditions separated by an OR. The loop should continue for as long as 'a' is zero or the user hits enter before typing anything.
Edit: the input is supposed to be a number, not a string.
a=input('Please enter "a" :');
while (a==0 || isempty(a))
a=input('\nCan''t do that m8, try again :');
>>Operands to the || and && operators must be convertible to logical scalar values.
I also tried using isempty(a)==1, but that didn't work either.
Any help would be greatly appreciated.
Cedric Wannaz on 6 Oct 2017
Edited: Cedric Wannaz on 6 Oct 2017
This thread should answer you question about what your variable a is and how that works:
Just a few extra hints:
>> 123 == 0
>> 'hello' == 0
1×5 logical array
0 0 0 0 0
>>  == 0
0×0 empty logical array
as you can see, the test a==0 can produce outputs of various sizes.
OCDER on 6 Oct 2017
One issue you'll find is that the input will error out for number inputs such as: "1 2 3 4". If you only want string inputs, use input('your message', 's'). Assuming you want to ask for 'a' indefinitely until the user types in 'a', then you could do this instead:
a = input('Please enter "a" : ', 's');
while ~strcmp(a, 'a')
a = input('Can''t do that m8, try again : ', 's');