How can I reformat code in MATLAB editor?
Solution 1
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)
Solution 2
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
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).
Solution 3
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
Related videos on Youtube

Hadi Momenzadeh
Updated on September 15, 2022Comments
-
Hadi Momenzadeh 9 months
How can I reformat code in MATLAB editor? Other IDEs usually have reformat or format option that arrange position of written codes. Does editor of MATLAB have it? I use MATLAB R2013a
-
RTL about 9 yearsI really couldn't resist the opportunity to use them... also to validate the kind of not great I really wish I had access to this list when I was starting... (or knew where to find it)
-
zhangxaochen over 7 yearsFYI, some tips from the doc: cn.mathworks.com/help/matlab/matlab_prog/…
-
stijn over 6 yearsThis should probably be the accepted answer for most 'format matlab code' questions on here :P
-
nbro over 5 yearsIt's a pain to have to remember and retype commands on the command window to format files. Also, your tool must be on the search path. In other words, you may want somehow to improve the usability of your tool.
-
DVarga over 5 yearsI've created an issue on GitHub: github.com/davidvarga/MBeautifier/issues/47 . The idea is to make it able to add shortcuts to the formatting methods automatically, which add MBeauty to the Matlab path and calls the appropriate method. I hope the update will fulfill your needs.
-
stijn over 5 years@nbro you don't have to remember the entire command. Type them once, jthen afterwards ust type the starting letters like
Mbea
and hit the up arrow. -
p8me over 5 yearsThe original Matlab shortcuts explained in the accepted answer do not format the code as of Matlab R2017b, unfortunately. The closest action to format is auto-indent (Ctrl+I) which does not format any spacing withing the lines. The package presented in this answer though does format. It also provides a shortcut feature. Thanks for the effort!