Home > vbmeg > functions > common > utility > vb_relativepath.m

vb_relativepath

PURPOSE ^

RELATIVEPATH returns the relative path from an actual path to the target path.

SYNOPSIS ^

function rel_path = vb_relativepath( tgt_path, act_path )

DESCRIPTION ^

RELATIVEPATH  returns the relative path from an actual path to the target path.
   Both arguments must be strings with absolute paths.
   The actual path is optional, if omitted the current dir is used instead.
   In case the volume drive letters don't match, an absolute path will be returned.
   If a relative path is returned, it always starts with '.\' or '..\'

   Syntax:
      rel_path = RELATIVEPATH( target_path, actual_path )
   
   Parameters:
      target_path        - Path which is targetted
      actual_path        - Start for relative path (optional, default = current dir)

   Examples:
      vb_relativepath( 'C:\local\data\matlab' , 'C:\local' ) = '.\data\matlab\'
      vb_relativepath( 'A:\MyProject\'        , 'C:\local' ) = 'a:\myproject\'

      vb_relativepath( 'C:\local\data\matlab' , cd         ) is the same as
      vb_relativepath( 'C:\local\data\matlab'              )

   See also:  ABSOLUTEPATH PATH

 Copyright (c) 2003, Jochen Lenz
 All rights reserved.
 http://www.mathworks.com/matlabcentral/fileexchange/3858-relativepath-m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function  rel_path = vb_relativepath( tgt_path, act_path )
0002 %RELATIVEPATH  returns the relative path from an actual path to the target path.
0003 %   Both arguments must be strings with absolute paths.
0004 %   The actual path is optional, if omitted the current dir is used instead.
0005 %   In case the volume drive letters don't match, an absolute path will be returned.
0006 %   If a relative path is returned, it always starts with '.\' or '..\'
0007 %
0008 %   Syntax:
0009 %      rel_path = RELATIVEPATH( target_path, actual_path )
0010 %
0011 %   Parameters:
0012 %      target_path        - Path which is targetted
0013 %      actual_path        - Start for relative path (optional, default = current dir)
0014 %
0015 %   Examples:
0016 %      vb_relativepath( 'C:\local\data\matlab' , 'C:\local' ) = '.\data\matlab\'
0017 %      vb_relativepath( 'A:\MyProject\'        , 'C:\local' ) = 'a:\myproject\'
0018 %
0019 %      vb_relativepath( 'C:\local\data\matlab' , cd         ) is the same as
0020 %      vb_relativepath( 'C:\local\data\matlab'              )
0021 %
0022 %   See also:  ABSOLUTEPATH PATH
0023 %
0024 % Copyright (c) 2003, Jochen Lenz
0025 % All rights reserved.
0026 % http://www.mathworks.com/matlabcentral/fileexchange/3858-relativepath-m
0027 
0028 % 2nd parameter is optional:
0029 if  nargin < 2
0030    act_path = cd;
0031 end
0032 
0033 % Predefine return string:
0034 rel_path = '';
0035 
0036 % Make sure strings end by a filesep character:
0037 if  length(act_path) == 0   |   ~isequal(act_path(end),filesep)
0038    act_path = [act_path filesep];
0039 end
0040 if  length(tgt_path) == 0   |   ~isequal(tgt_path(end),filesep)
0041    tgt_path = [tgt_path filesep];
0042 end
0043 
0044 % Convert to all lowercase:
0045 %[act_path] = fileparts( lower(act_path) );
0046 %[tgt_path] = fileparts( lower(tgt_path) );
0047 [act_path] = fileparts( act_path );
0048 [tgt_path] = fileparts( tgt_path );
0049 
0050 % Create a cell-array containing the directory levels:
0051 act_path_cell = pathparts(act_path);
0052 tgt_path_cell = pathparts(tgt_path);
0053 
0054 % If volumes are different, return absolute path:
0055 if  length(act_path_cell) == 0   |   length(tgt_path_cell) == 0
0056    return  % rel_path = ''
0057 else
0058    if  ~isequal( act_path_cell{1} , tgt_path_cell{1} )
0059       rel_path = tgt_path;
0060       return
0061    end
0062 end
0063 
0064 % Remove level by level, as long as both are equal:
0065 while  length(act_path_cell) > 0   &   length(tgt_path_cell) > 0
0066    if  isequal( act_path_cell{1}, tgt_path_cell{1} )
0067       act_path_cell(1) = [];
0068       tgt_path_cell(1) = [];
0069    else
0070       break
0071    end
0072 end
0073 
0074 % As much levels down ('..\') as levels are remaining in "act_path":
0075 for  i = 1 : length(act_path_cell)
0076    rel_path = ['..' filesep rel_path];
0077 end
0078 
0079 % Relative directory levels to target directory:
0080 for  i = 1 : length(tgt_path_cell)
0081    rel_path = [rel_path tgt_path_cell{i} filesep];
0082 end
0083 
0084 % Start with '.' or '..' :
0085 if  isempty(rel_path)
0086    rel_path = ['.' filesep];
0087 elseif  ~isequal(rel_path(1),'.')
0088    rel_path = ['.' filesep rel_path];
0089 end
0090 
0091 return
0092 
0093 % -------------------------------------------------
0094 
0095 function  path_cell = pathparts(path_str)
0096 
0097 path_str = [filesep path_str filesep];
0098 path_cell = {};
0099 
0100 sep_pos = findstr( path_str, filesep );
0101 for i = 1 : length(sep_pos)-1
0102    path_cell{i} = path_str( sep_pos(i)+1 : sep_pos(i+1)-1 );
0103 end
0104 
0105 return

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005