Code covered by the BSD License  

Highlights from
Relative Filename

from Relative Filename by Kesh Ikuma
A pair of M-files to convert between full and relative filenames.

relfile(files,refpath)
function files = relfile(files,refpath)
%RELFILE   Relative filename
%   RELFILE(FILE,PATH) returns the relative path of the specified FILE
%   with respect to the reference path, PATH. FILE can be a string
%   containig the file name or a cell string array to convert multiple
%   file names.
%
%   RELFILE(FILE) returns the relative path with respect to the current
%   MATLAB working directory.
%
%   Note that this function is case-sensitive.
%
%   See also rel2fullfile, fileparts, fullfile, filesep.

% Written by: Takeshi Ikuma
% Date: 10/31/2009
% Revision History:
%  (12/03/2010) allow '/' as the file separator in Windows
%  (01/19/2011) allow multiple files to be converted at once

error(nargchk(1,2,nargin));

if nargin<2, refpath = ''; end

if isempty(files), return; end
if isempty(refpath), refpath = pwd; end

% make sure the input is char not cellstr, convert if necessary
IS_CHAR = ischar(files);
if IS_CHAR
   files = cellstr(files);
elseif ~iscellstr(files)
   error('FILE must be a string or a cellstr array.');
end

if iscellstr(refpath), refpath = char(refpath); end
if ~ischar(refpath) || size(refpath,1)~=1
   error('PATH must be a string');
end

% for Windows platform, convert '/' to '\' in the pathnames
if filesep=='\' % WINDOWS==backslash as file separator
   files = strrep(files,'/','\');
   refpath = strrep(refpath,'/','\');
end

if refpath(end)~=filesep % make sure that the reference path ends with file separator
   refpath = [refpath filesep];
end

for k = 1:numel(files)
   
   N = min(length(files{k}),length(refpath));
   I = find(files{k}(1:N)~=refpath(1:N),1,'first'); % first unmatched char
   if isempty(I), I = N+1; end
   
   if (I~=1 && refpath(I-1)~=filesep) % if the last character of matched section is not filesep
      I = find(refpath(1:I-1)==filesep,1,'last'); % find the final filesep in the common path
      I = I+1;
   end
   
   files{k} = files{k}(I:end);
   rpath = refpath(I:end);
   
   if isempty(rpath)
      files{k} = strcat('.',filesep,files{k});
   else
      rpath = rpath(1:end-1); % remove the file separator
      while ~isempty(rpath)
         rpath = fileparts(rpath);
         files{k} = strcat('..',filesep,files{k});
      end
   end
end

if IS_CHAR, files = char(files); end

% Copyright (c)2009-2011, Takeshi Ikuma
% All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
% 
%     * Redistributions of source code must retain the above copyright
%     notice, this list of conditions and the following disclaimer. *
%     Redistributions in binary form must reproduce the above copyright
%     notice, this list of conditions and the following disclaimer in the
%     documentation and/or other materials provided with the distribution.
%     * Neither the name of the <ORGANIZATION> nor the names of its
%     contributors may be used to endorse or promote products derived from
%     this software without specific prior written permission.
% 
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Contact us at files@mathworks.com