matlab中ismember_MATLAB 代码格式化简易版
生活随笔
收集整理的這篇文章主要介紹了
matlab中ismember_MATLAB 代码格式化简易版
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
function formatMcodetry %#okdocument = matlab.desktop.editor.getActive;position = document.Selection;document.Text = format(document.Text);document.goToPositionInLine(position(1), position(2)) % 還原光標位置document.smartIndentContents();endfunction 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);% 字符串、注釋、數(shù)值中的符號以及一元操作符不處理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 ""], ''));
可以新建一個內(nèi)容為 "formatMcode" 的 Favorite Command, 并放在 quick access toolbar 中。之后可以用其快捷鍵對當前活動的 Editor 文本進行格式化。只適用于 .m 文件編輯,不適用 .mlx 文件。代碼有修改,不知道有沒有引入新的 Bug。
這個算是簡易版本,之前有看到過 GitHub 和 FEX 里有一些支持定制的功能更全面的 MATLAB Code beautifier,如果有更加復雜需求推薦使用那些
總結(jié)
以上是生活随笔為你收集整理的matlab中ismember_MATLAB 代码格式化简易版的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雷神se911怎么进入bois “雷神s
- 下一篇: 怎么样用pe调整分区大小 使用PE轻松分