How can I reformat code in MATLAB editor?

While not fulfilling the "in the editor" part of your question, there is another MATLAB style checker / code formatter: https://github.com/florianschanda/miss_hit


I have created a minimalist, configurable MATLAB source code formatter, which can be used directly in the MATLABEditor.

You can find it on GitHub: MBeautifier on GitHub

MBeautifier in action

Main features:

  • Padding operators and keywords with white spaces
  • Configurable indentation character and level. Indentation using the Smart Indent functionality of the MATLAB Editor
  • Removal/addition of continuous empty lines
  • Inserting missing element separators (commas) in matrix and cell array initializations
  • Insert missing continuous symbol line in matrix and cell array initializations
  • In-lining continuous lines
  • Formats the current page of the MATLAB Editor or only a selection in the MATLAB Editor or file(s)
  • While everything above is configurable in a single XML file

It can be used in Matlab versions starting from R2013b.

Deployment and configuration are described on GitHub (link above).


A few default formatting shortcuts for the Matlab editor - assuming windows

ctrl+A - Select all
ctrl+I - Smart Indend
ctrl+= - Collapse All (loop control statements)
ctrl+shift+= Expand All (loop control statements)
ctrl+] - Increase indent
ctrl+[ - Decrease indent
ctrl+J - Wrap comments


Other useful keyboard shortcuts for Matlab editor

ctrl+R - Comment out selection (adds leading % to all selected lines)
ctrl+T - Uncomment selection (removes leading % from selected lines)
ctrl+K - Kill line (deletes from caret to end of line, if line is empty deletes newline)
ctrl+M - View or expand current message
ctrl+F1 - Display function hints


Additional shortcuts can be found or defined in Prefrences>Keyboard>Shortcuts

Finally a special mention to ... for line continuation...
example:

[output1,output2,output3]=calltoafunctionwithlotsofinputs(...
    intput1, input2, input3, input4, input5, ...
    'property1', propertyvalue1, ...
    'property2', propertyvalue2)