source: trunk/src/fileparts_uvmat.m @ 339

Last change on this file since 339 was 339, checked in by sommeria, 13 years ago

name2display replaced by fileparts_uvmat and find_file_series in series.
name2display partially replaced in civ.m (to be continued)
series.fig reorganized with panels.

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