Question

I don't find an essential feature in Matlab 2012b:

Remove trailing whitespaces on save.

Related:

How to auto-remove trailing whitespace in Eclipse?

Aptana 3 - How remove trailing whitespaces on save

Was it helpful?

Solution

I had the same need, and wrote a little script to do something close. Put the following in a MATLAB desktop shortcut. Whenever you click the shortcut button, it will strip trailing whitespace from the active file in the editor. Not quite as good as automatically doing it on save - you need to remember to press the button before saving - but nearly. Tested on 11b, 12a, and 13b, but should also be fine on 12b.

Hope that helps!

% 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__

OTHER TIPS

I don't have enough reputation to comment, but I created a Gist on github, to update Sam Roberts' answer:

It will remember your last cursor position/selection and select back after removing the trailing whitespace.

I also made it remove all trailing blank lines at the end of the editor.

I find it quite useful for longer files

https://gist.github.com/hmaarrfk/8462415

% To remove a Matlab trailing whitespace in the editor
% Original Author: Sam Roberts 
% http://stackoverflow.com/questions/19770347/how-to-auto-remove-trailing-whitespaces-on-save-in-matlab
% Modified by Mark Harfouche to remember cursor location
%
%
% 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

% save the old cursor location
shtcutwh__.Selection = shtcutwh__.activeDoc.Selection;

% 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'));

% remove the trailing blank lines
for n = length(shtcutwh__.lines):-1:1
    if length(shtcutwh__.lines{n}) == 0
        shtcutwh__.lines(n) = [];
    else
        break
    end
end

% Reconcatenate lines.
shtcutwh__.addNewline = @(x)sprintf('%s\n',x);

shtcutwh__.lines = cellfun(shtcutwh__.addNewline, shtcutwh__.lines, 'UniformOutput', false);

% If you always want to add a newline at the end of the file, comment this line out
% Remove the last newline character
shtcutwh__.lines{end}(end) = ''; 

shtcutwh__.newtxt = horzcat(shtcutwh__.lines{:});

% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;

% Place the cursor back
shtcutwh__.activeDoc.Selection = shtcutwh__.Selection;

% Delete temp variable.
clear shtcutwh__
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top