source: trunk/src/series/relabel_i_j.m @ 599

Last change on this file since 599 was 599, checked in by sommeria, 11 years ago

various bug corrections. Steps further for civ_series (still development needed)

File size: 23.0 KB
Line 
1%'relabel_i_j': relabel an image series with two indices, and correct errors from the RDvision transfer program
2%------------------------------------------------------------------------
3% function ParamOut=relabel_i_j(Param)
4%------------------------------------------------------------------------
5%
6%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
7%
8%OUTPUT
9% ParamOut: sets options in the GUI series.fig needed for the function
10%
11%INPUT:
12% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
13% In batch mode, Param is the name of the corresponding xml file containing the same information
14% when Param.Action.RUN=0 (as activated when the current Action is selected
15% in series), the function ouput paramOut set the activation of the needed GUI elements
16%
17% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
18% see the current structure Param)
19%    .InputTable: cell of input file names, (several lines for multiple input)
20%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
21%    .OutputSubDir: name of the subdirectory for data outputs
22%    .OutputDirExt: directory extension for data outputs
23%    .Action: .ActionName: name of the current activated function
24%             .ActionPath:   path of the current activated function
25%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
26%             .RUN =0 for GUI input, =1 for function activation
27%             .RunMode='local','background', 'cluster': type of function  use
28%             
29%    .IndexRange: set the file or frame indices on which the action must be performed
30%    .FieldTransform: .TransformName: name of the selected transform function
31%                     .TransformPath:   path  of the selected transform function
32%    .InputFields: sub structure describing the input fields withfields
33%              .FieldName: name(s) of the field
34%              .VelType: velocity type
35%              .FieldName_1: name of the second field in case of two input series
36%              .VelType_1: velocity type of the second field in case of two input series
37%              .Coord_y: name of y coordinate variable
38%              .Coord_x: name of x coordinate variable
39%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41
42function ParamOut=relabel_i_j(Param) %default output=relabel_i_j(Param)
43
44%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
45if isstruct(Param) && isequal(Param.Action.RUN,0)
46    ParamOut.AllowInputSort='off';...% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
47    ParamOut.WholeIndexRange='on';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
48    ParamOut.NbSlice='one'; ...%nbre of slices, 'one' prevents splitting in several processes, ('off' by default)
49    ParamOut.VelType='off';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
50    ParamOut.FieldName='off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
51    ParamOut.FieldTransform = 'off';...%can use a transform function
52    ParamOut.ProjObject='off';...%can use projection object(option 'off'/'on',
53    ParamOut.Mask='off';...%can use mask option   (option 'off'/'on', 'off' by default)
54    ParamOut.OutputDirExt='';%set the output dir extension
55    if size(Param.InputTable,1)>1
56        msgbox_uvmat('WARNING', 'this function acts only on the first input file line')
57    end
58return
59end
60
61ParamOut=[];
62%%%%%%%%%%%% STANDARD PART  %%%%%%%%%%%%
63%% read input parameters from an xml file if input is a file name (batch mode)
64checkrun=1;
65if ischar(Param)
66    Param=xml2struct(Param);% read Param as input file (batch case)
67    checkrun=0;
68end
69
70%% root input file(s) and type
71RootPath=Param.InputTable(:,1);
72RootFile=Param.InputTable(:,3);
73SubDir=Param.InputTable(:,2);
74NomType=Param.InputTable(:,4);
75FileExt=Param.InputTable(:,5);
76
77% get the set of input file names (cell array filecell), and the lists of
78% input file or frame indices i1_series,i2_series,j1_series,j2_series
79[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
80 
81% numbers of slices and file indices
82
83nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
84nbfield_i=size(i1_series{1},2); %nb of fields for the i index
85nbfield=nbfield_j*nbfield_i; %total number of fields
86
87%determine the file type on each line from the first input file
88ImageTypeOptions={'image','multimage','mmreader','video'};
89
90if ~exist(filecell{1,1}','file')
91    msgbox_uvmat('ERROR',['the first input file ' filecell{1,1} ' does not exist'])
92    return
93end
94[FileType{1},FileInfo{1},MovieObject{1}]=get_file_type(filecell{1,1});
95CheckImage=~isempty(find(strcmp(FileType{1},ImageTypeOptions)));% =1 for images
96
97
98%% calibration data and timing: read the ImaDoc files
99mode=''; %default
100timecell={};
101itime=0;
102NbSlice_calib={};
103
104SubDirBase=regexprep(SubDir{1},'\..*','');%take the root part of SubDir, before the first dot '.'
105filexml=[fullfile(RootPath{1},SubDirBase) '.xml'];%new convention: xml at the level of the image folder
106if ~exist(filexml,'file')
107    filexml=[fullfile(RootPath{1},SubDir{1},RootFile{1}) '.xml']; % old convention: xml inside the image folder
108    if ~exist(filexml,'file')
109        filexml=[fullfile(RootPath{1},SubDir{1},RootFile{1}) '.civ']; % very old convention: .civ file
110        if ~exist(filexml,'file')
111            filexml='';
112        end
113    end
114end
115if ~isempty(filexml)
116    [XmlData,error]=imadoc2struct_special(filexml);
117end
118if isfield(XmlData,'Time')
119    itime=itime+1;
120    timecell{itime}=XmlData.Time;
121end
122if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
123    NbSlice_calib{1}=size(XmlData.GeometryCalib.SliceCoord,1);%nbre of slices for Zindex in phys transform
124    if ~isequal(NbSlice_calib{1},NbSlice_calib{1})
125        msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
126    end
127end
128
129%% check coincidence in time for several input file series
130% not relevant
131
132%% coordinate transform or other user defined transform
133%not relevant
134%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
135 % EDIT FROM HERE
136
137
138%% Set field names and velocity types
139% not relevant here
140
141%% Initiate output fields
142% not relevant here
143
144%% interactive input of specific parameters (for RDvision system)
145display('RDvision system')
146first_label=0; %image numbers start from 0
147if ~CheckImage || ~strcmp(NomType{1},'_000001')
148    msgbox_uvmat('WARNING','the input is not a file from RDvision: this function relabel_i_j has no action');%error message for directory creation
149    return
150else
151    answer=msgbox_uvmat('','this function will relabel the file series from RDvision from  and correct the xml file');%error message for directory creation
152    if ~strcmp(answer,'Yes')
153        return
154    end
155end
156
157%% copy and adapt the xml file
158NomTypeNew='_1_1';
159if ~isempty(XmlData)
160        t=xmltree(filexml);
161       
162        %update information on the first image name in the series
163        uid_Heading=find(t,'ImaDoc/Heading');
164        if isempty(uid_Heading)
165            [t,uid_Heading]=add(t,1,'element','Heading');
166        end
167        uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
168        j1=[];
169        if ~isempty(j1_series{1})
170            j1=j1_series{1};
171        end
172        ImageName=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},'_1_1',i1_series{1}(1),[],j1);
173        [pth,ImageName]=fileparts(ImageName);
174        ImageName=[ImageName '.png'];
175        if isempty(uid_ImageName)
176            [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
177        end
178        uid_value=children(t,uid_ImageName);
179        if isempty(uid_value)
180            t=add(t,uid_ImageName,'chardata',ImageName);%indicate  name of the first image, with ;png extension
181        else
182            t=set(t,uid_value(1),'value',ImageName);%indicate  name of the first image, with ;png extension
183        end
184       
185        %%%% correction RDvision %%%%
186        if isfield(XmlData,'NbDtj')
187            uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
188            uid_value=children(t,uid_NbDtj);
189            if ~isempty(uid_value)
190                t=set(t,uid_value(1),'value',num2str(XmlData.NbDtj));
191            end
192        end
193        if isfield(XmlData,'NbDtk')
194            uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
195            uid_value=children(t,uid_NbDtk);
196            if ~isempty(uid_value)
197                t=set(t,uid_value(1),'value',num2str(XmlData.NbDtk));
198            end
199        end
200        if isempty(j1_series{1}) && isfield(XmlData,'NbDti')
201            uid_Dti=find(t,'ImaDoc/Camera/BurstTiming/Dti');
202            t=add(t,uid_Dti,'chardata',num2str(XmlData.Dti));
203            uid_NbDti=find(t,'ImaDoc/Camera/BurstTiming/NbDti');
204            t=add(t,uid_NbDti,'chardata',num2str(XmlData.NbDti));
205            uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
206            uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
207            t=delete(t,uid_NbDtj);
208            t=delete(t,uid_NbDtk);
209            uid_Dtj=find(t,'ImaDoc/Camera/BurstTiming/Dtj');
210            uid_Dtk=find(t,'ImaDoc/Camera/BurstTiming/Dtk');
211            t=delete(t,uid_Dtj);
212            t=delete(t,uid_Dtk);
213            NomTypeNew='_1';
214        end
215            SubDirBase=regexprep(SubDir{1},'\..*','');%take the root part of SubDir, before the first dot '.'
216    filexml_new=[fullfile(RootPath{1},SubDirBase) '.xml'];
217       % save(t,filexml_new)
218end
219
220%% main loop on images
221%j1=[];%default
222nbfield2=size(XmlData.Time,2)
223for ifile=1:nbfield
224    if checkrun
225        stopstate=get(Param.RUNHandle,'BusyAction');
226        update_waitbar(Param.WaitbarHandle,ifile/nbfield)
227    else
228        stopstate='queue';
229    end
230    if ~isequal(stopstate,'queue')% enable STOP command
231        break
232    end
233    filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile));
234    j1=mod(ifile-1+first_label,nbfield2)+1;
235    i1=floor((ifile-1+first_label)/nbfield2)+1;
236    filename_new=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomTypeNew,i1,[],j1);
237    try
238        movefile(filename,filename_new);
239        [s,errormsg] = fileattrib(filename_new,'-w','a'); %set images to read only '-w' for all users ('a')
240        if ~s
241            msgbox_uvmat('ERROR',errormsg);
242            return
243        end
244    catch ME
245        msgbox_uvmat('ERROR',ME.message);
246        return
247    end
248   
249end
250
251%'imadoc2struct_special': reads the xml file for image documentation
252%------------------------------------------------------------------------
253% function [s,errormsg]=imadoc2struct_special(ImaDoc,option)
254%
255% OUTPUT:
256% s: structure representing ImaDoc
257%   s.Heading: information about the data hierarchical structure
258%   s.Time: matrix of times
259%   s.TimeUnit
260%  s.GeometryCalib: substructure containing the parameters for geometric calibration
261% errormsg: error message
262%
263% INPUT:
264% ImaDoc: full name of the xml input file with head key ImaDoc
265% option: ='GeometryCalib': read  the data of GeometryCalib, including source point coordinates
266
267function [s,errormsg]=imadoc2struct_special(ImaDoc,option)
268
269%% default input and output
270if ~exist('option','var')
271    option='*';
272end
273errormsg=[];%default
274s.Heading=[];%default
275s.Time=[]; %default
276s.TimeUnit=[]; %default
277s.GeometryCalib=[];
278tsai=[];%default
279
280%% opening the xml file
281if exist(ImaDoc,'file')~=2, errormsg=[ ImaDoc ' does not exist']; return;end;%input file does not exist
282try
283    t=xmltree(ImaDoc);
284catch
285    errormsg={[ImaDoc ' is not a valid xml file']; lasterr};
286    display(errormsg);
287    return
288end
289uid_root=find(t,'/ImaDoc');
290if isempty(uid_root), errormsg=[ImaDoc ' is not an image documentation file ImaDoc']; return; end;%not an ImaDoc .xml file
291
292
293%% Heading
294uid_Heading=find(t,'/ImaDoc/Heading');
295if ~isempty(uid_Heading),
296    uid_Campaign=find(t,'/ImaDoc/Heading/Campaign');
297    uid_Exp=find(t,'/ImaDoc/Heading/Experiment');
298    uid_Device=find(t,'/ImaDoc/Heading/Device');
299    uid_Record=find(t,'/ImaDoc/Heading/Record');
300    uid_FirstImage=find(t,'/ImaDoc/Heading/ImageName');
301    s.Heading.Campaign=get(t,children(t,uid_Campaign),'value');
302    s.Heading.Experiment=get(t,children(t,uid_Exp),'value');
303    s.Heading.Device=get(t,children(t,uid_Device),'value');
304    if ~isempty(uid_Record)
305        s.Heading.Record=get(t,children(t,uid_Record),'value');
306    end
307    s.Heading.ImageName=get(t,children(t,uid_FirstImage),'value');
308end
309
310%% Camera  and timing
311if strcmp(option,'*') || strcmp(option,'Camera')
312    uid_Camera=find(t,'/ImaDoc/Camera');
313    if ~isempty(uid_Camera)
314        uid_ImageSize=find(t,'/ImaDoc/Camera/ImageSize');
315        if ~isempty(uid_ImageSize);
316            ImageSize=get(t,children(t,uid_ImageSize),'value');
317            xindex=findstr(ImageSize,'x');
318            if length(xindex)>=2
319                s.Npx=str2double(ImageSize(1:xindex(1)-1));
320                s.Npy=str2double(ImageSize(xindex(1)+1:xindex(2)-1));
321            end
322        end
323        uid_TimeUnit=find(t,'/ImaDoc/Camera/TimeUnit');
324        if ~isempty(uid_TimeUnit)
325            s.TimeUnit=get(t,children(t,uid_TimeUnit),'value');
326        end
327        uid_BurstTiming=find(t,'/ImaDoc/Camera/BurstTiming');
328        if ~isempty(uid_BurstTiming)
329            for k=1:length(uid_BurstTiming)
330                subt=branch(t,uid_BurstTiming(k));%subtree under BurstTiming
331                % reading Dtk
332                Frequency=get_value(subt,'/BurstTiming/FrameFrequency',1);
333                Dtj=get_value(subt,'/BurstTiming/Dtj',[]);
334                Dtj=Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
335                NbDtj=get_value(subt,'/BurstTiming/NbDtj',[]);
336                %%%% correction RDvision %%%%
337%                 NbDtj=NbDtj/numel(Dtj);
338%                 s.NbDtj=NbDtj;
339%                 %%%%
340                Dti=get_value(subt,'/BurstTiming/Dti',[]);
341                NbDti=get_value(subt,'/BurstTiming/NbDti',1);
342                 %%%% correction RDvision %%%%
343                if isempty(Dti)% series
344                     Dti=Dtj;
345                      NbDti=NbDtj;
346                     Dtj=[];
347                     s.Dti=Dti;
348                     s.NbDti=NbDti;
349                else
350                    % NbDtj=NbDtj/numel(Dtj);%bursts
351                    if ~isempty(NbDtj)
352                    s.NbDtj=NbDtj/numel(Dtj);%bursts;
353                    else
354                        s.NbDtj=1;
355                    end
356                end
357                %%%% %%%%
358                Dti=Dti/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
359
360                Time_val=get_value(subt,'/BurstTiming/Time',0);%time in TimeUnit
361                if ~isempty(Dti)
362                    Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
363                    Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
364                end
365                if ~isempty(Dtj)
366                    Dtj=reshape(Dtj'*ones(1,s.NbDtj),1,s.NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
367                    Dtj=[0 Dtj];
368                    Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
369                end
370                % reading Dtk
371                Dtk=get_value(subt,'/BurstTiming/Dtk',[]);
372                NbDtk=get_value(subt,'/BurstTiming/NbDtk',1);
373                %%%% correction RDvision %%%%
374                if ~isequal(NbDtk,1)
375                    NbDtk=-1+(NbDtk+1)/(NbDti+1);
376                end
377                s.NbDtk=NbDtk;
378                %%%%%
379                if isempty(Dtk)
380                    s.Time=[s.Time;Time_val];
381                else
382                    for kblock=1:NbDtk+1
383                        Time_val_k=Time_val+(kblock-1)*Dtk;
384                        s.Time=[s.Time;Time_val_k];
385                    end
386                end
387            end
388        end
389    end
390end
391
392%% motor
393if strcmp(option,'*') || strcmp(option,'GeometryCalib')
394    uid_subtree=find(t,'/ImaDoc/TranslationMotor');
395    if length(uid_subtree)==1
396        subt=branch(t,uid_subtree);%subtree under GeometryCalib
397       [s.TranslationMotor,errormsg]=read_subtree(subt,{'Nbslice','ZStart','ZEnd'},[1 1 1],[1 1 1]);
398    end
399end
400%%  geometric calibration
401if strcmp(option,'*') || strcmp(option,'GeometryCalib')
402    uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
403    if ~isempty(uid_GeometryCalib)
404        if length(uid_GeometryCalib)>1
405            errormsg=['More than one GeometryCalib in ' filecivxml];
406            return
407        end
408        subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
409        cont=get(subt,1,'contents');
410        if ~isempty(cont)
411            uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
412            if isequal(length(uid_CalibrationType),1)
413                tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
414            end
415            uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
416            if isequal(length(uid_CoordUnit),1)
417                tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
418            end
419            uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
420            focal=[];%default fro old convention (Reg Wilson)
421            if isequal(length(uid_fx_fy),1)
422                tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
423            else %old convention (Reg Wilson)
424                uid_focal=find(subt,'/GeometryCalib/focal');
425                uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
426                uid_sx=find(subt,'/GeometryCalib/sx');
427                if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
428                    dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
429                    sx=str2num(get(subt,children(subt,uid_sx),'value'));
430                    focal=str2num(get(subt,children(subt,uid_focal),'value'));
431                    tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
432                    tsai.fx_fy(2)=focal/dpx_dpy(2);
433                end
434            end
435            uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
436            if ~isempty(uid_Cx_Cy)
437                tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
438            end
439            uid_kc=find(subt,'/GeometryCalib/kc');
440            if ~isempty(uid_kc)
441                tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
442            else %old convention (Reg Wilson)
443                uid_kappa1=find(subt,'/GeometryCalib/kappa1');
444                if ~isempty(uid_kappa1)&& ~isempty(focal)
445                    kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
446                    tsai.kc=-kappa1*focal*focal;
447                end
448            end
449            uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
450            if ~isempty(uid_Tx_Ty_Tz)
451                tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
452            end
453            uid_R=find(subt,'/GeometryCalib/R');
454            if ~isempty(uid_R)
455                RR=get(subt,children(subt,uid_R),'value');
456                if length(RR)==3
457                    tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
458                end
459            end
460           
461            %look for laser plane definitions
462            uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
463            uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
464            if isempty(uid_Pos)
465                uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
466            end
467            if ~isempty(uid_Angle)
468                tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
469            end
470            if ~isempty(uid_Pos)
471                for j=1:length(uid_Pos)
472                    tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
473                end
474                uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
475                uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
476                if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
477                    DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
478                    NbSlice=get(subt,children(subt,uid_NbSlice),'value');
479                    if isequal(NbSlice,'volume')
480                        tsai.NbSlice='volume';
481                        NbSlice=NbDtj+1;
482                    else
483                        tsai.NbSlice=str2double(NbSlice);
484                    end
485                    tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
486                end
487            end   
488            tsai.SliceAngle=get_value(subt,'/GeometryCalib/SliceAngle',[0 0 0]);
489            tsai.VolumeScan=get_value(subt,'/GeometryCalib/VolumeScan','n');
490            tsai.InterfaceCoord=get_value(subt,'/GeometryCalib/InterfaceCoord',[0 0 0]);
491            tsai.RefractionIndex=get_value(subt,'/GeometryCalib/RefractionIndex',1);
492           
493            if strcmp(option,'GeometryCalib')
494                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
495            end
496            s.GeometryCalib=tsai;
497        end
498    end
499end
500
501%--------------------------------------------------
502%  read a subtree
503% INPUT:
504% t: xltree
505% head_element: head elelemnt of the subtree
506% Data, structure containing
507%    .Key: element name
508%    .Type: type of element ('charg', 'float'....)
509%    .NbOccur: nbre of occurrence, NaN for un specified number
510function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
511%--------------------------------------------------
512s=[];%default
513errormsg='';
514head_element=get(subt,1,'name');
515    cont=get(subt,1,'contents');
516    if ~isempty(cont)
517        for ilist=1:length(Data)
518            uid_key=find(subt,[head_element '/' Data{ilist}]);
519            if ~isequal(length(uid_key),NbOccur(ilist))
520                errormsg=['wrong number of occurence for ' Data{ilist}];
521                return
522            end
523            for ival=1:length(uid_key)
524                val=get(subt,children(subt,uid_key(ival)),'value');
525                if ~NumTest(ilist)
526                    eval(['s.' Data{ilist} '=val;']);
527                else
528                    eval(['s.' Data{ilist} '=str2double(val);'])
529                end
530            end
531        end
532    end
533
534
535%--------------------------------------------------
536%  read an xml element
537function val=get_value(t,label,default)
538%--------------------------------------------------
539val=default;
540uid=find(t,label);%find the element iud(s)
541if ~isempty(uid) %if the element named label exists
542   uid_child=children(t,uid);%find the children
543   if ~isempty(uid_child)
544       data=get(t,uid_child,'type');%get the type of child
545       if iscell(data)% case of multiple element
546           for icell=1:numel(data)
547               val_read=str2num(get(t,uid_child(icell),'value'));
548               if ~isempty(val_read)
549                   val(icell,:)=val_read;
550               end
551           end
552%           val=val';
553       else % case of unique element value
554           val_read=str2num(get(t,uid_child,'value'));
555           if ~isempty(val_read)
556               val=val_read;
557           else
558              val=get(t,uid_child,'value');%char string data
559           end
560       end
561   end
562end
Note: See TracBrowser for help on using the repository browser.