while loop with OR statement
Show older comments
I am trying to ask a user input a real, positive number by using input function and checking the condition with a while loop. I am successful to check is the number is real and positive but I could not figure out how to correct if the user inputs a character.
K=input('Enter the K value = ', 's');
% I ask as string because if the user enters a character I want to give warning
%rather than code is not working.
K=str2num(K); % convert string to a number
sK=size(K); % if K is a character its size will be 0 0
sK=sK(1);
while (isnan(K)) || (any(imag(K)))|| ((K<=0))
disp('Number of domains input needs to be real and positive')
K=input('Enter the K value = ', 's');
K=str2num(K);
sK=size(K);
sK=sK(1);
end
% This while loop works but I can not check for if the input was a character
while (isnan(K)) || (any(imag(K)))|| ((K<=0)) || (sK==0) % get an error Operands to the || and && operators must be
%convertible to logical scalar values.
Any thoughts where I make a mistake?
(in the second while I can't write in the question , I dont know why. But I know there should be between statements) Thank you
1 Comment
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!