From e2ee6b0e4e020e34a1d8fc7a8c2706d6e1c7b477 Mon Sep 17 00:00:00 2001 From: Sertan Senturk Date: Fri, 25 Sep 2015 03:30:11 +0200 Subject: [PATCH] Added matlab object saving --- savejson.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/savejson.m b/savejson.m index 57125fe..f5c1d2f 100644 --- a/savejson.m +++ b/savejson.m @@ -159,6 +159,8 @@ txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); +elseif(isobject(item)) + txt=matlabobject2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end @@ -411,6 +413,23 @@ end txt=sprintf('%s%s%s',txt,padding1,'}'); +%%------------------------------------------------------------------------- +function txt=matlabobject2json(name,item,level,varargin) +if numel(item) == 0 %empty object + st = struct(); +else + % "st = struct(item);" would produce an inmutable warning, because it + % make the protected and private properties visible. Instead we get the + % visible properties + propertynames = properties(item); + for p = 1:numel(propertynames) + for o = numel(item):-1:1 % aray of objects + st(o).(propertynames{p}) = item(o).(propertynames{p}); + end + end +end +txt=struct2json(name,st,level,varargin{:}); + %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin)