MATLAB: While loop with OR statement

while

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

Best Answer

Here is the snippet I use:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
It should be easy to modify to handle just one input instead of 2.
Related Question