No BSD License  

Highlights from
SplitFunctions

image thumbnail
from SplitFunctions by Michael Robbins
takes a file with many functions in it and splits those functions up into as many files, entitled wi

SplitFunctions(filename,targetdir,DOWAITBAR)
function SplitFunctions(filename,targetdir,DOWAITBAR)
% SPLITFUNCTIONS takes a file with many functions in it and splits those
% functions up into as many files, entitled with the function names
%
% SPLITFUNCTION(FILENAME) splits the file into parts and puts those parts
%   in the same directory as FILENAME
% SPLITFUNCTION(FILENAME,TARGETDIR) splits the file, putting parts in
%   TARGETDIR
% SPLITFUNCTION(FILENAME,TARGETDIR,DOWAITBAR) same as above but uses
%   waitbar to show progress.
%
% It will turn
% <foo.m starts>
% function a
%  ...
% function [a,b] = b
%  ...
% function [a,b] = c(d,e)
% ....
% <foo.m ends>
%
% In to:
% <a.m starts>
% function a
%  ...
% <a.m ends>
% <b.m starts>
% function [a,b] = b
%  ...
% <b.m ends>
% <c.m starts>
% function [a,b] = c(d,e)
% ....
% <c.m ends>
%
% SEE ALSO regexp regesprep strrep strfind findstr strmatch
% KEY WORDS regular expressions split file function script procedure
%
%
% It's not fancy, but it works

% Michael Robbins
% michaelNOrobbinsSPAMusenet@yahoo.com
% robbins@bloomberg.net

TRUE  = logical(1);
FALSE = logical(0);
ONESWOOP = 1;
LOOPIT = 2;

filename = which(filename);

if nargin<2 targetdir = fileparts(filename); end;
if nargin<3 DOWAITBAR = logical(1); end;


% READ IN THE FILE
FID = fopen(filename,'rt');
if FID>-1
    slurp = fscanf(FID,'%c');
    fclose(FID);

    % SEPARATE THE FUNCTIONS
    if DOWAITBAR h=waitbar(0,'Please wait, splitting takes a while...'); end;
    funloc = [regexp(sprintf('\n%s',slurp),'\n\s*function') length(slurp)];
    switch LOOPIT
        case ONESWOOP, b=GetSubFunNames(slurp,TRUE);
        case LOOPIT
            for i=1:length(funloc)-1
                if DOWAITBAR
                    h=waitbar(i./(2.*length(funloc)),h, ...
                        sprintf('Processing pass #1 (%d/%d)', ...
                           i,length(funloc)));
                end;
                a(i).fullfun = slurp(funloc(i):funloc(i+1)-1);
                a(i).name = GetSubFunNames(a(i).fullfun,TRUE);
                if iscell(a(i).name) a(i).name = a(i).name{1}; end;
            end;
        case FPARSER
            %name = spit(a(i).fullfun,targetdir,'temp');
            %s = fparser(name);
            %delete(name);
        otherwise, errordlg('Bad switch.',mfilename);
    end;

    % SPIT OUT THE FILES
    for i=1:length(a)
        if DOWAITBAR
            h=waitbar((i+length(a))/(2.*length(a)),h, ...
                sprintf('Processing pass #2 %s (%d/%d)', ...
                   strrep(a(i).name,'_','\_'), ...
                   i,length(a)));
        end;
        if i==1 && exist(targetdir)~=7
            system(['md ' targetdir]);
        end
        spit(a(i).fullfun,targetdir,a(i).name);
    end;
    if DOWAITBAR close(h); end;
else
    errordlg(sprintf('%s failed to open for reading',filename), ...
        mfilename);
end;


function str = SFJustTheName(filename)
[path,str] = fileparts(filename);


function name = spit(fullfun,targetdir,name)
% BACKUP
name = fullfile(targetdir,[name '.m']);
if exist(name)==2
    system(sprintf('copy %s %s_%s.bak', ...
        name,name, ...
        datestr(now,'yyyymmddTHHMMSS')));
end;
FID = fopen(name,'wt');
if FID>1
%            a(i).fullfun = [a(i).prename a(i).name a(i).postname];
    fprintf(FID,'%s',fullfun);
    fclose(FID);
else
    errordlg(sprintf('%s failed to open for writing',name2), ...
        mfilename);
end;

Contact us