Skip to content

Commit 1597106

Browse files
committed
add patch provided by pjkoprowski to support MATLAB table, add RowNames support, fix #29
1 parent f16cc57 commit 1597106

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

savejson.m

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,12 @@
173173
txt=str2json(name,item,level,varargin{:});
174174
elseif(isa(item,'string'))
175175
txt=str2json(name,item{:},level,varargin{:});
176-
elseif(isobject(item))
177-
txt=matlabobject2json(name,item,level,varargin{:});
176+
elseif(isobject(item))
177+
if(~exist('OCTAVE_VERSION','builtin') && istable(item))
178+
txt=matlabtable2json(name,item,level,varargin{:});
179+
else
180+
txt=matlabobject2json(name,item,level,varargin{:});
181+
end
178182
else
179183
txt=mat2json(name,item,level,varargin{:});
180184
end
@@ -434,8 +438,13 @@
434438

435439
%%-------------------------------------------------------------------------
436440
function txt=matlabobject2json(name,item,level,varargin)
437-
st = struct();
438-
if numel(item) > 0 %non-empty object
441+
if numel(item) == 0 %empty object
442+
st = struct();
443+
elseif numel(item) == 1 %
444+
st = struct();
445+
txt = str2json(name, char(item), level, varargin(:));
446+
return
447+
else
439448
% "st = struct(item);" would produce an inmutable warning, because it
440449
% make the protected and private properties visible. Instead we get the
441450
% visible properties
@@ -448,6 +457,33 @@
448457
end
449458
txt=struct2json(name,st,level,varargin{:});
450459

460+
%%-------------------------------------------------------------------------
461+
function txt=matlabtable2json(name,item,level,varargin)
462+
if numel(item) == 0 %empty object
463+
st = struct();
464+
else
465+
% "st = struct(item);" would produce an inmutable warning, because it
466+
% make the protected and private properties visible. Instead we get the
467+
% visible properties
468+
st = struct();
469+
propertynames = properties(item);
470+
if(isfield(item.Properties,'RowNames') && ~isempty(item.Properties.RowNames))
471+
rownames=item.Properties.RowNames;
472+
for p = 1:(numel(propertynames)-1)
473+
for j = 1:size(item(:,p),1)
474+
st.(rownames{j}).(propertynames{p}) = item{j,p};
475+
end
476+
end
477+
else
478+
for p = 1:(numel(propertynames)-1)
479+
for j = 1:size(item(:,p),1)
480+
st(j).(propertynames{p}) = item{j,p};
481+
end
482+
end
483+
end
484+
end
485+
txt=struct2json(name,st,level,varargin{:});
486+
451487
%%-------------------------------------------------------------------------
452488
function txt=matdata2json(mat,level,varargin)
453489

0 commit comments

Comments
 (0)