Skip to content
Snippets Groups Projects
Commit 1597106f authored by Qianqian Fang's avatar Qianqian Fang
Browse files

add patch provided by pjkoprowski to support MATLAB table, add RowNames support, fix #29

parent f16cc57d
No related branches found
No related tags found
No related merge requests found
......@@ -174,7 +174,11 @@ elseif(ischar(item))
elseif(isa(item,'string'))
txt=str2json(name,item{:},level,varargin{:});
elseif(isobject(item))
if(~exist('OCTAVE_VERSION','builtin') && istable(item))
txt=matlabtable2json(name,item,level,varargin{:});
else
txt=matlabobject2json(name,item,level,varargin{:});
end
else
txt=mat2json(name,item,level,varargin{:});
end
......@@ -434,8 +438,13 @@ txt=sprintf('%s%s%s',txt,padding1,'}');
%%-------------------------------------------------------------------------
function txt=matlabobject2json(name,item,level,varargin)
if numel(item) == 0 %empty object
st = struct();
elseif numel(item) == 1 %
st = struct();
if numel(item) > 0 %non-empty object
txt = str2json(name, char(item), level, varargin(:));
return
else
% "st = struct(item);" would produce an inmutable warning, because it
% make the protected and private properties visible. Instead we get the
% visible properties
......@@ -448,6 +457,33 @@ if numel(item) > 0 %non-empty object
end
txt=struct2json(name,st,level,varargin{:});
%%-------------------------------------------------------------------------
function txt=matlabtable2json(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
st = struct();
propertynames = properties(item);
if(isfield(item.Properties,'RowNames') && ~isempty(item.Properties.RowNames))
rownames=item.Properties.RowNames;
for p = 1:(numel(propertynames)-1)
for j = 1:size(item(:,p),1)
st.(rownames{j}).(propertynames{p}) = item{j,p};
end
end
else
for p = 1:(numel(propertynames)-1)
for j = 1:size(item(:,p),1)
st(j).(propertynames{p}) = item{j,p};
end
end
end
end
txt=struct2json(name,st,level,varargin{:});
%%-------------------------------------------------------------------------
function txt=matdata2json(mat,level,varargin)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment