source: trunk/src/xml2struct.m @ 471

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

a few bugs repaired

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'
[471]21        out=[];%default
[450]22        names = fieldnames(ss);
[320]23        for k=1:length(names)
[450]24            out.(names{k})=convert_string(ss.(names{k}));
[320]25        end
[379]26    case 'char'   
[450]27        if isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$'))% if the string does not contains a set of numbers (with possible sign and decimal) separated by blanks
28            sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
29            if ~isempty(sep_ind)
30                sep_ind=[-2 sep_ind length(ss)+1];
31                for icolumn=1:length(sep_ind)-1
32                    out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
33                end
34            else
35                out=ss; %reproduce the input string
36            end
[320]37        else
[450]38            out=str2num(ss);
[320]39        end
[379]40    case 'cell'
[471]41        out=[];%default
[450]42        for ilist=1:numel(ss)
[453]43            if ~isempty(str2num(ss{ilist}))
[450]44            out(ilist,:)=str2num(ss{ilist});
[453]45            end
[379]46        end
[320]47    otherwise
[450]48        out=ss;
[320]49end
50
51   
Note: See TracBrowser for help on using the repository browser.