[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] | 12 | function [s,Heading]=xml2struct(filename,varargin) |
---|
[320] | 13 | t=xmltree(filename); |
---|
[472] | 14 | Heading=get(t,1,'name'); |
---|
[560] | 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 |
---|
[320] | 26 | |
---|
| 27 | |
---|
[450] | 28 | function out=convert_string(ss) |
---|
| 29 | info=whos('ss'); |
---|
[320] | 30 | switch info.class |
---|
| 31 | case 'struct' |
---|
[471] | 32 | out=[];%default |
---|
[450] | 33 | names = fieldnames(ss); |
---|
[320] | 34 | for k=1:length(names) |
---|
[450] | 35 | out.(names{k})=convert_string(ss.(names{k})); |
---|
[320] | 36 | end |
---|
[379] | 37 | case 'char' |
---|
[450] | 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 |
---|
[320] | 48 | else |
---|
[450] | 49 | out=str2num(ss); |
---|
[320] | 50 | end |
---|
[379] | 51 | case 'cell' |
---|
[471] | 52 | out=[];%default |
---|
[477] | 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 |
---|
[472] | 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 |
---|
[453] | 68 | end |
---|
[379] | 69 | end |
---|
[477] | 70 | if isequal(check_numeric,ones(size(ss))) |
---|
| 71 | out=cell2mat(out); |
---|
| 72 | end |
---|
[320] | 73 | otherwise |
---|
[450] | 74 | out=ss; |
---|
[320] | 75 | end |
---|
| 76 | |
---|
| 77 | |
---|