| 1 | %'fileparts_uvmat': Splits a file name and recognize file naming convention
|
|---|
| 2 | %--------------------------------------------------------------------
|
|---|
| 3 | %[RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
|
|---|
| 4 | %
|
|---|
| 5 | %OUTPUT:
|
|---|
| 6 | %RootFile: FileName without appendix
|
|---|
| 7 | %i1: first number i
|
|---|
| 8 | %i2: second number i (only for .nc files)
|
|---|
| 9 | %j1: first number j
|
|---|
| 10 | %j2: second number j (only for .nc files)
|
|---|
| 11 | %Ext: file Extension
|
|---|
| 12 | %NomType: char chain characterizing the file nomenclature: with values
|
|---|
| 13 | % NomType='': constant name [filebase Ext] (default output if 'NomType' is undefined)
|
|---|
| 14 | % NomType='*':constant name for a file representing a series (e.g. avi movie)
|
|---|
| 15 | % NomType='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
|
|---|
| 16 | % NomType='1a','1A','01a','01A','1_a','01_A',... with a numerical index and an index letter(e.g.'aa45b.png') (lower or upper case)
|
|---|
| 17 | % NomType='1_1','01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa45_2.png')
|
|---|
| 18 | % NomType='1-1': from pairs from a single index (e.g. 'aa_45-47.nc')
|
|---|
| 19 | % NomType='1_1-1': pairs of j indices (e.g. 'aa_45_2-3.nc')
|
|---|
| 20 | % NomType='1-1_1': pairs of i indices (e.g. 'aa_45-46_2.nc')
|
|---|
| 21 | % NomType='1_ab','01_ab','01_a-b'..., from pairs of '#' images (e.g.'aa045bc.nc'), Ext='.nc'
|
|---|
| 22 | %SubDir: name of the SubDirectory for netcdf files
|
|---|
| 23 |
|
|---|
| 24 | % OLD TYPES, not supported anymore
|
|---|
| 25 | % NomType='_1','_01','_001'...': series of files with a single index i with separator '_'(e.g. 'aa_045.png').
|
|---|
| 26 | % NomType='_1a','_1A','_01a','_01A',...: idem, with a separator '_' before the index
|
|---|
| 27 | % NomType='_1_1','_01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
|
|---|
| 28 | % NomType='_i1-i2': from pairs from a single index (e.g. 'aa_45-47.nc')
|
|---|
| 29 | % NomType='_i_j1-j2': pairs of j indices (e.g. 'aa_45_2-3.nc')
|
|---|
| 30 | % NomType='_i1-i2_j': pairs of i indices (e.g. 'aa_45-46_2.nc')
|
|---|
| 31 | % NomType='_1_ab','1_ab','01_ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), Ext='.nc'
|
|---|
| 32 |
|
|---|
| 33 | %
|
|---|
| 34 | %INPUT:
|
|---|
| 35 | %FileInput: complete name of the file, including path
|
|---|
| 36 |
|
|---|
| 37 | function [RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
|
|---|
| 38 |
|
|---|
| 39 | i1=[];
|
|---|
| 40 | i2=[];
|
|---|
| 41 | j1=[];
|
|---|
| 42 | j2=[];
|
|---|
| 43 | NomType='';
|
|---|
| 44 | SubDir='';
|
|---|
| 45 | RootFile='';
|
|---|
| 46 |
|
|---|
| 47 | if ~exist('FileInput','var')
|
|---|
| 48 | help fileparts_uvmat;
|
|---|
| 49 | test();
|
|---|
| 50 | return
|
|---|
| 51 | end
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | [RootPath,FileName,Ext]=fileparts(FileInput);
|
|---|
| 55 |
|
|---|
| 56 | switch Ext
|
|---|
| 57 | case '.avi'
|
|---|
| 58 | NomType='*';
|
|---|
| 59 | return
|
|---|
| 60 | case {'.tif','.tiff'}
|
|---|
| 61 | if exist(FileInput,'file')
|
|---|
| 62 | info=iminfo(FileInput);
|
|---|
| 63 | if length(info)>1
|
|---|
| 64 | NomType='*';
|
|---|
| 65 | return
|
|---|
| 66 | end
|
|---|
| 67 | end
|
|---|
| 68 | end
|
|---|
| 69 |
|
|---|
| 70 | % \D not a digit
|
|---|
| 71 | % \d digit
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | %% recursive test on FileName stqrting from the end
|
|---|
| 75 |
|
|---|
| 76 | % test whether FileName ends with a number or not
|
|---|
| 77 | r=regexp(FileName,'.*\D(?<num1>\d+)\>','names');
|
|---|
| 78 |
|
|---|
| 79 | if ~isempty(r)% FileName end matches num1
|
|---|
| 80 | num1=r.num1;
|
|---|
| 81 | r=regexp(FileName,['.*\D(?<num2>\d+)(?<delim1>[-_])' num1 '\>'],'names');
|
|---|
| 82 | if ~isempty(r)% FileName end matches num2+delim1+num1
|
|---|
| 83 | delim1=r.delim1;
|
|---|
| 84 | num2=r.num2;
|
|---|
| 85 | r=regexp(FileName,['.*\D(?<num3>\d+)(?<delim2>[-_])' num2 delim1 num1 '\>'],'names');
|
|---|
| 86 | if ~isempty(r) % FileName end matches delim2 num2 delim1 num1
|
|---|
| 87 | delim2=r.delim2;
|
|---|
| 88 | num3=r.num3;
|
|---|
| 89 | switch delim1
|
|---|
| 90 | case '_'
|
|---|
| 91 | j1=str2double(num1);
|
|---|
| 92 | switch delim2
|
|---|
| 93 | case '-'
|
|---|
| 94 | i1=str2double(num3);
|
|---|
| 95 | i2=str2double(num2);
|
|---|
| 96 | end
|
|---|
| 97 | case '-'
|
|---|
| 98 | j1=str2double(num2);
|
|---|
| 99 | j2=str2double(num1);
|
|---|
| 100 | switch delim2
|
|---|
| 101 | case '_'
|
|---|
| 102 | i1=str2double(num3);
|
|---|
| 103 | end
|
|---|
| 104 | end
|
|---|
| 105 | NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
|
|---|
| 106 | RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1],'');
|
|---|
| 107 | else
|
|---|
| 108 | switch delim1
|
|---|
| 109 | case '_'
|
|---|
| 110 | i1=str2double(num2);
|
|---|
| 111 | j1=str2double(num1);
|
|---|
| 112 | case '-'
|
|---|
| 113 | i1=str2double(num2);
|
|---|
| 114 | i2=str2double(num1);
|
|---|
| 115 | end
|
|---|
| 116 | NomType=[get_type(num2) delim1 get_type(num1)];
|
|---|
| 117 | RootFile=regexprep(FileName,[num2 delim1 num1],'');
|
|---|
| 118 | end
|
|---|
| 119 | else% only one number at the end
|
|---|
| 120 | i1=str2double(num1);
|
|---|
| 121 | NomType=get_type(num1);
|
|---|
| 122 | RootFile=regexprep(FileName,num1,'');
|
|---|
| 123 | end
|
|---|
| 124 | else% FileName ends with a letter
|
|---|
| 125 | r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
|
|---|
| 126 | if ~isempty(r)
|
|---|
| 127 | end_string=r.end_string;
|
|---|
| 128 | r=regexp(FileName,['.+(?<delim1>[_-])' end_string '\>'],'names');
|
|---|
| 129 | if ~isempty(r)
|
|---|
| 130 | delim1=r.delim1;
|
|---|
| 131 | r=regexp(FileName,['.*\D(?<num1>\d+)' delim1 end_string '\>'],'names');
|
|---|
| 132 | if ~isempty(r)
|
|---|
| 133 | num1=r.num1;
|
|---|
| 134 | NomType=[get_type(num1) delim1 get_type(end_string)];
|
|---|
| 135 | i1=str2double(num1);
|
|---|
| 136 | [j1,j2]=get_value(end_string);
|
|---|
| 137 | RootFile=regexprep(FileName,[num1 delim1 end_string],'');
|
|---|
| 138 |
|
|---|
| 139 | else
|
|---|
| 140 | NomType=get_type(end_string);
|
|---|
| 141 | [j1,j2]=get_value(end_string);
|
|---|
| 142 | RootFile=regexprep(FileName,end_string,'');
|
|---|
| 143 |
|
|---|
| 144 | end
|
|---|
| 145 | else
|
|---|
| 146 | r=regexp(FileName,['.*\D(?<num1>\d+)' end_string '\>'],'names');
|
|---|
| 147 | if ~isempty(r)
|
|---|
| 148 | num1=r.num1;
|
|---|
| 149 | % r=regexp(FileName,['.+(?<delim1>[-_])' num1 end_string '\>'],'names');
|
|---|
| 150 | % if ~isempty(r)
|
|---|
| 151 | % delim1=r.delim1;
|
|---|
| 152 | % i1=num1;
|
|---|
| 153 | % str_a=end_string;
|
|---|
| 154 | % NomType=[delim1 get_type(num1) get_type(end_string)];
|
|---|
| 155 | % RootFile=regexprep(FileName,[delim1 num1 end_string],'');
|
|---|
| 156 | % else
|
|---|
| 157 | i1=str2double(num1);
|
|---|
| 158 | [j1,j2]=get_value(end_string);
|
|---|
| 159 | NomType=[get_type(num1) get_type(end_string)];
|
|---|
| 160 | RootFile=regexprep(FileName,[num1 end_string],'');
|
|---|
| 161 | % end
|
|---|
| 162 | else
|
|---|
| 163 | end
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | end
|
|---|
| 168 | else
|
|---|
| 169 | end
|
|---|
| 170 | end
|
|---|
| 171 |
|
|---|
| 172 | if ~isempty(regexp(NomType,'-|ab|AB'))
|
|---|
| 173 | r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
|
|---|
| 174 | if ~isempty(r)
|
|---|
| 175 | SubDir=r.subdir;
|
|---|
| 176 | RootPath=r.newrootpath;
|
|---|
| 177 | end
|
|---|
| 178 | end
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 | function type=get_type(s)
|
|---|
| 184 | % returns the type of a label string:
|
|---|
| 185 | % for numbers, whether filled with 0 or not.
|
|---|
| 186 | % for letters, either a, A or ab, AB.
|
|---|
| 187 |
|
|---|
| 188 | switch s
|
|---|
| 189 | case {'a','b'}
|
|---|
| 190 | type='a';
|
|---|
| 191 | case {'A','B'}
|
|---|
| 192 | type='A';
|
|---|
| 193 | case 'ab'
|
|---|
| 194 | type='ab';
|
|---|
| 195 | case 'AB'
|
|---|
| 196 | type='AB';
|
|---|
| 197 | otherwise
|
|---|
| 198 | if ~isempty(regexp(s,'\<\d+\>','ONCE'))
|
|---|
| 199 | % switch s(1)
|
|---|
| 200 | % case '0'
|
|---|
| 201 | type=num2str(1,['%0' num2str(length(s)) 'd']);
|
|---|
| 202 | % otherwise
|
|---|
| 203 | % type='1';
|
|---|
| 204 | % end
|
|---|
| 205 | else
|
|---|
| 206 | type='';
|
|---|
| 207 | return
|
|---|
| 208 | end
|
|---|
| 209 | end
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | function [j1,j2]=get_value(s)
|
|---|
| 213 | % returns the type of a label string:
|
|---|
| 214 | % for numbers, whether filled with 0 or not.
|
|---|
| 215 | % for letters, either a, A or ab, AB.
|
|---|
| 216 | j1=[];
|
|---|
| 217 | j2=[];
|
|---|
| 218 |
|
|---|
| 219 | switch lower(s)
|
|---|
| 220 | case {'a'}
|
|---|
| 221 | j1=1;
|
|---|
| 222 | case {'b','B'}
|
|---|
| 223 | j1=2;
|
|---|
| 224 | case 'ab'
|
|---|
| 225 | j1=1;j2=2;
|
|---|
| 226 | otherwise
|
|---|
| 227 | return
|
|---|
| 228 | end
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 | function test(name)
|
|---|
| 233 | fprintf([...
|
|---|
| 234 | '######################################################\n'...
|
|---|
| 235 | ' Test for fileparts_uvmat \n'...
|
|---|
| 236 | '######################################################\n'...
|
|---|
| 237 | ]);
|
|---|
| 238 |
|
|---|
| 239 | if exist('name','var')
|
|---|
| 240 | FileName_list={name};
|
|---|
| 241 | else
|
|---|
| 242 | FileName_list={...
|
|---|
| 243 | 'Image1a.png'...
|
|---|
| 244 | 'toto'...
|
|---|
| 245 | 'B011_1.png'...
|
|---|
| 246 | 'B001.png'...
|
|---|
| 247 | 'B55a.tif'...
|
|---|
| 248 | 'B002B.tiff'...
|
|---|
| 249 | 'B_001.png'...
|
|---|
| 250 | 'B_3331AB.png'...
|
|---|
| 251 | 'aa45_2.png'...
|
|---|
| 252 | 'B005.png'...
|
|---|
| 253 | 'Image_3.jpg'...
|
|---|
| 254 | 'Image_3-4.jpg'...
|
|---|
| 255 | 'Image_3-4_2.jpg'...
|
|---|
| 256 | 'Image_5_3-4.jpg'...
|
|---|
| 257 | 'Image_3_ab.jpg'...
|
|---|
| 258 | 'Image005AB.jpg'...
|
|---|
| 259 | 'Image_3_ab.jpg'...
|
|---|
| 260 | 'Image_3a-b.jpg'...
|
|---|
| 261 | 'Image3_a.jpg'...
|
|---|
| 262 | 'movie57.avi'...
|
|---|
| 263 | };
|
|---|
| 264 | end
|
|---|
| 265 |
|
|---|
| 266 | for FileName=FileName_list
|
|---|
| 267 | % [RootPath,RootFile,i1,i2,str_a,str_b,Ext,NomType,SubDir]=name2display(FileName{1});
|
|---|
| 268 | [~,RootFile,i1,i2,j1,j2,~,NomType,SubDir]=...
|
|---|
| 269 | fileparts_uvmat(FileName{1});
|
|---|
| 270 | fprintf([...
|
|---|
| 271 | 'File name : ' FileName{1} '\n'...
|
|---|
| 272 | ' NomType : ' NomType '\n'...
|
|---|
| 273 | ' RootFile : ' RootFile '\n'...
|
|---|
| 274 | ' i1 / i2 : ' num2str(i1) ' / ' num2str(i2) '\n'...
|
|---|
| 275 | ' j1 / j2 : ' num2str(j1) ' / ' num2str(j2) '\n'...
|
|---|
| 276 | ]);
|
|---|
| 277 | end
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|