Code covered by the BSD License  

Highlights from
Save a copy of current active *.m file in editor adding time mark.

Save a copy of current active *.m file in editor adding time mark.

by

 

08 Sep 2010 (Updated )

Current *.m file in editor is saved with name suffixed by '_data_time' under the same directory

savem_z
function savem_z
% SAVEM_Z saves a copy of the current active *.m file in editor, with the
% file name suffixed by '_data_time' string under the same directory.
%
% Syntax:
%       savem_z
% tested for MATLAB 7.4.0 (R2007a), 7.9.0 (R2009b), 7.11.0 (R2010b),
% 7.12.0 (R2011a), 7.13.0 (R2011b)
% on UNIX and PC platforms
%
% Description:
%   Too simple to be stated here.
%
% Programmed and Copyright by J. Zhang: xyzhangj@physics.ucla.edu
%
% $Version=2.1 $Date=2012/02/21
%   1. Tested for MATLAB 7.13.0 (R2011b) & 7.4.0 (R2007a)
%   2. Add the switch section to detect MATLAB version and use the
%   corresponding code section
%   3. Add the detection of UNIX or PC 
%
% $Version=1.1 $Date=2011/08/09
%   Make modifications for MATLAB 7.12.0 (R2011a)
%   Changes: 
%       class name: matlab.desktop.editor.Document
%       class method calling format: class.mathod  
%
% $Version=1.2 $Date=2011/10/26
%   Make modifications for MATLAB 7.11.0 (R2010b)
%
% $Version=0.3 $Date=2010/10/24
%   backup file is saved under the ./backup directory
%
% $Version=0.2 $Date=2010/09/08
% bug fixed:
%   backup file is named as *_yyyymmdd_104.m @time 10:04 AM
%   it should be named as *_yyyymmdd_1004.m instead
%
% $Version=0.1 $Date=2010/07/25
%

% get the 1st and 2nd version numbers
[ver_1,remain] = strtok(version, '.');
ver_2 = strtok(remain,'.');

if str2num(ver_1) < 7
    error(' Sorry!\n The current SAVEM_Z has not included the support for MATLAB version earlier than 7.');
else
    switch str2num(ver_2)
        case {4}
            %% For 7.4.0 (R2007a)
            % Referred to
            % http://www.mathworks.com/matlabcentral/fileexchange/12323-insertheader/content/InsertHeader.m
            % 'EditorCurrentFile' function--Thank you, Dan K.
            % Define the handle for the set of java commands:
            desktopHandle = com.mathworks.mlservices.MatlabDesktopServices.getDesktop;
            % Determine the last selected document in the editor:
            lastDocument = desktopHandle.getLastDocumentSelectedInGroup('Editor');
            % Strip off the '*' which indicates that it has been modified.
            Filename_z = strtok(char(desktopHandle.getTitle(lastDocument)),'*');
            es = com.mathworks.mlservices.MLEditorServices;
            javaMethod('saveDocument',es,Filename_z);
            
        case {9}
            %% For 7.9.0 (R2009b)
            es = com.mathworks.mlservices.MLEditorServices;
            Filename_z = char(es.builtinGetActiveDocument);
            javaMethod('saveDocument',es,Filename_z);
            
        case {11}
            %% For 7.11.0 (R2010b)
            getActive_z = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor;
            Filename_z = char(getActive_z.getLongName);
            getActive_z.negotiateSave;
            
        case {12, 13}
            %% For 7.12.0 (R2011a) / 7.13.0 (R2011b)
%             % Actually the following section of acquiring the active *.m
%             file still works in 7.12.0--but keep the old methods
%             since the chance for testing in previous versions of MATLAB
%             does not exist anymore.
%
%             % Referred to
%             % http://www.mathworks.com/matlabcentral/fileexchange/12323-insertheader/content/InsertHeader.m
%             % 'EditorCurrentFile' function
%             % Define the handle for the set of java commands:
%             desktopHandle = com.mathworks.mlservices.MatlabDesktopServices.getDesktop;
%             % Determine the last selected document in the editor:
%             lastDocument = desktopHandle.getLastDocumentSelectedInGroup('Editor');
%             % Strip off the '*' which indicates that it has been modified.
%             Filename_z = strtok(char(desktopHandle.getTitle(lastDocument)),'*');

            getActive_z = matlab.desktop.editor.getActive;
            Filename_z = char(getActive_z.Filename);
            getActive_z.save;
            
        otherwise
            error(' Sorry!\n The current SAVEM_Z has not included the support for this MATLAB version.\n You might want to tweak it a little bit to make it work.');
            
    end
end


timevec = clock;
timestr = sprintf('_%s_%02d%02d',datestr(date,'yyyymmdd'),timevec(4),timevec(5));

if isunix
    slashes = strfind(Filename_z,'/');
    directory_backup = sprintf('%sbackup/',Filename_z(1:slashes(end)));
    filename_backup = sprintf('%s%s%s.m',directory_backup,Filename_z(slashes(end)+1:end-2),timestr);
elseif ispc
    backslashes = strfind(Filename_z,'\');
    directory_backup = sprintf('%sbackup\\',Filename_z(1:backslashes(end)));
    filename_backup = sprintf('%s%s%s.m',directory_backup,Filename_z(backslashes(end)+1:end-2),timestr);
end

if (exist(directory_backup,'dir')~=7)
    mkdir(directory_backup);
end

copyfile(Filename_z,filename_backup);
dispstr = sprintf('@%s## A copy of ##%s## has been saved under ./backup/',...
    sprintf('%s_%02d%02d',datestr(date,'yyyymmdd'),timevec(4),timevec(5)),Filename_z);
disp(dispstr);

end

Contact us