source: trunk/src/xml2struct.m @ 466

Last change on this file since 466 was 453, checked in by sommeria, 13 years ago

series adapted to BATCH mode. Tests still needed.

File size: 1.5 KB
Line 
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
10
11function s=xml2struct(filename)
12t=xmltree(filename);
13ss=convert(t);
14s=convert_string(ss);
15
16
17function out=convert_string(ss)
18info=whos('ss');
19switch info.class
20    case 'struct'
21        names = fieldnames(ss);
22        for k=1:length(names)
23            out.(names{k})=convert_string(ss.(names{k}));
24        end
25    case 'char'   
26        if isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$'))% if the string does not contains a set of numbers (with possible sign and decimal) separated by blanks
27            sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
28            if ~isempty(sep_ind)
29                sep_ind=[-2 sep_ind length(ss)+1];
30                for icolumn=1:length(sep_ind)-1
31                    out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
32                end
33            else
34                out=ss; %reproduce the input string
35            end
36        else
37            out=str2num(ss);
38        end
39    case 'cell'
40        for ilist=1:numel(ss)
41            if ~isempty(str2num(ss{ilist}))
42            out(ilist,:)=str2num(ss{ilist});
43            end
44        end
45    otherwise
46        out=ss;
47end
48
49   
Note: See TracBrowser for help on using the repository browser.