function f = absolutepath(x) <x> is a string referring to a file. can be absolute or relative to the current directory. return the absolute path to the file. we rely on pwd to figure out the path. note: the file does not actually have to exist. example: absolutepath('temp')
0001 function f = absolutepath(x) 0002 0003 % function f = absolutepath(x) 0004 % 0005 % <x> is a string referring to a file. can be absolute or 0006 % relative to the current directory. 0007 % 0008 % return the absolute path to the file. 0009 % we rely on pwd to figure out the path. 0010 % note: the file does not actually have to exist. 0011 % 0012 % example: 0013 % absolutepath('temp') 0014 0015 % find any / 0016 locs = strfind(x,filesep); 0017 0018 % if none, be relative to current directory and get out 0019 if isempty(locs) 0020 f = [pwd filesep x]; 0021 return; 0022 end 0023 0024 % save old cd 0025 oldcd = cd; 0026 0027 % change to new 0028 cd(x(1:locs(end))); 0029 0030 % what is the filename 0031 remain = x(locs(end)+1:end); 0032 0033 % return 0034 cwd = pwd; 0035 if isequal(cwd,filesep) 0036 f = [cwd remain]; 0037 else 0038 f = [cwd filesep remain]; 0039 end 0040 0041 % change back to old cd 0042 cd(oldcd);