-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadxml.m
140 lines (128 loc) · 3.8 KB
/
loadxml.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function varargout = loadxml(filename,varargin)
%LOADXML Load workspace variables from disk (XML file).
% LOADXML FILENAME retrieves all variables from a file given a full
% pathname or a MATLABPATH relative partial pathname (see PARTIALPATH).
% If FILENAME has no extension LOAD looks for FILENAME and FILENAME.xml
% and treats it as an XML file.
%
% LOAD, by itself, uses the XML file named 'matlab.xml'. It is an error
% if 'matlab.xml' is not found.
%
% LOAD FILENAME X loads only X.
% LOAD FILENAME X Y Z ... loads just the specified variables. The
% wildcard '*' loads variables that match a pattern.
% Requested variables from FILENAME are created in the workspace.
%
% S = LOAD(...) returns the contents of FILENAME in variable S. S is
% a struct containing fields matching the variables retrieved.
%
% Use the functional form of LOAD, such as LOAD('filename'), when the
% file name is stored in a string, when an output argument is requested,
% or if FILENAME contains spaces.
%
% See also LOAD, XML2MAT, XMLTREE.
% Copyright 2003 Guillaume Flandin.
% $Revision: 112 $ $Date: 2003/07/10 13:50 $
% $Id: loadxml.m 112 2005-05-04 18:20:52Z john $
if nargin == 0
filename = 'matlab.xml';
fprintf('\nLoading from: %s\n\n',filename);
end
if ~ischar(filename)
error('[LOADXML] Argument must contain a string.');
end
if ~exist(filename,'file')
filename = [filename '.xml'];
if ~exist(filename,'file')
error(sprintf(...
'[LOADXML] Unable to read file %s: file does not exist',filename));
end
end
if nargout > 1,
error('[LOADXML] Too many output arguments.');
end
t = xmltree(filename);
uid = children(t,root(t));
if nargout == 1
% varargout{1} = struct([]); % Matlab 6.0 and above
end
flagfirstvar = 1;
for i=1:length(uid)
if strcmp(get(t,uid(i),'type'),'element')
vname = get(t,uid(i),'name');
% TODO % No need to parse the whole tree
if isempty(varargin) | ismember(varargin,vname)
v = xml_create_var(t,uid(i));
if nargout == 1
if flagfirstvar
varargout{1} = struct(vname,v);
flagfirstvar = 0;
else
varargout{1} = setfield(varargout{1},vname,v);
end
else
assignin('caller',vname,v);
end
end
end
end
%=======================================================================
function v = xml_create_var(t,uid)
type = attributes(t,'get',uid,'type');
sz = str2num(attributes(t,'get',uid,'size'));
switch type
case 'double'
v = str2num(get(t,children(t,uid),'value'));
if ~isempty(sz)
v = reshape(v,sz);
end
case 'sparse'
u = children(t,uid);
for k=1:length(u)
if strcmp(get(t,u(k),'name'),'row')
i = str2num(get(t,children(t,u(k)),'value'));
elseif strcmp(get(t,u(k),'name'),'col')
j = str2num(get(t,children(t,u(k)),'value'));
elseif strcmp(get(t,u(k),'name'),'val')
s = str2num(get(t,children(t,u(k)),'value'));
end
end
v = sparse(i,j,s,sz(1),sz(2));
case 'struct'
u = children(t,uid);
v = []; % works with Matlab < 6.0
for i=1:length(u)
s(1).type = '()';
s(1).subs = {str2num(attributes(t,'get',u(i),'index'))};
s(2).type = '.';
s(2).subs = get(t,u(i),'name');
v = subsasgn(v,s,xml_create_var(t,u(i)));
end
if isempty(u),
v = struct([]); % Need Matlab 6.0 and above
end
case 'cell'
v = cell(sz);
u = children(t,uid);
for i=1:length(u)
v{str2num(attributes(t,'get',u(i),'index'))} = ...
xml_create_var(t,u(i));
end
case 'char'
if isempty(children(t,uid))
v = '';
else
v = get(t,children(t,uid),'value');
end
case {'int8','uint8','int16','uint16','int32','uint32'}
% TODO % Handle integer formats
warning(sprintf('%s matrices not handled.',type));
v = 0;
otherwise
try,
v = feval(class(v),get(t,uid,'value'));
catch,
warning(sprintf(...
'[LOADXML] Cannot convert from XML to %s.',type));
end
end