Skip to content

Commit 98c9aad

Browse files
Sertan SenturkSertan Senturk
authored andcommitted
Added matlab object saving to saveubjson
1 parent e2ee6b0 commit 98c9aad

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

savejson.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
% rootname: the name of the root-object, when set to '', the root name
1818
% is ignored, however, when opt.ForceRootName is set to 1 (see below),
1919
% the MATLAB variable name will be used as the root name.
20-
% obj: a MATLAB object (array, cell, cell array, struct, struct array).
20+
% obj: a MATLAB object (array, cell, cell array, struct, struct array,
21+
% class instance).
2122
% filename: a string for the file name to save the output JSON data.
2223
% opt: a struct for additional options, ignore to use default values.
2324
% opt can have the following fields (first in [.|.] is the default)

saveubjson.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
% rootname: the name of the root-object, when set to '', the root name
1818
% is ignored, however, when opt.ForceRootName is set to 1 (see below),
1919
% the MATLAB variable name will be used as the root name.
20-
% obj: a MATLAB object (array, cell, cell array, struct, struct array)
20+
% obj: a MATLAB object (array, cell, cell array, struct, struct array,
21+
% class instance)
2122
% filename: a string for the file name to save the output UBJSON data
2223
% opt: a struct for additional options, ignore to use default values.
2324
% opt can have the following fields (first in [.|.] is the default)
@@ -129,6 +130,8 @@
129130
txt=struct2ubjson(name,item,level,varargin{:});
130131
elseif(ischar(item))
131132
txt=str2ubjson(name,item,level,varargin{:});
133+
elseif(isobject(item))
134+
txt=matlabobject2ubjson(name,item,level,varargin{:});
132135
else
133136
txt=mat2ubjson(name,item,level,varargin{:});
134137
end
@@ -328,6 +331,23 @@
328331
end
329332
txt=[txt,'}'];
330333

334+
%%-------------------------------------------------------------------------
335+
function txt=matlabobject2ubjson(name,item,level,varargin)
336+
if numel(item) == 0 %empty object
337+
st = struct();
338+
else
339+
% "st = struct(item);" would produce an inmutable warning, because it
340+
% make the protected and private properties visible. Instead we get the
341+
% visible properties
342+
propertynames = properties(item);
343+
for p = 1:numel(propertynames)
344+
for o = numel(item):-1:1 % aray of objects
345+
st(o).(propertynames{p}) = item(o).(propertynames{p});
346+
end
347+
end
348+
end
349+
txt=struct2ubjson(name,st,level,varargin{:});
350+
331351
%%-------------------------------------------------------------------------
332352
function txt=matdata2ubjson(mat,level,varargin)
333353
if(isempty(mat))

0 commit comments

Comments
 (0)