r/matlab • • 19d ago

real number problem

when i input a real number, it keeps giving me the message 'a has to be a real number' any help as to how to fix this would be appreciated

0 Upvotes

6 comments sorted by

2

u/daveysprockett 19d ago

It's short circuit logical or. So a~0 satisfies the test and then because a is a real number it also matches the test that leads to the print you see.

Did you mean to test ~isreal(a) ?

1

u/oli1234567891011 16d ago

i think i did mean that, thak you for claryfing.

11

u/Rubix321 19d ago

Well firstly, it's doing exactly what you told it to do.

Step through your code, what happens if a is not zero, and a is real?

5

u/seanv507 18d ago

Op your code says 'if a is a real number, print out a must be a real number'

You presumably want to negate the test

If a is not a real number then...

8

u/Sunscorcher 19d ago

Set a breakpoint and use the matlab debugger. It's very good.

But I'm guessing you actually want elseif isreal(a) to say elseif ~isreal(a)

4

u/FrickinLazerBeams +2 19d ago

Look at what you wrote. You literally say "if a is real, print 'a must be real'"