No BSD License  

Highlights from
Gui Package Path Tool

from Gui Package Path Tool by Jan Hoeft
manages the path for a dialog based package

pathPackageTool(id,command)
function pathPackageTool(id,command)
%pathPackageTool: manages the path for a dialog based package
%
% adds the subpaths of the calling script to the path and removes the added
% folders from the path afterwards
%
% helps you to work as if you have your own namespace
%
%usage:
%
%   %in the initialization script:
%   pathPackageTool('MyApp','add');
%
%   f=figure();
%   set(f,'CloseRequestFcn','pathPackageTool(''MyApp'',''remove'');');
%
%Author: Jan Hoeft(Jan_Hoeft@gmx.de)
%Date: 15.2.2006

persistent AddedPaths

switch lower(command)
    case 'add'
        before=getPathAsCellArray();
        
        stack=dbstack('-completenames');
        callingpath=fileparts(stack(2).file);
        addpath(genpath(callingpath));
        
        after=getPathAsCellArray();
        
        oldcells=findInCellArray(after,before);
        after(oldcells)=[];
        
        p.id=id;
        p.addedpath=after;
        
        AddedPaths=[AddedPaths,p];
    case 'remove'        
        i=strcmpi({AddedPaths.id},id);
        addedpath=AddedPaths(i).addedpath;
        
        pathtodelete=[];
        for(i=1:length(addedpath))
            pathtodelete=[pathtodelete,addedpath{i},pathsep()];
        end
        
        if(~isempty(pathtodelete))
            rmpath(pathtodelete);
        end
end

function p = getPathAsCellArray()
t=path;
ps=pathsep;
p={};
j=1;
for(i=1:length(t))
    if(t(i)==ps)
        p=[p,{t(j:(i-1))}];
        j=i+1;
    end
end

function index = findInCellArray(arr,tofind)
index=[];
for i=1:length(tofind)
    in=[];
    if(ischar(tofind{i}))
        in=strcmp(arr,tofind{i});
    else    
        in=(arr==tofind{i});
    end
    if(any(in))
        x=find(in);
        index=[index,x(1)];
    end
end

Contact us at files@mathworks.com