function files = rel2fullfile(files,refpath)
%REL2FULLFILE Build full filename from relative file name
% REL2FULLFILE(RELFILE,PATH) builds a full file name from the relative
% path name RELFILE with respect to PATH. RELFILE may be a cellstr array
% of file names.
%
% REL2FULLFILE(RELFILE) builds the full filename using the current
% MATLAB working directory as PATH.
%
% See also relfile, 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 % if reference path ends with file separator, remove it
refpath = refpath(1:end-1);
end
for k = 1:numel(files)
if strcmp(files{k}(1:2),'.\')
files{k} = [refpath files{k}(2:end)];
else
N = length(files{k});
i0 = 1;
rpath = refpath;
while N>=3 && strcmp(files{k}(i0:i0+2),'..\') && ~isempty(refpath)
i0 = i0 + 3;
N = N - 3;
rpath = fileparts(rpath);
end
if rpath(end)==filesep
files{k} = [rpath files{k}(i0:end)];
else
files{k} = [rpath filesep files{k}(i0:end)];
end
end
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.