function [f,file] = stripfile(x,flag,sep) <x> is a string referring to a file (it is okay if the file does not actually exist). if <x> ends in /, we automatically act as if that / does not exist. <flag> (optional) is whether to swap the output arguments. default: 0. <sep> (optional) is the character to use. default: '/'. if <flag> is 0, return <f> as the string but with the file name removed. return <file> with the file name. if <flag> is 1, these arguments are swapped. example: isequal(stripfile('blah/temp.png'),'blah/') isequal(stripfile('temp.png'),'') isequal(stripfile('ok/blah/',1),'blah')
0001 function [f,file] = stripfile(x,flag,sep) 0002 0003 % function [f,file] = stripfile(x,flag,sep) 0004 % 0005 % <x> is a string referring to a file (it is okay if the file 0006 % does not actually exist). if <x> ends in /, we automatically 0007 % act as if that / does not exist. 0008 % <flag> (optional) is whether to swap the output arguments. default: 0. 0009 % <sep> (optional) is the character to use. default: '/'. 0010 % 0011 % if <flag> is 0, 0012 % return <f> as the string but with the file name removed. 0013 % return <file> with the file name. 0014 % if <flag> is 1, these arguments are swapped. 0015 % 0016 % example: 0017 % isequal(stripfile('blah/temp.png'),'blah/') 0018 % isequal(stripfile('temp.png'),'') 0019 % isequal(stripfile('ok/blah/',1),'blah') 0020 0021 % input 0022 if ~exist('flag','var') || isempty(flag) 0023 flag = 0; 0024 end 0025 if ~exist('sep','var') || isempty(sep) 0026 sep = '/'; 0027 end 0028 0029 % find any / 0030 locs = strfind(x,sep); 0031 0032 % if none, return '' 0033 if isempty(locs) 0034 f = ''; 0035 file = x; 0036 % otherwise, return entire string up to the last / 0037 else 0038 if locs(end)==length(x) % ignore trailing / 0039 x = x(1:end-1); 0040 locs = locs(1:end-1); 0041 end 0042 if isempty(locs) 0043 f = ''; 0044 file = x; 0045 else 0046 f = x(1:locs(end)); 0047 file = x(locs(end)+1:end); 0048 end 0049 end 0050 0051 % swap the output? 0052 if flag 0053 [f,file] = swap(f,file); 0054 end