我有同樣的需求,并寫了一個小腳本來做一些接近的事情.將以下內容放在
MATLAB desktop shortcut中.每當您單擊快捷方式按鈕時,它將從編輯器中的活動文件中刪除尾隨空格.不如在保存時自動執行它 – 你需要記住在保存之前按下按鈕 – 但差不多.測試在11b,12a和13b,但在12b也應該沒問題.
希望有所幫助!
% Temp variable for shortcut. Give it an unusual name so it's unlikely to
% conflict with anything in the workspace.
shtcutwh__ = struct;
% Check that the editor is available.
if ~matlab.desktop.editor.isEditorAvailable
return
end
% Check that a document exists.
shtcutwh__.activeDoc = matlab.desktop.editor.getActive;
if isempty(shtcutwh__.activeDoc)
return
end
% Get the current text.
shtcutwh__.txt = shtcutwh__.activeDoc.Text;
% Remove trailing whitespace from each line.
shtcutwh__.lines = deblank(regexp(shtcutwh__.txt,'[^\n]*(\n)|[^\n]*$', 'match'));
% Reconcatenate lines.
shtcutwh__.addNewline = @(x)sprintf('%s\n',x);
shtcutwh__.lines = cellfun(shtcutwh__.addNewline, shtcutwh__.lines, 'UniformOutput', false);
shtcutwh__.newtxt = horzcat(shtcutwh__.lines{:});
% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;
% Delete temp variable.
clear shtcutwh__