Eth: 0x00cce8E2e56a543abc084920eee3f88eFD0921ea

Sunday, March 4, 2012

If Statement in Matlab

How to do a double if statement in Matlab (have a process execute under 2 or more conditions):


Matlab 2011a (and other versions) is tricky program to code correctly. Sometimes you think you are doing everything right and you are not getting any warning messages but things are still broken. It is easy to do this with the IF statement. Recently I coded up a double or compound if statement and I thought it was working properly. I had the script:

However, Matlab 2011a will not check the second condition! Furthermore, it won't tell you there is a problem. The correct way to code this up to make sure both conditions are checked is to either brute force the problem by building in two if statements:

Or the more refined version, use the && operator to make sure both conditions are evaluated:

Read more on IF statements at Matlab documentation site.

Happy Coding
-Láscal

No comments: