[575] | 1 | %'fileparts_uvmat': splits a file name in root and indices 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 | %% display help and test function in the absence of input arument
|
---|
[327] | 41 | if ~exist('FileInput','var')
|
---|
| 42 | help fileparts_uvmat;
|
---|
| 43 | test();
|
---|
| 44 | return
|
---|
| 45 | end
|
---|
[318] | 46 |
|
---|
[339] | 47 | %% default root name output
|
---|
[334] | 48 | [RootPath,FileName,FileExt]=fileparts(FileInput);
|
---|
[339] | 49 | RootFile=FileName;
|
---|
[327] | 50 |
|
---|
[339] | 51 | %% case of input file name which is a pure number
|
---|
[334] | 52 | if ~isnan(str2double(FileName))
|
---|
| 53 | RootFile='';
|
---|
| 54 | i1=str2double(FileName);
|
---|
| 55 | return
|
---|
| 56 | end
|
---|
[339] | 57 |
|
---|
| 58 | %% recursive test on FileName starting from the end
|
---|
[327] | 59 | % test whether FileName ends with a number or not
|
---|
[334] | 60 | r=regexp(FileName,'.*\D(?<num1>\d+)$','names');% \D = not a digit, \d =digit
|
---|
[318] | 61 |
|
---|
[327] | 62 | if ~isempty(r)% FileName end matches num1
|
---|
[318] | 63 | num1=r.num1;
|
---|
[334] | 64 | r=regexp(FileName,['.*\D(?<num2>\d+)(?<delim1>[-_])' num1 '$'],'names');
|
---|
[327] | 65 | if ~isempty(r)% FileName end matches num2+delim1+num1
|
---|
[318] | 66 | delim1=r.delim1;
|
---|
| 67 | num2=r.num2;
|
---|
[352] | 68 | switch delim1
|
---|
| 69 | case '_'
|
---|
| 70 | delim2_to_match='-';
|
---|
| 71 | case '-'
|
---|
| 72 | delim2_to_match='_';
|
---|
| 73 | end
|
---|
| 74 | r=regexp(FileName,['.*\D(?<num3>\d+)(?<delim2>' delim2_to_match ')' num2 delim1 num1 '$'],'names');
|
---|
[346] | 75 | if ~isempty(r) % FileName end matches num3 delim2 num2 delim1 num1
|
---|
[318] | 76 | delim2=r.delim2;
|
---|
| 77 | num3=r.num3;
|
---|
| 78 | switch delim1
|
---|
| 79 | case '_'
|
---|
[327] | 80 | j1=str2double(num1);
|
---|
[318] | 81 | switch delim2
|
---|
| 82 | case '-'
|
---|
[327] | 83 | i1=str2double(num3);
|
---|
| 84 | i2=str2double(num2);
|
---|
[318] | 85 | end
|
---|
| 86 | case '-'
|
---|
[327] | 87 | j1=str2double(num2);
|
---|
| 88 | j2=str2double(num1);
|
---|
[318] | 89 | switch delim2
|
---|
| 90 | case '_'
|
---|
[327] | 91 | i1=str2double(num3);
|
---|
[318] | 92 | end
|
---|
| 93 | end
|
---|
[327] | 94 | NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
|
---|
[384] | 95 | RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1 '$'],'');
|
---|
[318] | 96 | else
|
---|
| 97 | switch delim1
|
---|
| 98 | case '_'
|
---|
[327] | 99 | i1=str2double(num2);
|
---|
| 100 | j1=str2double(num1);
|
---|
[318] | 101 | case '-'
|
---|
[327] | 102 | i1=str2double(num2);
|
---|
| 103 | i2=str2double(num1);
|
---|
[318] | 104 | end
|
---|
[327] | 105 | NomType=[get_type(num2) delim1 get_type(num1)];
|
---|
[379] | 106 | RootFile=regexprep(FileName,[num2 delim1 num1 '$'],'');
|
---|
[318] | 107 | end
|
---|
[334] | 108 | NomType=regexprep(NomType,'-1','-2'); %set 1-2 instead of 1-1
|
---|
[318] | 109 | else% only one number at the end
|
---|
[327] | 110 | i1=str2double(num1);
|
---|
| 111 | NomType=get_type(num1);
|
---|
[384] | 112 | RootFile=regexprep(FileName,[num1 '$'],'');
|
---|
[318] | 113 | end
|
---|
[327] | 114 | else% FileName ends with a letter
|
---|
[334] | 115 | %r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
|
---|
| 116 | NomType='';
|
---|
[405] | 117 | r=regexp(RootFile,'\D*(?<num1>\d+)(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
|
---|
[318] | 118 | if ~isempty(r)
|
---|
[334] | 119 | NomType=get_type(r.end_string);
|
---|
| 120 | RootFile=regexprep(RootFile,[r.num1 r.end_string '$'],'');
|
---|
| 121 | else % case with separator '_'
|
---|
| 122 | r=regexp(RootFile,'\D(?<num1>\d+)_(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
|
---|
[318] | 123 | if ~isempty(r)
|
---|
[334] | 124 | NomType=['_' get_type(r.end_string)];
|
---|
| 125 | RootFile=regexprep(RootFile,[r.num1 '_' r.end_string '$'],'');
|
---|
| 126 | end
|
---|
[318] | 127 | end
|
---|
[334] | 128 | if ~isempty(NomType)
|
---|
| 129 | [j1,j2]=get_value(r.end_string);
|
---|
| 130 | i1=str2double(r.num1);
|
---|
| 131 | NomType=[get_type(r.num1) NomType];
|
---|
| 132 | end
|
---|
[318] | 133 | end
|
---|
| 134 |
|
---|
[334] | 135 | %% suppress '_' at the end of RootFile, put it on NomType
|
---|
[390] | 136 | % if strcmp(RootFile(end),'_')
|
---|
| 137 | % RootFile(end)=[];
|
---|
| 138 | detect=regexp(RootFile,'_$'); %detect '_' at the end of RootFILE
|
---|
| 139 | if ~isempty(detect)
|
---|
| 140 | RootFile=regexprep(RootFile,'_$','');
|
---|
[334] | 141 | NomType=['_' NomType];
|
---|
[333] | 142 | end
|
---|
[318] | 143 |
|
---|
[339] | 144 | %% extract subdirectory for pairs i1-i2 or j1-j2 (or ab, AB)
|
---|
[437] | 145 | % if ~isempty(i2) || ~isempty(j2)
|
---|
[339] | 146 | r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
|
---|
| 147 | if ~isempty(r)
|
---|
| 148 | SubDir=r.subdir;
|
---|
| 149 | RootPath=r.newrootpath;
|
---|
| 150 | end
|
---|
[334] | 151 | % end
|
---|
[318] | 152 |
|
---|
| 153 |
|
---|
[437] | 154 |
|
---|
[318] | 155 | function type=get_type(s)
|
---|
| 156 | % returns the type of a label string:
|
---|
| 157 | % for numbers, whether filled with 0 or not.
|
---|
[334] | 158 | type='';%default
|
---|
[318] | 159 |
|
---|
[334] | 160 | if ~isempty(regexp(s,'\<\d+\>','ONCE'))
|
---|
| 161 | type=num2str(1,['%0' num2str(length(s)) 'd']);
|
---|
| 162 | else
|
---|
| 163 | code=double(s); % ascii code of the input string
|
---|
| 164 | if code >= 65 & code <= 90 % test on ascii code for capital letters
|
---|
| 165 | if length(s)==1
|
---|
| 166 | type='A';
|
---|
| 167 | elseif length(s)==2
|
---|
| 168 | type='AB';
|
---|
[318] | 169 | end
|
---|
[334] | 170 | elseif code >= 97 & code <= 122 % test on ascii code for small letters
|
---|
| 171 | if length(s)==1
|
---|
| 172 | type='a';
|
---|
| 173 | elseif length(s)==2
|
---|
| 174 | type='ab';
|
---|
| 175 | end
|
---|
| 176 | end
|
---|
[318] | 177 | end
|
---|
| 178 |
|
---|
| 179 |
|
---|
[334] | 180 |
|
---|
[318] | 181 | function [j1,j2]=get_value(s)
|
---|
[334] | 182 | % returns the value of a label string:
|
---|
[318] | 183 | % for numbers, whether filled with 0 or not.
|
---|
| 184 | % for letters, either a, A or ab, AB.
|
---|
| 185 | j1=[];
|
---|
| 186 | j2=[];
|
---|
[334] | 187 | code=double(s); % ascii code of the input string
|
---|
| 188 | if code >= 65 & code <= 90 % test on ascii code for capital letters
|
---|
| 189 | index=double(s)-64; %change capital letters to corresponding number in the alphabet
|
---|
| 190 | elseif code >= 97 & code <= 122 % test on ascii code for small letters
|
---|
| 191 | index=double(s)-96; %change small letters to corresponding number in the alphabet
|
---|
| 192 | else
|
---|
| 193 | index=str2num(s);
|
---|
[318] | 194 | end
|
---|
[334] | 195 | if ~isempty(index)
|
---|
| 196 | j1=index(1);
|
---|
| 197 | if length(index)==2
|
---|
| 198 | j2=index(2);
|
---|
| 199 | end
|
---|
| 200 | end
|
---|
[318] | 201 |
|
---|
| 202 |
|
---|
[327] | 203 | function test(name)
|
---|
| 204 | fprintf([...
|
---|
| 205 | '######################################################\n'...
|
---|
| 206 | ' Test for fileparts_uvmat \n'...
|
---|
| 207 | '######################################################\n'...
|
---|
| 208 | ]);
|
---|
| 209 |
|
---|
| 210 | if exist('name','var')
|
---|
| 211 | FileName_list={name};
|
---|
| 212 | else
|
---|
| 213 | FileName_list={...
|
---|
| 214 | 'Image1a.png'...
|
---|
| 215 | 'toto'...
|
---|
| 216 | 'B011_1.png'...
|
---|
| 217 | 'B001.png'...
|
---|
| 218 | 'B55a.tif'...
|
---|
| 219 | 'B002B.tiff'...
|
---|
| 220 | 'B_001.png'...
|
---|
| 221 | 'B_3331AB.png'...
|
---|
| 222 | 'aa45_2.png'...
|
---|
| 223 | 'B005.png'...
|
---|
| 224 | 'Image_3.jpg'...
|
---|
| 225 | 'Image_3-4.jpg'...
|
---|
| 226 | 'Image_3-4_2.jpg'...
|
---|
| 227 | 'Image_5_3-4.jpg'...
|
---|
| 228 | 'Image_3_ab.jpg'...
|
---|
[334] | 229 | 'Image005AD.jpg'...
|
---|
| 230 | 'Image_3_ac.jpg'...
|
---|
[327] | 231 | 'Image_3a-b.jpg'...
|
---|
| 232 | 'Image3_a.jpg'...
|
---|
| 233 | 'movie57.avi'...
|
---|
[346] | 234 | 'merged_20_12_1.png'...
|
---|
[327] | 235 | };
|
---|
| 236 | end
|
---|
| 237 |
|
---|
| 238 | for FileName=FileName_list
|
---|
[334] | 239 | % [RootPath,RootFile,i1,i2,str_a,str_b,FileExt,NomType,SubDir]=name2display(FileName{1});
|
---|
[353] | 240 | [tild,SubDir,RootFile,i1,i2,j1,j2,tild,NomType]=...
|
---|
[327] | 241 | fileparts_uvmat(FileName{1});
|
---|
| 242 | fprintf([...
|
---|
| 243 | 'File name : ' FileName{1} '\n'...
|
---|
[333] | 244 | ' NomType : ' NomType '\n'...
|
---|
| 245 | ' RootFile : ' RootFile '\n'...
|
---|
| 246 | ' i1 / i2 : ' num2str(i1) ' / ' num2str(i2) '\n'...
|
---|
| 247 | ' j1 / j2 : ' num2str(j1) ' / ' num2str(j2) '\n'...
|
---|
[327] | 248 | ]);
|
---|
| 249 | end
|
---|
| 250 |
|
---|
| 251 |
|
---|