Matlab code formatting similar to AStyle? [closed]
Asked Answered
L

2

8

Is there any tool similar to AStyle to format matlab code in m-files?

Litton answered 22/10, 2011 at 16:43 Comment(1)
I know it is an old question, but if you are interested, see my answer on this question: #23961322Baggage
M
6

In recent versions of MATLAB, you can use the "Smart Indent" tool programmatically using the MATLAB Editor API.

As an example, say you want to fix indentation of all M-files contained in a specific directory:

%# gel list of m-files in a directory
BASE_DIR = 'c:\path\to\folder';
files = dir( fullfile(BASE_DIR,'*.m') );
files = {files.name};

for i=1:numel(files)
    %# open file in editor, apply smart indentation, save and close
    doc = matlab.desktop.editor.openDocument( fullfile(BASE_DIR,files{i}) );
    doc.smartIndentContents;
    doc.save;
    doc.close;
end
Maldives answered 22/10, 2011 at 20:47 Comment(1)
thanks for the smart snippet. I'd like to format whitespace alsoLitton
W
1

Remember that you can select text in Matlab's editor and press Ctrl+I to auto-indent it. (Also , use Ctrl+A to select all the text.)

Waft answered 22/10, 2011 at 17:18 Comment(1)
mathworks.com/matlabcentral/answers/…Litton

© 2022 - 2024 — McMap. All rights reserved.