[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); |
---|
[565] | 14 | iline=0; |
---|
| 15 | Heading=''; |
---|
| 16 | while isempty(Heading) |
---|
| 17 | iline=iline+1; |
---|
| 18 | if strcmp(get(t,iline,'type'),'element') |
---|
| 19 | Heading=get(t,iline,'name'); |
---|
| 20 | end |
---|
| 21 | end |
---|
[560] | 22 | if 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 |
---|
| 33 | else |
---|
| 34 | ss=convert(t); |
---|
| 35 | s=convert_string(ss); |
---|
| 36 | end |
---|
[320] | 37 | |
---|
| 38 | |
---|
[450] | 39 | function out=convert_string(ss) |
---|
| 40 | info=whos('ss'); |
---|
[320] | 41 | switch 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 |
---|
[663] | 48 | case 'char' |
---|
| 49 | out=ss; %reproduce the input string |
---|
[675] | 50 | % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval') |
---|
| 51 | if ~isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$')) || ~isempty(regexp(ss,'\d+e(-|+)\d+')) % if the string corresponds to a set of numbers (with possible sign and decimal, or scientific notation) separated by blanks |
---|
[663] | 52 | out=str2num(ss); |
---|
[675] | 53 | else |
---|
| 54 | sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables |
---|
| 55 | if ~isempty(sep_ind) |
---|
| 56 | sep_ind=[-2 sep_ind length(ss)+1]; |
---|
| 57 | for icolumn=1:length(sep_ind)-1 |
---|
| 58 | out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
---|
[450] | 59 | end |
---|
[675] | 60 | else |
---|
| 61 | out=ss; %reproduce the input string |
---|
[450] | 62 | end |
---|
[320] | 63 | end |
---|
[379] | 64 | case 'cell' |
---|
[471] | 65 | out=[];%default |
---|
[477] | 66 | check_numeric=zeros(size(ss)); |
---|
| 67 | for ilist=1:numel(ss) |
---|
[663] | 68 | if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist})) |
---|
[477] | 69 | out{ilist,1}=str2num(ss{ilist}); |
---|
| 70 | check_numeric(ilist)=1; |
---|
| 71 | else |
---|
[472] | 72 | sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables |
---|
| 73 | if ~isempty(sep_ind) |
---|
| 74 | sep_ind=[-2 sep_ind length(ss{ilist})+1]; |
---|
| 75 | for icolumn=1:length(sep_ind)-1 |
---|
| 76 | out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
---|
| 77 | end |
---|
| 78 | else |
---|
| 79 | out{ilist,1}=ss{ilist}; %reproduce the input string |
---|
| 80 | end |
---|
[453] | 81 | end |
---|
[379] | 82 | end |
---|
[477] | 83 | if isequal(check_numeric,ones(size(ss))) |
---|
| 84 | out=cell2mat(out); |
---|
| 85 | end |
---|
[320] | 86 | otherwise |
---|
[450] | 87 | out=ss; |
---|
[320] | 88 | end |
---|
| 89 | |
---|
| 90 | |
---|