[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:
|
---|
[334] | 6 | %RootPath: path to the base file
|
---|
| 7 | %SubDir: name of the SubDirectory for netcdf files (NomTypes with index pairs 1-2 or ab )
|
---|
[327] | 8 | %RootFile: FileName without appendix
|
---|
| 9 | %i1: first number i
|
---|
| 10 | %i2: second number i (only for .nc files)
|
---|
| 11 | %j1: first number j
|
---|
| 12 | %j2: second number j (only for .nc files)
|
---|
[334] | 13 | %FileExt: file Extension
|
---|
[327] | 14 | %NomType: char chain characterizing the file nomenclature: with values
|
---|
[334] | 15 | % NomType='': constant name [filebase FileExt] (default output if 'NomType' is undefined)
|
---|
[327] | 16 | % NomType='*':constant name for a file representing a series (e.g. avi movie)
|
---|
| 17 | % NomType='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
|
---|
| 18 | % 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)
|
---|
| 19 | % NomType='1_1','01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa45_2.png')
|
---|
| 20 | % NomType='1-1': from pairs from a single index (e.g. 'aa_45-47.nc')
|
---|
[334] | 21 | % NomType='1_1-2': pairs of j indices (e.g. 'aa_45_2-3.nc')
|
---|
| 22 | % NomType='1-2_1': pairs of i indices (e.g. 'aa_45-46_2.nc')
|
---|
| 23 | % NomType='1_ab','01_ab','01ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), FileExt='.nc'
|
---|
[327] | 24 | %SubDir: name of the SubDirectory for netcdf files
|
---|
[318] | 25 | %
|
---|
| 26 | %INPUT:
|
---|
[327] | 27 | %FileInput: complete name of the file, including path
|
---|
[318] | 28 |
|
---|
[334] | 29 | function [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomType]=fileparts_uvmat(FileInput)
|
---|
| 30 | RootPath='';
|
---|
| 31 | SubDir='';
|
---|
| 32 | RootFile='';
|
---|
[327] | 33 | i1=[];
|
---|
[318] | 34 | i2=[];
|
---|
| 35 | j1=[];
|
---|
| 36 | j2=[];
|
---|
[334] | 37 | FileExt='';
|
---|
[327] | 38 | NomType='';
|
---|
[318] | 39 |
|
---|
[334] | 40 |
|
---|
| 41 |
|
---|
| 42 | %% display help and test function in the absence of input arument
|
---|
[327] | 43 | if ~exist('FileInput','var')
|
---|
| 44 | help fileparts_uvmat;
|
---|
| 45 | test();
|
---|
| 46 | return
|
---|
| 47 | end
|
---|
[318] | 48 |
|
---|
[334] | 49 | [RootPath,FileName,FileExt]=fileparts(FileInput);
|
---|
| 50 | RootFile=FileName;%default
|
---|
| 51 | % switch FileExt
|
---|
| 52 | % case '.avi'
|
---|
| 53 | % NomType='*';
|
---|
| 54 | % return
|
---|
| 55 | % case {'.tif','.tiff'}
|
---|
| 56 | % if exist(FileInput,'file')
|
---|
| 57 | % info=iminfo(FileInput);
|
---|
| 58 | % if length(info)>1
|
---|
| 59 | % NomType='*';
|
---|
| 60 | % return
|
---|
| 61 | % end
|
---|
| 62 | % end
|
---|
| 63 | % end
|
---|
[327] | 64 |
|
---|
[318] | 65 | % \D not a digit
|
---|
| 66 | % \d digit
|
---|
| 67 |
|
---|
| 68 |
|
---|
[334] | 69 | %% recursive test on FileName starting from the end
|
---|
| 70 | % case of pure number
|
---|
| 71 | if ~isnan(str2double(FileName))
|
---|
| 72 | RootFile='';
|
---|
| 73 | i1=str2double(FileName);
|
---|
| 74 | return
|
---|
| 75 | end
|
---|
[327] | 76 | % test whether FileName ends with a number or not
|
---|
[334] | 77 | r=regexp(FileName,'.*\D(?<num1>\d+)$','names');% \D = not a digit, \d =digit
|
---|
[318] | 78 |
|
---|
[327] | 79 | if ~isempty(r)% FileName end matches num1
|
---|
[318] | 80 | num1=r.num1;
|
---|
[334] | 81 | r=regexp(FileName,['.*\D(?<num2>\d+)(?<delim1>[-_])' num1 '$'],'names');
|
---|
[327] | 82 | if ~isempty(r)% FileName end matches num2+delim1+num1
|
---|
[318] | 83 | delim1=r.delim1;
|
---|
| 84 | num2=r.num2;
|
---|
[334] | 85 | r=regexp(FileName,['.*\D(?<num3>\d+)(?<delim2>[-_])' num2 delim1 num1 '$'],'names');
|
---|
[327] | 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
|
---|
[334] | 119 | NomType=regexprep(NomType,'-1','-2'); %set 1-2 instead of 1-1
|
---|
[318] | 120 | else% only one number at the end
|
---|
[327] | 121 | i1=str2double(num1);
|
---|
| 122 | NomType=get_type(num1);
|
---|
| 123 | RootFile=regexprep(FileName,num1,'');
|
---|
[318] | 124 | end
|
---|
[327] | 125 | else% FileName ends with a letter
|
---|
[334] | 126 | %r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
|
---|
| 127 | NomType='';
|
---|
| 128 | r=regexp(RootFile,'\D(?<num1>\d+)(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
|
---|
[318] | 129 | if ~isempty(r)
|
---|
[334] | 130 | NomType=get_type(r.end_string);
|
---|
| 131 | RootFile=regexprep(RootFile,[r.num1 r.end_string '$'],'');
|
---|
| 132 | else % case with separator '_'
|
---|
| 133 | r=regexp(RootFile,'\D(?<num1>\d+)_(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
|
---|
[318] | 134 | if ~isempty(r)
|
---|
[334] | 135 | NomType=['_' get_type(r.end_string)];
|
---|
| 136 | RootFile=regexprep(RootFile,[r.num1 '_' r.end_string '$'],'');
|
---|
| 137 | end
|
---|
[318] | 138 | end
|
---|
[334] | 139 | if ~isempty(NomType)
|
---|
| 140 | [j1,j2]=get_value(r.end_string);
|
---|
| 141 | i1=str2double(r.num1);
|
---|
| 142 | NomType=[get_type(r.num1) NomType];
|
---|
| 143 | r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
|
---|
| 144 | if ~isempty(r)
|
---|
| 145 | SubDir=r.subdir;
|
---|
| 146 | RootPath=r.newrootpath;
|
---|
| 147 | end
|
---|
| 148 | end
|
---|
[318] | 149 | end
|
---|
| 150 |
|
---|
[334] | 151 | %% suppress '_' at the end of RootFile, put it on NomType
|
---|
| 152 | if strcmp(RootFile(end),'_')
|
---|
| 153 | RootFile(end)=[];
|
---|
| 154 | NomType=['_' NomType];
|
---|
[333] | 155 | end
|
---|
[318] | 156 |
|
---|
[334] | 157 | % if ~isempty(regexp(NomType,'-|ab|AB'))
|
---|
| 158 | % r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
|
---|
| 159 | % if ~isempty(r)
|
---|
| 160 | % SubDir=r.subdir;
|
---|
| 161 | % RootPath=r.newrootpath;
|
---|
| 162 | % end
|
---|
| 163 | % end
|
---|
[318] | 164 |
|
---|
| 165 |
|
---|
| 166 |
|
---|
[334] | 167 |
|
---|
[318] | 168 | function type=get_type(s)
|
---|
| 169 | % returns the type of a label string:
|
---|
| 170 | % for numbers, whether filled with 0 or not.
|
---|
[334] | 171 | type='';%default
|
---|
[318] | 172 |
|
---|
[334] | 173 | if ~isempty(regexp(s,'\<\d+\>','ONCE'))
|
---|
| 174 | type=num2str(1,['%0' num2str(length(s)) 'd']);
|
---|
| 175 | else
|
---|
| 176 | code=double(s); % ascii code of the input string
|
---|
| 177 | if code >= 65 & code <= 90 % test on ascii code for capital letters
|
---|
| 178 | if length(s)==1
|
---|
| 179 | type='A';
|
---|
| 180 | elseif length(s)==2
|
---|
| 181 | type='AB';
|
---|
[318] | 182 | end
|
---|
[334] | 183 | elseif code >= 97 & code <= 122 % test on ascii code for small letters
|
---|
| 184 | if length(s)==1
|
---|
| 185 | type='a';
|
---|
| 186 | elseif length(s)==2
|
---|
| 187 | type='ab';
|
---|
| 188 | end
|
---|
| 189 | end
|
---|
[318] | 190 | end
|
---|
| 191 |
|
---|
| 192 |
|
---|
[334] | 193 |
|
---|
[318] | 194 | function [j1,j2]=get_value(s)
|
---|
[334] | 195 | % returns the value of a label string:
|
---|
[318] | 196 | % for numbers, whether filled with 0 or not.
|
---|
| 197 | % for letters, either a, A or ab, AB.
|
---|
| 198 | j1=[];
|
---|
| 199 | j2=[];
|
---|
[334] | 200 | code=double(s); % ascii code of the input string
|
---|
| 201 | if code >= 65 & code <= 90 % test on ascii code for capital letters
|
---|
| 202 | index=double(s)-64; %change capital letters to corresponding number in the alphabet
|
---|
| 203 | elseif code >= 97 & code <= 122 % test on ascii code for small letters
|
---|
| 204 | index=double(s)-96; %change small letters to corresponding number in the alphabet
|
---|
| 205 | else
|
---|
| 206 | index=str2num(s);
|
---|
[318] | 207 | end
|
---|
[334] | 208 | if ~isempty(index)
|
---|
| 209 | j1=index(1);
|
---|
| 210 | if length(index)==2
|
---|
| 211 | j2=index(2);
|
---|
| 212 | end
|
---|
| 213 | end
|
---|
[318] | 214 |
|
---|
| 215 |
|
---|
[327] | 216 | function test(name)
|
---|
| 217 | fprintf([...
|
---|
| 218 | '######################################################\n'...
|
---|
| 219 | ' Test for fileparts_uvmat \n'...
|
---|
| 220 | '######################################################\n'...
|
---|
| 221 | ]);
|
---|
| 222 |
|
---|
| 223 | if exist('name','var')
|
---|
| 224 | FileName_list={name};
|
---|
| 225 | else
|
---|
| 226 | FileName_list={...
|
---|
| 227 | 'Image1a.png'...
|
---|
| 228 | 'toto'...
|
---|
| 229 | 'B011_1.png'...
|
---|
| 230 | 'B001.png'...
|
---|
| 231 | 'B55a.tif'...
|
---|
| 232 | 'B002B.tiff'...
|
---|
| 233 | 'B_001.png'...
|
---|
| 234 | 'B_3331AB.png'...
|
---|
| 235 | 'aa45_2.png'...
|
---|
| 236 | 'B005.png'...
|
---|
| 237 | 'Image_3.jpg'...
|
---|
| 238 | 'Image_3-4.jpg'...
|
---|
| 239 | 'Image_3-4_2.jpg'...
|
---|
| 240 | 'Image_5_3-4.jpg'...
|
---|
| 241 | 'Image_3_ab.jpg'...
|
---|
[334] | 242 | 'Image005AD.jpg'...
|
---|
| 243 | 'Image_3_ac.jpg'...
|
---|
[327] | 244 | 'Image_3a-b.jpg'...
|
---|
| 245 | 'Image3_a.jpg'...
|
---|
| 246 | 'movie57.avi'...
|
---|
| 247 | };
|
---|
| 248 | end
|
---|
| 249 |
|
---|
| 250 | for FileName=FileName_list
|
---|
[334] | 251 | % [RootPath,RootFile,i1,i2,str_a,str_b,FileExt,NomType,SubDir]=name2display(FileName{1});
|
---|
| 252 | [~,SubDir,RootFile,i1,i2,j1,j2,~,NomType]=...
|
---|
[327] | 253 | fileparts_uvmat(FileName{1});
|
---|
| 254 | fprintf([...
|
---|
| 255 | 'File name : ' FileName{1} '\n'...
|
---|
[333] | 256 | ' NomType : ' NomType '\n'...
|
---|
| 257 | ' RootFile : ' RootFile '\n'...
|
---|
| 258 | ' i1 / i2 : ' num2str(i1) ' / ' num2str(i2) '\n'...
|
---|
| 259 | ' j1 / j2 : ' num2str(j1) ' / ' num2str(j2) '\n'...
|
---|
[327] | 260 | ]);
|
---|
| 261 | end
|
---|
| 262 |
|
---|
| 263 |
|
---|