[356] | 1 | % 'xml2struct': read an xml file as a Matlab structure, converts numeric character strings into numbers |
---|
| 2 | %----------------------------------------------------------------------- |
---|
[686] | 3 | % function [s,RootTag,errormsg]=xml2struct(filename,varargin) |
---|
[356] | 4 | % |
---|
| 5 | % OUTPUT: |
---|
| 6 | % s= Matlab structure corresponding to the input xml file |
---|
[686] | 7 | % RootTag= name of the root tag in the xml file |
---|
| 8 | % errormsg: errormessage, ='' by default |
---|
[356] | 9 | % |
---|
| 10 | % INPUT: |
---|
| 11 | % filename: name of the xml file |
---|
[686] | 12 | % varargin: optional list of strings to restrict the reading to a selection of subtrees, for instance 'GeometryCalib' (save reading time) |
---|
[356] | 13 | |
---|
[686] | 14 | function [s,RootTag,errormsg]=xml2struct(filename,varargin) |
---|
[685] | 15 | s=[]; |
---|
[686] | 16 | RootTag=''; |
---|
[685] | 17 | errormsg=''; |
---|
| 18 | try |
---|
[686] | 19 | t=xmltree(filename); |
---|
[685] | 20 | catch ME |
---|
| 21 | errormsg=ME.message; |
---|
[687] | 22 | if ~isempty(regexp(ME.message,'Undefined function'))||~isempty(regexp(ME.message,'Missing')) |
---|
[686] | 23 | errormsg=[errormsg ': package xmltree not correctly installed, reload it from www.artefact.tk/software/matlab/xml']; |
---|
[685] | 24 | end |
---|
| 25 | return |
---|
| 26 | end |
---|
[565] | 27 | iline=0; |
---|
[685] | 28 | |
---|
[686] | 29 | while isempty(RootTag) |
---|
[565] | 30 | iline=iline+1; |
---|
| 31 | if strcmp(get(t,iline,'type'),'element') |
---|
[686] | 32 | RootTag=get(t,iline,'name'); |
---|
[565] | 33 | end |
---|
| 34 | end |
---|
[560] | 35 | if nargin>1 |
---|
| 36 | for isub=1:nargin-1 |
---|
[686] | 37 | uid_sub=find(t,['/' RootTag '/' varargin{isub}]); |
---|
[565] | 38 | if isempty(uid_sub) |
---|
| 39 | s.(varargin{isub})=[]; |
---|
| 40 | else |
---|
[560] | 41 | tsub=branch(t,uid_sub); |
---|
| 42 | ss=convert(tsub); |
---|
| 43 | s.(varargin{isub})=convert_string(ss); |
---|
[565] | 44 | end |
---|
[560] | 45 | end |
---|
| 46 | else |
---|
| 47 | ss=convert(t); |
---|
| 48 | s=convert_string(ss); |
---|
| 49 | end |
---|
[320] | 50 | |
---|
| 51 | |
---|
[450] | 52 | function out=convert_string(ss) |
---|
| 53 | info=whos('ss'); |
---|
[320] | 54 | switch info.class |
---|
| 55 | case 'struct' |
---|
[471] | 56 | out=[];%default |
---|
[450] | 57 | names = fieldnames(ss); |
---|
[320] | 58 | for k=1:length(names) |
---|
[450] | 59 | out.(names{k})=convert_string(ss.(names{k})); |
---|
[320] | 60 | end |
---|
[663] | 61 | case 'char' |
---|
| 62 | out=ss; %reproduce the input string |
---|
[675] | 63 | % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval') |
---|
| 64 | 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] | 65 | out=str2num(ss); |
---|
[675] | 66 | else |
---|
| 67 | sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables |
---|
| 68 | if ~isempty(sep_ind) |
---|
| 69 | sep_ind=[-2 sep_ind length(ss)+1]; |
---|
| 70 | for icolumn=1:length(sep_ind)-1 |
---|
| 71 | out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
---|
[450] | 72 | end |
---|
[675] | 73 | else |
---|
| 74 | out=ss; %reproduce the input string |
---|
[450] | 75 | end |
---|
[320] | 76 | end |
---|
[379] | 77 | case 'cell' |
---|
[471] | 78 | out=[];%default |
---|
[477] | 79 | check_numeric=zeros(size(ss)); |
---|
| 80 | for ilist=1:numel(ss) |
---|
[663] | 81 | if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist})) |
---|
[477] | 82 | out{ilist,1}=str2num(ss{ilist}); |
---|
| 83 | check_numeric(ilist)=1; |
---|
| 84 | else |
---|
[472] | 85 | sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables |
---|
| 86 | if ~isempty(sep_ind) |
---|
| 87 | sep_ind=[-2 sep_ind length(ss{ilist})+1]; |
---|
| 88 | for icolumn=1:length(sep_ind)-1 |
---|
| 89 | out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
---|
| 90 | end |
---|
| 91 | else |
---|
| 92 | out{ilist,1}=ss{ilist}; %reproduce the input string |
---|
| 93 | end |
---|
[453] | 94 | end |
---|
[379] | 95 | end |
---|
[477] | 96 | if isequal(check_numeric,ones(size(ss))) |
---|
| 97 | out=cell2mat(out); |
---|
| 98 | end |
---|
[320] | 99 | otherwise |
---|
[450] | 100 | out=ss; |
---|
[320] | 101 | end |
---|
| 102 | |
---|
| 103 | |
---|