MATLAB Matlab help with if conditional

  • Thread starter Thread starter astronut555
  • Start date Start date
  • Tags Tags
    Conditional Matlab
AI Thread Summary
The discussion revolves around the use of an "if" statement in MATLAB without a typical conditional expression. The code snippet presented uses "if 0," which effectively prevents the enclosed commands from executing. This approach allows the programmer to easily toggle the execution of the code by changing the zero to one, indicating that the plot command will only run if the condition evaluates to true. This technique is often used during debugging, where a developer may want to keep the code for future use without executing it at the moment. The practice highlights a method for maintaining code flexibility while simplifying the debugging process.
astronut555
Messages
18
Reaction score
1
Hello all,

I'm reading through some code that someone else wrote and have come across the following conditional:

if 0

figure; plot(times, average);

end;


I've never seen if used without a statement to test (ie: if x>0...) and I can't find any documentation on it anywhere.

Anyone seen this before and know what it does?

Thanks!
 
Physics news on Phys.org
Matlab's "if" command bases its decision on whether the next object is true or false, and this can be expressed also by 1 or 0. You are used to seeing an expression such as x>0, but this will evaluate to either true/1 (if the condition is true) or false/0 (otherwise). Your code might have had such a test at one time but it was replaced by 0 to force the plot command not to execute. Its possible the programmer added this plot during debug with a 1, then changed it to 0 when debug was over instead of removing the code. That way he/she can always reactivate the plot by changing one character.
 
makes sense, thanks!
 

Similar threads

Replies
4
Views
4K
Replies
5
Views
3K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
4
Views
1K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
9
Views
5K
Replies
2
Views
3K
Back
Top