Skip to content

Commit

Permalink
saving table objects with new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jun 24, 2019
1 parent 0a0182f commit eba4078
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
33 changes: 33 additions & 0 deletions jdatadecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
end
end

%% handle array data
if(~isempty(strmatch(N_('_ArrayType_'),fn)) && (~isempty(strmatch(N_('_ArrayData_'),fn)) || ~isempty(strmatch(N_('_ArrayZipData_'),fn))))
newdata=cell(len,1);
for j=1:len
Expand Down Expand Up @@ -174,6 +175,38 @@
newdata=newdata{1};
end
end

%% handle table data
if(~isempty(strmatch(N_('_TableRecords_'),fn)))
newdata=cell(len,1);
for j=1:len
ndata=data(j).(N_('_TableRecords_'));
if(iscell(ndata))
rownum=length(ndata);
colnum=length(ndata{1});
nd=cell(rownum, colnum);
for i1=1:rownum;
for i2=1:colnum
nd{i1,i2}=ndata{i1}{i2};
end
end
newdata{j}=cell2table(nd);
else
newdata{j}=array2table(ndata');
end
if(isfield(data(j),N_('_TableRows_'))&& ~isempty(data(j).(N_('_TableRows_'))))
newdata{j}.Properties.RowNames=data(j).(N_('_TableRows_'))(:);
end
if(isfield(data(j),N_('_TableCols_')) && ~isempty(data(j).(N_('_TableCols_'))))
newdata{j}.Properties.VariableNames=data(j).(N_('_TableCols_'));
end
end
if(len==1)
newdata=newdata{1};
end
end

%% handle map data
if(~isempty(strmatch(N_('_MapData_'),fn)))
newdata=cell(len,1);
for j=1:len
Expand Down
1 change: 1 addition & 0 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@
astr=arraystr;
astr(astr=='[')='';
astr(astr==']')='';
astr=regexprep(deblank(astr),'\s+,',',');
[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);
if(nextidx>=length(astr)-1)
obj=reshape(obj,nextdim,numel(obj)/nextdim)';
Expand Down
28 changes: 9 additions & 19 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -621,27 +621,17 @@

%%-------------------------------------------------------------------------
function txt=matlabtable2json(name,item,level,varargin)
if numel(item) == 0 %empty object
st = struct();
st=containers.Map();
st('_TableRecords_')=table2cell(item);
st('_TableRows_')=item.Properties.RowNames';
st('_TableCols_')=item.Properties.VariableNames;
if(isempty(name))
txt=map2json(name,st,level,varargin{:});
else
st = struct();
propertynames = item.Properties.VariableNames;
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)
for j = 1:size(item(:,p),1)
st(j).(propertynames{p}) = item{j,p};
end
end
end
temp=struct(name,struct());
temp.(name)=st;
txt=map2json(name,temp.(name),level,varargin{:});
end
txt=struct2json(name,st,level,varargin{:});

%%-------------------------------------------------------------------------
function txt=matdata2json(mat,level,varargin)
Expand Down
28 changes: 9 additions & 19 deletions saveubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -578,27 +578,17 @@

%%-------------------------------------------------------------------------
function txt=matlabtable2ubjson(name,item,level,varargin)
if numel(item) == 0 %empty object
st = struct();
st=containers.Map();
st('_TableRecords_')=table2cell(item);
st('_TableRows_')=item.Properties.RowNames';
st('_TableCols_')=item.Properties.VariableNames;
if(isempty(name))
txt=map2ubjson(name,st,level,varargin{:});
else
st = struct();
propertynames = item.Properties.VariableNames;
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)
for j = 1:size(item(:,p),1)
st(j).(propertynames{p}) = item{j,p};
end
end
end
temp=struct(name,struct());
temp.(name)=st;
txt=map2ubjson(name,temp.(name),level,varargin{:});
end
txt=struct2ubjson(name,st,level,varargin{:});

%%-------------------------------------------------------------------------
function txt=matlabobject2ubjson(name,item,level,varargin)
Expand Down

0 comments on commit eba4078

Please sign in to comment.