source: trunk/src/xml2struct.m @ 697

Last change on this file since 697 was 697, checked in by sommeria, 10 years ago

bug corrected in geometry_calib: more precise determination ofcalib points
xml2struct corrected to work in background

File size: 3.5 KB
RevLine 
[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]14function [s,RootTag,errormsg]=xml2struct(filename,varargin)
[685]15s=[];
[686]16RootTag='';
[685]17errormsg='';
18try
[686]19    t=xmltree(filename);
[685]20catch 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
26end
[565]27iline=0;
[685]28
[686]29while 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
34end
[560]35if 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
46else
47    ss=convert(t);
48    s=convert_string(ss);
49end
[320]50
51
[450]52function out=convert_string(ss)
53info=whos('ss');
[320]54switch 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'
[675]62        % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval')
63        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]64            out=str2num(ss);
[675]65        else
66            sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
67            if ~isempty(sep_ind)
68                sep_ind=[-2 sep_ind length(ss)+1];
[697]69                out={};
[675]70                for icolumn=1:length(sep_ind)-1
[697]71                    out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);% get info between separators as a cell array
[450]72                end
[675]73            else
74                out=ss; %reproduce the input string
[450]75            end
[320]76        end
[379]77    case 'cell'
[697]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]101end
102
103   
Note: See TracBrowser for help on using the repository browser.