Skip to content

Commit e2ee6b0

Browse files
Sertan SenturkSertan Senturk
authored andcommitted
Added matlab object saving
1 parent 930d94b commit e2ee6b0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

savejson.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
txt=struct2json(name,item,level,varargin{:});
160160
elseif(ischar(item))
161161
txt=str2json(name,item,level,varargin{:});
162+
elseif(isobject(item))
163+
txt=matlabobject2json(name,item,level,varargin{:});
162164
else
163165
txt=mat2json(name,item,level,varargin{:});
164166
end
@@ -411,6 +413,23 @@
411413
end
412414
txt=sprintf('%s%s%s',txt,padding1,'}');
413415

416+
%%-------------------------------------------------------------------------
417+
function txt=matlabobject2json(name,item,level,varargin)
418+
if numel(item) == 0 %empty object
419+
st = struct();
420+
else
421+
% "st = struct(item);" would produce an inmutable warning, because it
422+
% make the protected and private properties visible. Instead we get the
423+
% visible properties
424+
propertynames = properties(item);
425+
for p = 1:numel(propertynames)
426+
for o = numel(item):-1:1 % aray of objects
427+
st(o).(propertynames{p}) = item(o).(propertynames{p});
428+
end
429+
end
430+
end
431+
txt=struct2json(name,st,level,varargin{:});
432+
414433
%%-------------------------------------------------------------------------
415434
function txt=matdata2json(mat,level,varargin)
416435

0 commit comments

Comments
 (0)