Is there any tool similar to AStyle to format matlab code in m-files?
Select MATLAB > Editor/Debugger > Language, and adjust the Comment formatting preferences. To adjust Comment formatting preferences in MATLAB Online, select Editor/Debugger > MATLAB Language.
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.
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
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With