if and elseif statement with unexpected error that I cannot figure out

2 views (last 30 days)
I am trying to a) Create a script with if & elseif statements that will output one of three statements given three potential conditions:
1) If X & Y are < 3 but > 0: output the statement ‘X and Y are Less than 3 but greater than 0’.
2) If X and Y are less than 0: output the statement ‘X and Y are negative’
3) If X or Y are > 3: output ‘Either X or Y is greater than 3’. And lastly if none of the above conditions are met, output ‘None of the three conditions have been met’.
Where x=-0.5, y=-2
my script:
>> x=-0.5,y=-2
if x < 3 && y < 3 && x >0 && x>0
disp (X and Y are Less than 3 but greater than 0)
elseif X<0 && Y<0
disp (X and Y are negative)
elseif X>3 || Y>3
disp (Either X or Y is greater than 3)
else
disp (None of the three conditions have been met)
end
x =
-0.5000
y =
-2
My display is wrong:
disp (X and Y are Less than 3 but greater than 0)
|
Here is my error:
Error: The input character is not valid in MATLAB statements
or expressions.
  2 Comments
Elizabeth
Elizabeth on 16 Sep 2012
I have done your suggestions and still get the same error >> x=-0.5, y=-2 if x<3 && y<3 && x>0 && y>0 disp (‘x and y are less than 3 but greater than 0’) elseif x<0 && y<0 disp (‘x and y are negative’) elseif x>3 y>3 disp (‘Either x or y is greater than 3’) else disp (‘None of the three conditions have been met’) end
x =
-0.5000
y =
-2
disp (x and y are less than 3 but greater than 0)
|
Error: The input character is not valid in MATLAB statements
or expressions.
Matt Fig
Matt Fig on 16 Sep 2012
Please Highlight ALL of your code and then hit the {} Code button.

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 16 Sep 2012
Edited: Matt Fig on 16 Sep 2012
Look carefully at what you wrote! In the first conditional line, you check if x is greater than zero twice. In the second and third conditional line you use capital x and capital y. MATLAB is case sensitive. X is not the same as x, and Y is not the same as y.
Finally, you are to only use a single quote in MATLAB strings...
disp('A correct MATLAB string...')
Fix these things and you will be fine.
.
.
.
.
EDIT
You are still using some strange thing instead of a single quote. Look at this one:
>> x=-0.5, y=-2
if x<3 && y<3 && x>0 && y>0
disp ('x and y are less than 3 but greater than 0')
elseif x<0 && y<0
disp ('x and y are negative')
elseif x>3 || y>3
disp ('Either x or y is greater than 3')
else
disp ('None of the three conditions have been met')
end
x =
-0.5000
y =
-2
x and y are negative

More Answers (0)

Categories

Find more on Entering Commands 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!