while loop with OR statement
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!