Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab code formatting similar to AStyle? [closed]

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

like image 813
Chesnokov Yuriy Avatar asked Oct 22 '11 16:10

Chesnokov Yuriy


People also ask

How do you auto format a script in Matlab?

Select MATLAB > Editor/Debugger > Language, and adjust the Comment formatting preferences. To adjust Comment formatting preferences in MATLAB Online, select Editor/Debugger > MATLAB Language.

How do you correct a code in Matlab?

If you are unfamiliar with the problem, right-click the highlighted code. The first item in the context menu shows the suggested fix. Select the item to apply the fix. If multiple instances of a problem exist, MATLAB might offer to apply the suggested fix for all instances of the problem.


2 Answers

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
like image 134
Amro Avatar answered Nov 15 '22 16:11

Amro


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.)

like image 39
cyborg Avatar answered Nov 15 '22 17:11

cyborg