MATLAB Language MATLAB Best Practices Indent code properly

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Proper indentation gives not only the aesthetic look but also increases the readability of the code.

For example, consider the following code:

%no need to understand the code, just give it a look
n = 2;
bf = false;
while n>1
for ii = 1:n
for jj = 1:n
if ii+jj>30
bf = true;
break
end
end
if bf
break 
end
end
if bf
break 
end
n = n + 1;
end

As you can see, you need to give a careful look to see which loop and if statements are ending where.
With smart indentation, you'll get this look:

n = 2;
bf = false;
while n>1
    for ii = 1:n
        for jj = 1:n
            if ii+jj>30
                bf = true;
                break
            end
        end
        if bf
            break
        end
    end
    if bf
        break
    end
    n = n + 1;
end

This clearly indicates the starting and ending of loops/if statement.

You can do smart indentation by:
selecting all your code (Ctrl+A)
and then pressing Ctrl+I or clicking SmartIndentIcon from edit bar. Editor



Got any MATLAB Language Question?