1 | %'name2display': extracts the root name and field numbers from an input filename
|
---|
2 | %--------------------------------------------------------------------
|
---|
3 | %[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput)
|
---|
4 | %
|
---|
5 | %OUTPUT:
|
---|
6 | %filebasesub: filename without appendix
|
---|
7 | %field_count: string for the first number i
|
---|
8 | %str2: string for the second number i (only for .nc files)
|
---|
9 | %str_a: string for the first number j
|
---|
10 | %str_b:string for the second number j (only for .nc files)
|
---|
11 | %ext: file extension
|
---|
12 | %nom_type: char chain characterizing the file nomenclature: with values
|
---|
13 | % nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
|
---|
14 | % nom_type='*':constant name for a file representing a series (e.g. avi movie)
|
---|
15 | % nom_type='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
|
---|
16 | % nom_type='_1','_01','_001'...': series of files with a single index i with separator '_'(e.g. 'aa_045.png').
|
---|
17 | % nom_type='1a','1A','01a','01A',... with a numerical index and an index letter(e.g.'aa45b.png') (lower or upper case)
|
---|
18 | % nom_type='_1a','_1A','_01a','_01A',...: idem, with a separator '_' before the index
|
---|
19 | % nom_type='_1_1','_01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
|
---|
20 | % nom_type='_1-2': from pairs from a single index (e.g. 'aa_45-47.nc')
|
---|
21 | % nom_type='_1_1-2': pairs of j indices (e.g. 'aa_45_2-3.nc')
|
---|
22 | % nom_type='_1-2_j': pairs of i indices (e.g. 'aa_45-46_2.nc')
|
---|
23 | % nom_type='_1_ab','1_ab','01_ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), ext='.nc'
|
---|
24 | %subdir: name of the subdirectory for netcdf files
|
---|
25 | %
|
---|
26 | %INPUT:
|
---|
27 | %fileinput: complete name of the file, including path
|
---|
28 |
|
---|
29 | function [RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput)
|
---|
30 | % siz=length(fileinput);
|
---|
31 | % indcur=siz;
|
---|
32 | % default values:
|
---|
33 | % test_=0;
|
---|
34 | field_count='';%character string
|
---|
35 | str2='';
|
---|
36 | str_a='';
|
---|
37 | str_b='';
|
---|
38 | % ext='';
|
---|
39 | nom_type='';
|
---|
40 | subdir='';
|
---|
41 | %select file extension
|
---|
42 | [RootPath,RootFile,ext]=fileparts(fileinput);
|
---|
43 | indcur=length(RootFile);% nbre of characters in fileraw
|
---|
44 |
|
---|
45 | %recognize the name form
|
---|
46 | % filerawascii=double(RootFile);%ascci code
|
---|
47 | % val=(48>filerawascii)|(filerawascii>57); % test for the non-numerical characters
|
---|
48 | indsel=regexp(RootFile,'\D');% character indices of non numerical characters
|
---|
49 | filelit=RootFile(indsel);% fileraw name with numbers removed
|
---|
50 | nbchar=length(indsel);
|
---|
51 | if nbchar<4% put '*' before the name (remove at the end)
|
---|
52 | prefilelit(1:4-nbchar)='*';%insert 3_nbchar '*' in the file name
|
---|
53 | filelit=[prefilelit filelit];
|
---|
54 | indsel=[1:4-nbchar indsel+4-nbchar];
|
---|
55 | RootFile=[prefilelit RootFile];
|
---|
56 | indcur=indcur+4-nbchar;
|
---|
57 | end
|
---|
58 | separ3=indsel(end);% index of last non numerical character in fileraw
|
---|
59 | separ2=indsel(end-1);% index of previous non numerical character
|
---|
60 | separ1=indsel(end-2);
|
---|
61 | separ0=indsel(end-3);
|
---|
62 | num1='';num2='';num3='';
|
---|
63 | if separ1>=separ0+1,num0=RootFile(separ0+1:separ1-1);end
|
---|
64 | if separ2>=separ1+1,num1=RootFile(separ1+1:separ2-1);end
|
---|
65 | if separ3>=separ2+1,num2=RootFile(separ2+1:separ3-1);end
|
---|
66 | if indcur>=separ3+1,num3=RootFile(separ3+1:indcur);end
|
---|
67 | last_str=RootFile(indcur);%last character in fileraw
|
---|
68 | last=double(last_str);%corresponding ascii code
|
---|
69 | penult=double(RootFile(indcur-1));%ascii code of the penultimate character
|
---|
70 | testsub=0; %default
|
---|
71 | % % case of an indexed series in a single file
|
---|
72 | % if strcmpi(ext,'.avi')
|
---|
73 | % nom_type='*';
|
---|
74 | % %case of a numerical index follewed by a lower case letter (e.g. a,b,c):
|
---|
75 | % %the penultimate character is a number and the last one a letter (lower case: last >= 97 && last <= 122
|
---|
76 | % % capital
|
---|
77 | % % letter: last >= 65 && last <= 90)
|
---|
78 | if penult >= 48 && penult <= 57 && ((last >= 65 && last <= 90)||(last >= 97 && last <= 122))
|
---|
79 | str_a=last_str; %extract appendix a,b,c... or A,B,C... as output.
|
---|
80 | ind_end=indcur-1; %current index just before the suffix letter
|
---|
81 | indices_root=regexp(RootFile(1:indcur-1),'\D');%detect non digit characters
|
---|
82 | indcur=max(indices_root);
|
---|
83 | field_count=RootFile(indcur+1:ind_end);
|
---|
84 | charstring=['%0' num2str(length(field_count)) 'd'];
|
---|
85 | nom_type=num2str(1,charstring);
|
---|
86 | if strcmp(RootFile(indcur),'_')
|
---|
87 | nom_type=['_' nom_type];
|
---|
88 | indcur=indcur-1;
|
---|
89 | end
|
---|
90 | if (last >= 65 && last <= 90)
|
---|
91 | nom_type=[nom_type 'A'];
|
---|
92 | else
|
---|
93 | nom_type=[nom_type 'a'];
|
---|
94 | end
|
---|
95 | elseif strcmp(filelit(end-2:end),'-_-_')%new nomenclature appendix num1-num2_num_a-num_b
|
---|
96 | field_count=num0;
|
---|
97 | str2=num1;
|
---|
98 | str_a=num2;
|
---|
99 | str_b=num3;
|
---|
100 | nom_type='_1-2_1-2';
|
---|
101 | testsub=1;
|
---|
102 | indcur=separ0-1;
|
---|
103 | elseif strcmp(filelit(end-2:end),'_-_')%new nomenclature appendix num1-num2_num_a
|
---|
104 | field_count=num1;
|
---|
105 | str2=num2;
|
---|
106 | str_a=num3;
|
---|
107 | nom_type='_1-2_1';
|
---|
108 | testsub=1;
|
---|
109 | indcur=separ1-1;
|
---|
110 | elseif strcmp(filelit(end-2:end),'__-')%new nomenclature appendix num1_num2-num2
|
---|
111 | indcur=separ1-1;
|
---|
112 | field_count=num1;
|
---|
113 | str_a=num2;
|
---|
114 | str_b=num3;
|
---|
115 | nom_type='_1_1-2';
|
---|
116 | testsub=1;
|
---|
117 | elseif strcmp(filelit(end-1:end),'_-')
|
---|
118 | indcur=separ2-1;
|
---|
119 | field_count=num2;
|
---|
120 | str2=num3;
|
---|
121 | str_a='';
|
---|
122 | nom_type='_1-2';
|
---|
123 | testsub=1;
|
---|
124 | elseif strcmp(filelit(end-1:end),'__')
|
---|
125 | indcur=separ2-1;
|
---|
126 | field_count=num2;
|
---|
127 | str2='';
|
---|
128 | str_a=num3;
|
---|
129 | nom_type='_1_1';
|
---|
130 | elseif strcmp(filelit(end),'_')
|
---|
131 | indcur=separ3-1;
|
---|
132 | str2='';
|
---|
133 | str_a='';
|
---|
134 | %detect zeros before the number
|
---|
135 | field_count=RootFile(separ3+1:end);% set the selected field number'%03d'
|
---|
136 | charstring=['%0' num2str(length(field_count)) 'd'];
|
---|
137 | nom_type=['_' num2str(1,charstring)];
|
---|
138 | elseif RootFile(indcur-2)=='_'% search appendix a,b,c,d
|
---|
139 | lasts=RootFile(indcur-1:indcur);
|
---|
140 | % if isequal(length(last),2)
|
---|
141 | str_a=lasts(1);%put appendix a,b,c, ou d
|
---|
142 | str_b=lasts(2);%put appendix a,b,c, ou d
|
---|
143 | separ0=indsel(end-3);
|
---|
144 | field_count=RootFile(separ0+1:separ1-1);
|
---|
145 | indcur=separ0;
|
---|
146 | if double(lasts) >= 97 & double(lasts)<= 122
|
---|
147 | nom_type='_ab';
|
---|
148 | testsub=1;
|
---|
149 | elseif double(lasts) >= 65 & double(lasts) <= 90
|
---|
150 | nom_type='_AB';
|
---|
151 | testsub=1;
|
---|
152 | end
|
---|
153 | charstring=['%0' num2str(length(field_count)) 'd'];
|
---|
154 | nom_type=[num2str(1,charstring) nom_type];
|
---|
155 | % end
|
---|
156 | %search for other names with counter
|
---|
157 | else
|
---|
158 | if length(ext)>1
|
---|
159 | num=1;count=0; % extract the numerical appendix
|
---|
160 | while num==1;
|
---|
161 | filascii=double(RootFile(indcur));
|
---|
162 | if (48>filascii)||(filascii>57); % select the non-numerical characters
|
---|
163 | num=0;
|
---|
164 | else
|
---|
165 | indcur=indcur-1; count=count+1;
|
---|
166 | end
|
---|
167 | end
|
---|
168 | if count~=0
|
---|
169 | field_count=RootFile(indcur+1:indcur+count);% set the selected field number'%03d'
|
---|
170 | charstring=['%0' num2str(length(field_count)) 'd'];
|
---|
171 | nom_type=num2str(1,charstring);
|
---|
172 | end
|
---|
173 | end
|
---|
174 | end
|
---|
175 | %select the root name in the file_input window
|
---|
176 | RootFile=RootFile(1:indcur);
|
---|
177 | if nbchar<4% put '*' before the name (remove at the end)
|
---|
178 | RootFile(1:4-nbchar)=[];
|
---|
179 | end
|
---|
180 | if testsub
|
---|
181 | [RootPath,subdir,extdir]=fileparts(RootPath);
|
---|
182 | subdir=[subdir extdir];
|
---|
183 | end
|
---|
184 |
|
---|
185 | %resolve ambigous nomenclature types when the number of 0 is unknown (type %0...):
|
---|
186 | % ind_zero=findstr('0',nom_type);
|
---|
187 | % nb_zero=numel(ind_zero);
|
---|
188 | % if ~isempty(ind_zero)
|
---|
189 | % for itest=0:nb_zero-1
|
---|
190 | % filename=name_generator(fullfile(RootPath,RootFile),1,1,ext,nom_type,1,1,1,subdir)
|
---|
191 | % if exist(filename,'file')
|
---|
192 | % break
|
---|
193 | % else
|
---|
194 | % nom_type(ind_zero(1))=[]; % remove a zero in nom_type
|
---|
195 | % end
|
---|
196 | % end
|
---|
197 | % end
|
---|