Matlab code formatting similar to AStyle? [closed]

怎甘沉沦 提交于 2019-12-03 03:20:26

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!