[327] | 1 | %'fileparts_uvmat': Splits a file name and recognize file naming convention
|
---|
[318] | 2 | %--------------------------------------------------------------------
|
---|
[327] | 3 | %[RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
|
---|
[318] | 4 | %
|
---|
| 5 | %OUTPUT:
|
---|
[327] | 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 |
|
---|
[318] | 33 | %
|
---|
| 34 | %INPUT:
|
---|
[327] | 35 | %FileInput: complete name of the file, including path
|
---|
[318] | 36 |
|
---|
[327] | 37 | function [RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
|
---|
| 38 |
|
---|
| 39 | i1=[];
|
---|
[318] | 40 | i2=[];
|
---|
| 41 | j1=[];
|
---|
| 42 | j2=[];
|
---|
[327] | 43 | NomType='';
|
---|
| 44 | SubDir='';
|
---|
[318] | 45 | RootFile='';
|
---|
| 46 |
|
---|
[327] | 47 | if ~exist('FileInput','var')
|
---|
| 48 | help fileparts_uvmat;
|
---|
| 49 | test();
|
---|
| 50 | return
|
---|
| 51 | end
|
---|
[318] | 52 |
|
---|
[327] | 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 |
|
---|
[318] | 70 | % \D not a digit
|
---|
| 71 | % \d digit
|
---|
| 72 |
|
---|
| 73 |
|
---|
[327] | 74 | %% recursive test on FileName stqrting from the end
|
---|
[318] | 75 |
|
---|
[327] | 76 | % test whether FileName ends with a number or not
|
---|
| 77 | r=regexp(FileName,'.*\D(?<num1>\d+)\>','names');
|
---|
[318] | 78 |
|
---|
[327] | 79 | if ~isempty(r)% FileName end matches num1
|
---|
[318] | 80 | num1=r.num1;
|
---|
[327] | 81 | r=regexp(FileName,['.*\D(?<num2>\d+)(?<delim1>[-_])' num1 '\>'],'names');
|
---|
| 82 | if ~isempty(r)% FileName end matches num2+delim1+num1
|
---|
[318] | 83 | delim1=r.delim1;
|
---|
| 84 | num2=r.num2;
|
---|
[327] | 85 | r=regexp(FileName,['.*\D(?<num3>\d+)(?<delim2>[-_])' num2 delim1 num1 '\>'],'names');
|
---|
| 86 | if ~isempty(r) % FileName end matches delim2 num2 delim1 num1
|
---|
[318] | 87 | delim2=r.delim2;
|
---|
| 88 | num3=r.num3;
|
---|
| 89 | switch delim1
|
---|
| 90 | case '_'
|
---|
[327] | 91 | j1=str2double(num1);
|
---|
[318] | 92 | switch delim2
|
---|
| 93 | case '-'
|
---|
[327] | 94 | i1=str2double(num3);
|
---|
| 95 | i2=str2double(num2);
|
---|
[318] | 96 | end
|
---|
| 97 | case '-'
|
---|
[327] | 98 | j1=str2double(num2);
|
---|
| 99 | j2=str2double(num1);
|
---|
[318] | 100 | switch delim2
|
---|
| 101 | case '_'
|
---|
[327] | 102 | i1=str2double(num3);
|
---|
[318] | 103 | end
|
---|
| 104 | end
|
---|
[327] | 105 | NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
|
---|
| 106 | RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1],'');
|
---|
[318] | 107 | else
|
---|
| 108 | switch delim1
|
---|
| 109 | case '_'
|
---|
[327] | 110 | i1=str2double(num2);
|
---|
| 111 | j1=str2double(num1);
|
---|
[318] | 112 | case '-'
|
---|
[327] | 113 | i1=str2double(num2);
|
---|
| 114 | i2=str2double(num1);
|
---|
[318] | 115 | end
|
---|
[327] | 116 | NomType=[get_type(num2) delim1 get_type(num1)];
|
---|
| 117 | RootFile=regexprep(FileName,[num2 delim1 num1],'');
|
---|
[318] | 118 | end
|
---|
| 119 | else% only one number at the end
|
---|
[327] | 120 | i1=str2double(num1);
|
---|
| 121 | NomType=get_type(num1);
|
---|
| 122 | RootFile=regexprep(FileName,num1,'');
|
---|
[318] | 123 | end
|
---|
[327] | 124 | else% FileName ends with a letter
|
---|
| 125 | r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
|
---|
[318] | 126 | if ~isempty(r)
|
---|
| 127 | end_string=r.end_string;
|
---|
[327] | 128 | r=regexp(FileName,['.+(?<delim1>[_-])' end_string '\>'],'names');
|
---|
[318] | 129 | if ~isempty(r)
|
---|
| 130 | delim1=r.delim1;
|
---|
[327] | 131 | r=regexp(FileName,['.*\D(?<num1>\d+)' delim1 end_string '\>'],'names');
|
---|
[318] | 132 | if ~isempty(r)
|
---|
| 133 | num1=r.num1;
|
---|
[327] | 134 | NomType=[get_type(num1) delim1 get_type(end_string)];
|
---|
| 135 | i1=str2double(num1);
|
---|
[318] | 136 | [j1,j2]=get_value(end_string);
|
---|
[327] | 137 | RootFile=regexprep(FileName,[num1 delim1 end_string],'');
|
---|
[318] | 138 |
|
---|
| 139 | else
|
---|
[327] | 140 | NomType=get_type(end_string);
|
---|
[318] | 141 | [j1,j2]=get_value(end_string);
|
---|
[327] | 142 | RootFile=regexprep(FileName,end_string,'');
|
---|
[318] | 143 |
|
---|
| 144 | end
|
---|
| 145 | else
|
---|
[327] | 146 | r=regexp(FileName,['.*\D(?<num1>\d+)' end_string '\>'],'names');
|
---|
[318] | 147 | if ~isempty(r)
|
---|
| 148 | num1=r.num1;
|
---|
[327] | 149 | % r=regexp(FileName,['.+(?<delim1>[-_])' num1 end_string '\>'],'names');
|
---|
[318] | 150 | % if ~isempty(r)
|
---|
| 151 | % delim1=r.delim1;
|
---|
| 152 | % i1=num1;
|
---|
| 153 | % str_a=end_string;
|
---|
[327] | 154 | % NomType=[delim1 get_type(num1) get_type(end_string)];
|
---|
| 155 | % RootFile=regexprep(FileName,[delim1 num1 end_string],'');
|
---|
[318] | 156 | % else
|
---|
[327] | 157 | i1=str2double(num1);
|
---|
[318] | 158 | [j1,j2]=get_value(end_string);
|
---|
[327] | 159 | NomType=[get_type(num1) get_type(end_string)];
|
---|
| 160 | RootFile=regexprep(FileName,[num1 end_string],'');
|
---|
[318] | 161 | % end
|
---|
| 162 | else
|
---|
| 163 | end
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | end
|
---|
| 168 | else
|
---|
| 169 | end
|
---|
| 170 | end
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | function type=get_type(s)
|
---|
| 178 | % returns the type of a label string:
|
---|
| 179 | % for numbers, whether filled with 0 or not.
|
---|
| 180 | % for letters, either a, A or ab, AB.
|
---|
| 181 |
|
---|
| 182 | switch s
|
---|
| 183 | case {'a','b'}
|
---|
| 184 | type='a';
|
---|
| 185 | case {'A','B'}
|
---|
| 186 | type='A';
|
---|
| 187 | case 'ab'
|
---|
| 188 | type='ab';
|
---|
| 189 | case 'AB'
|
---|
| 190 | type='AB';
|
---|
| 191 | otherwise
|
---|
| 192 | if ~isempty(regexp(s,'\<\d+\>','ONCE'))
|
---|
[327] | 193 | % switch s(1)
|
---|
| 194 | % case '0'
|
---|
[318] | 195 | type=num2str(1,['%0' num2str(length(s)) 'd']);
|
---|
[327] | 196 | % otherwise
|
---|
| 197 | % type='1';
|
---|
| 198 | % end
|
---|
[318] | 199 | else
|
---|
| 200 | type='';
|
---|
| 201 | return
|
---|
| 202 | end
|
---|
| 203 | end
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | function [j1,j2]=get_value(s)
|
---|
| 207 | % returns the type of a label string:
|
---|
| 208 | % for numbers, whether filled with 0 or not.
|
---|
| 209 | % for letters, either a, A or ab, AB.
|
---|
| 210 | j1=[];
|
---|
| 211 | j2=[];
|
---|
| 212 |
|
---|
| 213 | switch lower(s)
|
---|
| 214 | case {'a'}
|
---|
| 215 | j1=1;
|
---|
| 216 | case {'b','B'}
|
---|
| 217 | j1=2;
|
---|
| 218 | case 'ab'
|
---|
| 219 | j1=1;j2=2;
|
---|
| 220 | otherwise
|
---|
| 221 | return
|
---|
| 222 | end
|
---|
| 223 |
|
---|
| 224 |
|
---|
[327] | 225 |
|
---|
| 226 | function test(name)
|
---|
| 227 | fprintf([...
|
---|
| 228 | '######################################################\n'...
|
---|
| 229 | ' Test for fileparts_uvmat \n'...
|
---|
| 230 | '######################################################\n'...
|
---|
| 231 | ]);
|
---|
| 232 |
|
---|
| 233 | if exist('name','var')
|
---|
| 234 | FileName_list={name};
|
---|
| 235 | else
|
---|
| 236 | FileName_list={...
|
---|
| 237 | 'Image1a.png'...
|
---|
| 238 | 'toto'...
|
---|
| 239 | 'B011_1.png'...
|
---|
| 240 | 'B001.png'...
|
---|
| 241 | 'B55a.tif'...
|
---|
| 242 | 'B002B.tiff'...
|
---|
| 243 | 'B_001.png'...
|
---|
| 244 | 'B_3331AB.png'...
|
---|
| 245 | 'aa45_2.png'...
|
---|
| 246 | 'B005.png'...
|
---|
| 247 | 'Image_3.jpg'...
|
---|
| 248 | 'Image_3-4.jpg'...
|
---|
| 249 | 'Image_3-4_2.jpg'...
|
---|
| 250 | 'Image_5_3-4.jpg'...
|
---|
| 251 | 'Image_3_ab.jpg'...
|
---|
| 252 | 'Image005AB.jpg'...
|
---|
| 253 | 'Image_3_ab.jpg'...
|
---|
| 254 | 'Image_3a-b.jpg'...
|
---|
| 255 | 'Image3_a.jpg'...
|
---|
| 256 | 'movie57.avi'...
|
---|
| 257 | };
|
---|
| 258 | end
|
---|
| 259 |
|
---|
| 260 | for FileName=FileName_list
|
---|
| 261 | % [RootPath,RootFile,i1,i2,str_a,str_b,Ext,NomType,SubDir]=name2display(FileName{1});
|
---|
| 262 | [~,RootFile_bis,i1_bis,i2_bis,j1_bis,j2_bis,~,NomType_bis,SubDir_bis]=...
|
---|
| 263 | fileparts_uvmat(FileName{1});
|
---|
| 264 | fprintf([...
|
---|
| 265 | 'File name : ' FileName{1} '\n'...
|
---|
| 266 | ' NomType : ' NomType_bis '\n'...
|
---|
| 267 | ' RootFile : ' RootFile_bis '\n'...
|
---|
| 268 | ' i1 / i2 : ' num2str(i1_bis) ' / ' num2str(i2_bis) '\n'...
|
---|
| 269 | ' j1 / j2 : ' num2str(j1_bis) ' / ' num2str(j2_bis) '\n'...
|
---|
| 270 | ]);
|
---|
| 271 | end
|
---|
| 272 |
|
---|
| 273 |
|
---|