


miscellaneous string manipulation options
FORMAT string_out = spm_str_manip(string_in,options)
string_in - input string, string matrix, or cell array of strings
string_out - output sring, string matrix, or cell array of strings
options - a string of options flags
_______________________________________________________________________
Each of the options is performed from left to right.
The options are:
'r' - remove trailing suffix
's' - remove trailing suffix -
only if it is either '.img', '.hdr', '.mat' or '.nii'
'e' - remove everything except the suffix
'h' - remove trailing pathname component
'H' - always remove trailing pathname component
(returns '.' for straight filenames like 'test.img' )
(wheras 'h' option mimics csh & returns 'test.img' )
't' - remove leading pathname component
['f' num2str(n)] - remove all except first n characters
['l' num2str(n)] - remove all except last n characters
['k' num2str(n)] - produce a string of at most n characters long.
If the input string is longer than n, then
it is prefixed with '..' and the last n-2 characters
['a' num2str(n)] - similar to above - except the leading directories
are replaced by './'.
eg. spm_str_manip('/dir1/dir2/file.img','a16') would
produce '../dir2/file.img'.
are returned.
'v' - delete non valid filename characters
Valid are '.a..zA..Z01..9_-: ' & filesep
'p' - canonicalise pathname (see spm_select('CPath',strin))
'c' - remove leading components common to all strings
returns leading component as a second output argument
'C' - returns single string compressed version of a
cellstr, such as '/data/pic{01,12,23}.img'.
Second argument is a structure with fields:
.s - start string (E.g. '/data/pic')
.m - middle bits cellstr (E.g.{'01','02','03'})
.e - end string (E.g. '.img')
'd' - deblank - this is always done!
_______________________________________________________________________
Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e


0001 function [strout,R2] = nic_spm_str_manip(strin,options) 0002 % miscellaneous string manipulation options 0003 % FORMAT string_out = spm_str_manip(string_in,options) 0004 % string_in - input string, string matrix, or cell array of strings 0005 % string_out - output sring, string matrix, or cell array of strings 0006 % options - a string of options flags 0007 %_______________________________________________________________________ 0008 % Each of the options is performed from left to right. 0009 % The options are: 0010 % 'r' - remove trailing suffix 0011 % 's' - remove trailing suffix - 0012 % only if it is either '.img', '.hdr', '.mat' or '.nii' 0013 % 'e' - remove everything except the suffix 0014 % 'h' - remove trailing pathname component 0015 % 'H' - always remove trailing pathname component 0016 % (returns '.' for straight filenames like 'test.img' ) 0017 % (wheras 'h' option mimics csh & returns 'test.img' ) 0018 % 't' - remove leading pathname component 0019 % ['f' num2str(n)] - remove all except first n characters 0020 % ['l' num2str(n)] - remove all except last n characters 0021 % ['k' num2str(n)] - produce a string of at most n characters long. 0022 % If the input string is longer than n, then 0023 % it is prefixed with '..' and the last n-2 characters 0024 % ['a' num2str(n)] - similar to above - except the leading directories 0025 % are replaced by './'. 0026 % eg. spm_str_manip('/dir1/dir2/file.img','a16') would 0027 % produce '../dir2/file.img'. 0028 % are returned. 0029 % 'v' - delete non valid filename characters 0030 % Valid are '.a..zA..Z01..9_-: ' & filesep 0031 % 'p' - canonicalise pathname (see spm_select('CPath',strin)) 0032 % 'c' - remove leading components common to all strings 0033 % returns leading component as a second output argument 0034 % 'C' - returns single string compressed version of a 0035 % cellstr, such as '/data/pic{01,12,23}.img'. 0036 % Second argument is a structure with fields: 0037 % .s - start string (E.g. '/data/pic') 0038 % .m - middle bits cellstr (E.g.{'01','02','03'}) 0039 % .e - end string (E.g. '.img') 0040 % 'd' - deblank - this is always done! 0041 % 0042 %_______________________________________________________________________ 0043 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e 0044 0045 % John Ashburner 0046 % $Id: spm_str_manip.m 434 2006-02-15 13:46:45Z john $ 0047 0048 0049 if nargin<2, options=''; end 0050 if nargin<1, strout=[]; R2=''; return, end 0051 0052 sep = filesep; 0053 0054 strout = cellstr(strin); 0055 bSStrOut = ischar(strin); 0056 R2 = ''; 0057 0058 while (~isempty(options)) 0059 o = 2; 0060 0061 % Read in optional numeric argument c 0062 %--------------------------------------------------------------- 0063 opt = options(2:length(options)); 0064 c = 0; 0065 if (~isempty(opt)), b = opt(1); else, b = '-'; end 0066 while(b >= '0'+0 & b <= '9'+0) 0067 c = c * 10 + opt(1)-'0'; 0068 opt = opt(2:length(opt)); 0069 if (isempty(opt)) 0070 b = '-'; 0071 else 0072 b = opt(1); 0073 end 0074 o = o + 1; 0075 end 0076 0077 0078 %-Process option - string by string processing options 0079 %--------------------------------------------------------------- 0080 for i=1:prod(size(strout)) 0081 str = deblank(strout{i}); 0082 0083 switch options(1) 0084 case 'r' % Remove a trailing suffix of the form `.xxx', 0085 % leaving the basename. 0086 d1 = max([find(str == sep) 0]); 0087 d2 = max([find(str == '.') 0]); 0088 if (d2>d1), str = str(1:(d2-1)); end 0089 0090 case 'e' % Remove all but the suffix. 0091 d1 = max([find(str == sep) 0]); 0092 d2 = max([find(str == '.') 0]); 0093 if (d2>d1) 0094 str = str((d2+1):length(str)); 0095 else 0096 str = ''; 0097 end 0098 0099 case 'h' % Remove a trailing pathname component, 0100 % leaving the head. 0101 d1 = max([find(str == sep) 0]); 0102 if (d1>0) 0103 str = str(1:(d1-1)); 0104 end 0105 0106 case 'H' % Remove a trailing pathname component, 0107 % leaving the head. 0108 d1 = max([find(str == sep) 0]); 0109 if (d1>0) 0110 str = str(1:(d1-1)); 0111 else 0112 str = '.'; 0113 end 0114 0115 case 't' % Remove all leading pathname components, 0116 % leaving the tail. 0117 d1 = max([find(str == sep) 0]); 0118 if (d1>0) 0119 str = str((d1+1):length(str)); 0120 end 0121 0122 case 'f' % First few characters 0123 str = str(1:min([length(str) c])); 0124 0125 case 'l' % Last few characters 0126 l = length(str); 0127 str = str(l-min([length(str) c])+1:l); 0128 0129 case 'k' % Last few characters 0130 l = length(str); 0131 if (l>c) 0132 str = ['..' str(l-c+2:l)]; 0133 end 0134 0135 case 'a' % Last few characters 0136 m1 = find(str == sep); 0137 l = length(str); 0138 if (c < l) 0139 m2 = find(l-m1+1+2 <= c); 0140 if ~isempty(m2) 0141 str = ['.' str(m1(min(m2)):l)]; 0142 else 0143 str = ['.' str(max(m1):l)]; 0144 end 0145 end 0146 0147 case 's' % Strip off '.img', '.hdr' or '.mat' suffixes 0148 l = length(str); 0149 if (l > 4) 0150 if (strcmp(str((l-3):l),'.img') | ... 0151 strcmp(str((l-3):l),'.hdr') | ... 0152 strcmp(str((l-3):l),'.nii') | ... 0153 strcmp(str((l-3):l),'.mat')) 0154 str = nic_spm_str_manip(str, 'r'); 0155 end 0156 end 0157 0158 case 'v' 0159 tmp = find(... 0160 ( str>='a' & str<='z' ) | ... 0161 ( str>='A' & str<='Z' ) | ... 0162 ( str>='0' & str<='9' ) | ... 0163 str==',' | ... 0164 str=='-' | str=='_' | ... 0165 str=='.' | str==' ' | ... 0166 str=='(' | str==')' | ... 0167 str==sep | str==':'); 0168 str = str(tmp); 0169 0170 case 'p' 0171 str = strvcat(nic_spm_select('CPath',cellstr(str))); 0172 0173 case {'c','C','d'} 0174 %-Allow these options (implemented below) 0175 otherwise 0176 warning(['ignoring unrecognised option: ',options(1)]) 0177 0178 end % (case) 0179 0180 strout{i} = str; 0181 end 0182 0183 %-Process option - all strings options 0184 %--------------------------------------------------------------- 0185 if options(1)=='c' % Remove common path components 0186 if length(strout)>1 0187 tmp = size(strout); 0188 strout = char(strout(:)); 0189 msk = diff(strout+0)~=0; 0190 d1 = min(find(sum(msk,1))); 0191 d1 = max([find(strout(1,1:d1) == sep) 0]); 0192 R2 = strout(1,1:d1); 0193 strout = reshape(cellstr(strout(:,d1+1:end)),tmp); 0194 end 0195 0196 elseif options(1)=='C' %-Common path components 0197 if length(strout)>1 0198 tmp = size(strout); 0199 str = char(strout); 0200 msk = diff(str+0)~=0; 0201 d1 = min(find(sum(msk,1))); 0202 if isempty(d1) 0203 R2.s = str(1,:); 0204 R2.e = ''; 0205 R2.m = cell(tmp); 0206 [R2.m{:}] = deal(''); 0207 else 0208 R2.s = str(1,1:d1-1); 0209 str = cellstr(str(:,d1:end)); 0210 for i=1:length(str), str{i}=fliplr(str{i}); end 0211 str = char(str); 0212 msk = diff(str+0)~=0; 0213 d1 = max([1,min(find(sum(msk,1)))]); 0214 R2.e = fliplr(str(1,1:d1-1)); 0215 str = cellstr(str(:,d1:end)); 0216 for i=1:length(str), str{i}=fliplr(str{i}); end 0217 R2.m = reshape(str,tmp); 0218 end 0219 strout = {[ sprintf('%s{%s',R2.s,R2.m{1}),... 0220 sprintf(',%s',R2.m{2:end}),... 0221 sprintf('}%s',R2.e)]}; 0222 end 0223 bSStrOut = 1; 0224 0225 elseif options(1)=='d' % Deblanking is always done by cellstr above 0226 % strout = deblank(strout); 0227 end 0228 0229 options = options(o:length(options)); 0230 0231 end 0232 0233 if bSStrOut, strout=char(strout); end