while loop with OR statement

11 vues (au cours des 30 derniers jours)
Kamuran
Kamuran le 5 Déc 2015
Commenté : Kamuran le 9 Déc 2015
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 commentaire
Kamuran
Kamuran le 5 Déc 2015
Modifié(e) : Kamuran le 5 Déc 2015
Or simpler problem I am having. I decided to separate the while loop into two while loops. In the first one I am checking if the user enter a alphabetic character first.
% Asking user to input the variable Out1=input(['Enter the beginning point of the domain = '],'s'); Out2=input(['Enter the ending point of the domain = '],'s');
Out1=str2num(Out1); sK1=size(Out1); % if OUT1 is not a number, sK1 will be 0 by 0 sK1=sK1(1); Out2=str2num(Out2); sK2=size(Out2); % if OUT2 is not a number, sK2 will be 0 by 0 sK2=sK2(1); while (sK1==0) (sK2==0)
Out1=input(['Enter the starting point of the domain = '],'s');
Out2=input(['Enter the ending point of the domain = '],'s');
Out1=str2num(Out1);
sK1=size(Out1);
sK1=sK1(1);
Out2=str2num(Out2);
sK2=size(Out2);
sK2=sK2(1);
end
This does not work ..but if I check 1 by 1 it does work ..I dont understand why.
Any suggestion?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 5 Déc 2015
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.
  1 commentaire
Kamuran
Kamuran le 9 Déc 2015
Thank you. I will be able to modify it from your idea.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by