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 |
---|
10 | % varargin: optional list of strings to restrict the reading to a selection of subtrees, for instance 'GeometryCalib' (save time) |
---|
11 | |
---|
12 | function [s,Heading]=xml2struct(filename,varargin) |
---|
13 | t=xmltree(filename); |
---|
14 | Heading=get(t,1,'name'); |
---|
15 | if nargin>1 |
---|
16 | for isub=1:nargin-1 |
---|
17 | uid_sub=find(t,['/' Heading '/' varargin{isub}]); |
---|
18 | tsub=branch(t,uid_sub); |
---|
19 | ss=convert(tsub); |
---|
20 | s.(varargin{isub})=convert_string(ss); |
---|
21 | end |
---|
22 | else |
---|
23 | ss=convert(t); |
---|
24 | s=convert_string(ss); |
---|
25 | end |
---|
26 | |
---|
27 | |
---|
28 | function out=convert_string(ss) |
---|
29 | info=whos('ss'); |
---|
30 | switch info.class |
---|
31 | case 'struct' |
---|
32 | out=[];%default |
---|
33 | names = fieldnames(ss); |
---|
34 | for k=1:length(names) |
---|
35 | out.(names{k})=convert_string(ss.(names{k})); |
---|
36 | end |
---|
37 | case 'char' |
---|
38 | if isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$'))% if the string does not contains a set of numbers (with possible sign and decimal) separated by blanks |
---|
39 | sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables |
---|
40 | if ~isempty(sep_ind) |
---|
41 | sep_ind=[-2 sep_ind length(ss)+1]; |
---|
42 | for icolumn=1:length(sep_ind)-1 |
---|
43 | out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
---|
44 | end |
---|
45 | else |
---|
46 | out=ss; %reproduce the input string |
---|
47 | end |
---|
48 | else |
---|
49 | out=str2num(ss); |
---|
50 | end |
---|
51 | case 'cell' |
---|
52 | out=[];%default |
---|
53 | check_numeric=zeros(size(ss)); |
---|
54 | for ilist=1:numel(ss) |
---|
55 | if ~isempty(str2num(ss{ilist})) |
---|
56 | out{ilist,1}=str2num(ss{ilist}); |
---|
57 | check_numeric(ilist)=1; |
---|
58 | else |
---|
59 | sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables |
---|
60 | if ~isempty(sep_ind) |
---|
61 | sep_ind=[-2 sep_ind length(ss{ilist})+1]; |
---|
62 | for icolumn=1:length(sep_ind)-1 |
---|
63 | out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
---|
64 | end |
---|
65 | else |
---|
66 | out{ilist,1}=ss{ilist}; %reproduce the input string |
---|
67 | end |
---|
68 | end |
---|
69 | end |
---|
70 | if isequal(check_numeric,ones(size(ss))) |
---|
71 | out=cell2mat(out); |
---|
72 | end |
---|
73 | otherwise |
---|
74 | out=ss; |
---|
75 | end |
---|
76 | |
---|
77 | |
---|