| [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 | |
|---|
| [809] | 14 | %======================================================================= |
|---|
| 15 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
|---|
| 16 | % http://www.legi.grenoble-inp.fr |
|---|
| 17 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
|---|
| 18 | % |
|---|
| 19 | % This file is part of the toolbox UVMAT. |
|---|
| 20 | % |
|---|
| 21 | % UVMAT is free software; you can redistribute it and/or modify |
|---|
| 22 | % it under the terms of the GNU General Public License as published |
|---|
| 23 | % by the Free Software Foundation; either version 2 of the license, |
|---|
| 24 | % or (at your option) any later version. |
|---|
| 25 | % |
|---|
| 26 | % UVMAT is distributed in the hope that it will be useful, |
|---|
| 27 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 28 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 29 | % GNU General Public License (see LICENSE.txt) for more details. |
|---|
| 30 | %======================================================================= |
|---|
| 31 | |
|---|
| [686] | 32 | function [s,RootTag,errormsg]=xml2struct(filename,varargin) |
|---|
| [685] | 33 | s=[]; |
|---|
| [686] | 34 | RootTag=''; |
|---|
| [685] | 35 | errormsg=''; |
|---|
| 36 | try |
|---|
| [772] | 37 | t=xmltree(filename);% read the file as an xmltree object t |
|---|
| [685] | 38 | catch ME |
|---|
| 39 | errormsg=ME.message; |
|---|
| [687] | 40 | if ~isempty(regexp(ME.message,'Undefined function'))||~isempty(regexp(ME.message,'Missing')) |
|---|
| [686] | 41 | errormsg=[errormsg ': package xmltree not correctly installed, reload it from www.artefact.tk/software/matlab/xml']; |
|---|
| [685] | 42 | end |
|---|
| 43 | return |
|---|
| 44 | end |
|---|
| [565] | 45 | iline=0; |
|---|
| [685] | 46 | |
|---|
| [686] | 47 | while isempty(RootTag) |
|---|
| [565] | 48 | iline=iline+1; |
|---|
| 49 | if strcmp(get(t,iline,'type'),'element') |
|---|
| [686] | 50 | RootTag=get(t,iline,'name'); |
|---|
| [565] | 51 | end |
|---|
| 52 | end |
|---|
| [560] | 53 | if nargin>1 |
|---|
| 54 | for isub=1:nargin-1 |
|---|
| [686] | 55 | uid_sub=find(t,['/' RootTag '/' varargin{isub}]); |
|---|
| [565] | 56 | if isempty(uid_sub) |
|---|
| 57 | s.(varargin{isub})=[]; |
|---|
| 58 | else |
|---|
| [560] | 59 | tsub=branch(t,uid_sub); |
|---|
| 60 | ss=convert(tsub); |
|---|
| 61 | s.(varargin{isub})=convert_string(ss); |
|---|
| [565] | 62 | end |
|---|
| [560] | 63 | end |
|---|
| 64 | else |
|---|
| [772] | 65 | ss=convert(t);%transform the xmltree object into a Matlab structure. |
|---|
| [560] | 66 | s=convert_string(ss); |
|---|
| 67 | end |
|---|
| [320] | 68 | |
|---|
| 69 | |
|---|
| [450] | 70 | function out=convert_string(ss) |
|---|
| 71 | info=whos('ss'); |
|---|
| [320] | 72 | switch info.class |
|---|
| 73 | case 'struct' |
|---|
| [471] | 74 | out=[];%default |
|---|
| [450] | 75 | names = fieldnames(ss); |
|---|
| [320] | 76 | for k=1:length(names) |
|---|
| [450] | 77 | out.(names{k})=convert_string(ss.(names{k})); |
|---|
| [320] | 78 | end |
|---|
| [663] | 79 | case 'char' |
|---|
| [675] | 80 | % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval') |
|---|
| [772] | 81 | if exist(ss,'builtin')||exist(ss,'file')% ss corresponds to the name of a builtin Matlab function or a file |
|---|
| 82 | out=ss; %reproduce the input string |
|---|
| [675] | 83 | else |
|---|
| [772] | 84 | out=str2num(ss);% convert to number or vector (str2num applied to a fct name executes this fct by 'eval', thus this possibility had to be ruled out above |
|---|
| 85 | if isempty(out) |
|---|
| 86 | sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables |
|---|
| 87 | if ~isempty(sep_ind) |
|---|
| 88 | sep_ind=[-2 sep_ind length(ss)+1]; |
|---|
| 89 | out={}; |
|---|
| 90 | for icolumn=1:length(sep_ind)-1 |
|---|
| 91 | out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);% get info between separators as a cell array |
|---|
| 92 | end |
|---|
| 93 | else |
|---|
| 94 | out=ss; %reproduce the input string |
|---|
| [450] | 95 | end |
|---|
| 96 | end |
|---|
| [320] | 97 | end |
|---|
| [379] | 98 | case 'cell' |
|---|
| [697] | 99 | out={};%default |
|---|
| [477] | 100 | check_numeric=zeros(size(ss)); |
|---|
| 101 | for ilist=1:numel(ss) |
|---|
| [663] | 102 | if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist})) |
|---|
| [477] | 103 | out{ilist,1}=str2num(ss{ilist}); |
|---|
| 104 | check_numeric(ilist)=1; |
|---|
| 105 | else |
|---|
| [472] | 106 | sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables |
|---|
| 107 | if ~isempty(sep_ind) |
|---|
| 108 | sep_ind=[-2 sep_ind length(ss{ilist})+1]; |
|---|
| 109 | for icolumn=1:length(sep_ind)-1 |
|---|
| 110 | out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1); |
|---|
| 111 | end |
|---|
| 112 | else |
|---|
| 113 | out{ilist,1}=ss{ilist}; %reproduce the input string |
|---|
| 114 | end |
|---|
| [453] | 115 | end |
|---|
| [379] | 116 | end |
|---|
| [477] | 117 | if isequal(check_numeric,ones(size(ss))) |
|---|
| 118 | out=cell2mat(out); |
|---|
| 119 | end |
|---|
| [320] | 120 | otherwise |
|---|
| [450] | 121 | out=ss; |
|---|
| [320] | 122 | end |
|---|
| 123 | |
|---|
| [809] | 124 | |
|---|