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