我是靠谱客的博主 成就荷花,这篇文章主要介绍规范Matlab代码,现在分享给大家,希望可以做个参考。

以下程序用于规范Matlab代码,仅为备用。
原作者:打浦桥程序员

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function formatMcode try %#ok document = matlab.desktop.editor.getActive; position = document.Selection; document.Text = format(document.Text); document.goToPositionInLine(position(1), position(2)) % 还原光标位置 document.smartIndentContents(); end function code = format(code) code = regexprep(code, {'(S)[ fvt]+n', '([^n])$'}, '$1n'); tree = mtree(code, '-comments'); assert(isempty(tree.mtfind('Kind', 'ERR'))) % a(:) 不替换为 a( : ) colons = tree.mtfind('Kind', 'COLON'); colonPos = colons.position(); colonPos = colonPos(colons.lefttreepos == colons.righttreepos); % 字符串、注释、数值中的符号以及一元操作符不处理 exc = tree.mtfind('Kind', 'CHARVECTOR') | tree.mtfind('Kind', 'STRING') | ... tree.mtfind('Kind', 'DOUBLE') | tree.mtfind('Kind', 'COMMENT') | ... tree.mtfind('Kind', 'UPLUS') | tree.mtfind('Kind', 'UMINUS'); excPos = [colonPos' ... cell2mat(arrayfun(@colon, exc.lefttreepos, exc.righttreepos, 'un', 0)')]; [ops, code, start] = regexp(string(code), ... '(.(^|*|/|\)|<=|>=|==|~=|&&||||+|-|*|\|/|^|||&|<|>|=|,|;|:)', ... 'match', 'split', 'start'); idx = ~ismember(start, excPos); code(idx) = regexprep(code(idx), '[ frtv]+$', ''); code([false idx]) = regexprep(code([false idx]), '^[ frtv]+', ''); ops(idx) = ops(idx) + " "; idx = idx & ~ops.startsWith(["," ";"]); ops(idx) = " " + ops(idx); code = char(strjoin([code; ops ""], ''));

最后

以上就是成就荷花最近收集整理的关于规范Matlab代码的全部内容,更多相关规范Matlab代码内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部