Question

Does anyone know of an existing method of automatically cleaning the indentation of a MATLAB/Octave script? I've got someone else's code (no, really!), and it's horrible - none of the loops or functions are indented, and half of the other lines are indented to apparently random depths.

The problem with MATLAB is that it doesn't use braces, so C++ style indenters aren't going to work. Python ones might, with a bit of modification, which I will try if I can't find a pre-existing solution.

Basically it'd just need to indent lines after lines starting with function, for, if, while... and un-indent lines starting with end*, I think...

Clarification: As pointed out by Jonas, MATLAB users can just select all, and ctrl+I to nicify the indentation. Unfortunately, I don't have access to the MATLAB editor, and it would also be nice to be able to auto-indent a batch of files all at once.

Was it helpful?

Solution

CTRL+A (to select all), followed by CTRL+I (to automatically indent) will do the trick in the Matlab editor.

OTHER TIPS

Ah, I should have known emacs and vi would have answers. I really should learn one of them. Anyway, I got frustrated with the work I was doing, and wrote this as a displacement activity. Remove the + '.test.m' to replace files:

#!/usr/bin/env python

import re, sys

def startswith(line=""):
    # these need some word-boundary condition, but \b isn't working
    ctrlstart = '\s*(function|if|while|for|switch)'
    ctrlcont = '\s*(elseif|else|case|catch|otherwise)'
    ctrlend = '\s*(end|endfunction|endif|endwhile|endfor|endswitch)'
    match = re.match(ctrlstart, line)
    if ( match != None ) :
        return ['start',  match.group(0)]
    match=re.match(ctrlcont, line) 
    if ( match!=None ) :
        return ['cont',  match.group(0)]
    match=re.match(ctrlend, line)
    if ( match!=None ) :
        return ['end',  match.group(0)]
    else :
        return [False,  None]

def main( filelist = list() ) :
    for filename in filelist:
        nextindent = 0
        indentmult = 2
        file = open(filename, 'r')
        filelines = file.readlines()
        for ind in range(0, len(filelines)) :
            indentlevel = nextindent
            match = startswith(filelines[ind])
            if match[0] == 'start' :
                nextindent += 1
            elif match[0] == 'cont' :
                indentlevel -= 1
            elif match[0] == 'end' :
                indentlevel -= 1
                nextindent -= 1
            elif match[0] == False :
                nextindent = indentlevel
            filelines[ind] = ' '*indentlevel*indentmult + filelines[ind].lstrip().rstrip() +'\n'
        outfile = open(filename + '.test.m', 'w')
        outfile.writelines(filelines)
        file.close()
        outfile.close()

args = []
for arg in sys.argv[1:] :
    args += [str(arg)]
main(args)

I tried the emacs way but it doesn't work, i am new to ubuntu and octave. So i took the easiest way :D, online site that indent the code for me and i can copy/paste the new clean code.

http://base-n.de/matlab/code_beautifier.html

Here is a vim plugin to automatically indent and syntax highlight for octave code

https://github.com/tranvansang/octave.vim

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top