source: trunk/src/xml2struct.m @ 611

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

small bugs corrected

File size: 2.8 KB
RevLine 
[356]1% 'xml2struct': read an xml file as a Matlab structure, converts numeric character strings into numbers
2%-----------------------------------------------------------------------
3% function s=xml2struct(filename)
4%
5% OUTPUT:
6% s= Matlab structure corresponding to the input xml file
7%
8% INPUT:
9% filename: name of the xml file
[560]10% varargin: optional list of strings to restrict the reading to a selection of subtrees, for instance 'GeometryCalib' (save time)
[356]11
[560]12function [s,Heading]=xml2struct(filename,varargin)
[320]13t=xmltree(filename);
[565]14iline=0;
15Heading='';
16while isempty(Heading)
17    iline=iline+1;
18    if strcmp(get(t,iline,'type'),'element')
19        Heading=get(t,iline,'name');
20    end
21end
[560]22if nargin>1
23    for isub=1:nargin-1
24        uid_sub=find(t,['/' Heading '/' varargin{isub}]);
[565]25        if isempty(uid_sub)
26            s.(varargin{isub})=[];
27        else
[560]28        tsub=branch(t,uid_sub);
29        ss=convert(tsub);
30        s.(varargin{isub})=convert_string(ss);
[565]31        end
[560]32    end
33else
34    ss=convert(t);
35    s=convert_string(ss);
36end
[320]37
38
[450]39function out=convert_string(ss)
40info=whos('ss');
[320]41switch info.class
42    case 'struct'
[471]43        out=[];%default
[450]44        names = fieldnames(ss);
[320]45        for k=1:length(names)
[450]46            out.(names{k})=convert_string(ss.(names{k}));
[320]47        end
[379]48    case 'char'   
[450]49        if isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$'))% if the string does not contains a set of numbers (with possible sign and decimal) separated by blanks
50            sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
51            if ~isempty(sep_ind)
52                sep_ind=[-2 sep_ind length(ss)+1];
53                for icolumn=1:length(sep_ind)-1
54                    out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
55                end
56            else
57                out=ss; %reproduce the input string
58            end
[320]59        else
[450]60            out=str2num(ss);
[320]61        end
[379]62    case 'cell'
[471]63        out=[];%default
[477]64        check_numeric=zeros(size(ss));
65        for ilist=1:numel(ss)
66            if ~isempty(str2num(ss{ilist}))
67                out{ilist,1}=str2num(ss{ilist});
68                check_numeric(ilist)=1;
69            else
[472]70                sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables
71                if ~isempty(sep_ind)
72                    sep_ind=[-2 sep_ind length(ss{ilist})+1];
73                    for icolumn=1:length(sep_ind)-1
74                        out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
75                    end
76                else
77                    out{ilist,1}=ss{ilist}; %reproduce the input string
78                end
[453]79            end
[379]80        end
[477]81        if isequal(check_numeric,ones(size(ss)))
82            out=cell2mat(out);
83        end
[320]84    otherwise
[450]85        out=ss;
[320]86end
87
88   
Note: See TracBrowser for help on using the repository browser.