source: trunk/src/xml2struct.m @ 457

Last change on this file since 457 was 453, checked in by sommeria, 12 years ago

series adapted to BATCH mode. Tests still needed.

File size: 1.5 KB
RevLine 
[356]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
[320]11function s=xml2struct(filename)
12t=xmltree(filename);
[324]13ss=convert(t);
[320]14s=convert_string(ss);
15
16
[450]17function out=convert_string(ss)
18info=whos('ss');
[320]19switch info.class
20    case 'struct'
[450]21        names = fieldnames(ss);
[320]22        for k=1:length(names)
[450]23            out.(names{k})=convert_string(ss.(names{k}));
[320]24        end
[379]25    case 'char'   
[450]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
[320]36        else
[450]37            out=str2num(ss);
[320]38        end
[379]39    case 'cell'
[450]40        for ilist=1:numel(ss)
[453]41            if ~isempty(str2num(ss{ilist}))
[450]42            out(ilist,:)=str2num(ss{ilist});
[453]43            end
[379]44        end
[320]45    otherwise
[450]46        out=ss;
[320]47end
48
49   
Note: See TracBrowser for help on using the repository browser.