Skip to content

Commit b968992

Browse files
committed
complted handle non scalar structs
1 parent e6a598b commit b968992

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

inst/struct2dbtable.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@
3939

4040
names = fieldnames(astruct);
4141

42-
if isscalar (s)
42+
if isscalar (astruct)
4343
values = struct2cell(astruct);
4444
t = dbtable (values{:}, 'VariableNames', names');
4545
else
4646
values = astruct(:);
4747
values = struct2cell(values);
48-
t = dbtable (values{:}, 'VariableNames', names');
48+
values = values';
49+
cols = {};
50+
for idx=1:size(values, 2)
51+
v = values(:,idx);
52+
cols{end+1} = v;
53+
endfor
54+
t = dbtable (cols{:}, 'VariableNames', names');
4955
endif
5056
endfunction
5157

@@ -57,6 +63,14 @@
5763
%! assert(t.Properties.VariableNames, {'a', 'b'});
5864
%! assert(t.a, [1;2;3]);
5965

66+
%!test
67+
%! s = struct('a', {[1;2;3]}, 'b', {[2;4;6]});
68+
%! t = struct2dbtable(s);
69+
%! assert(istable(t));
70+
%! assert(size(t), [3 2]);
71+
%! assert(t.Properties.VariableNames, {'a', 'b'});
72+
%! assert(t.a, [1;2;3]);
73+
6074
%!error <Not a struct> struct2dbtable(1);
6175

6276
%!error <Not a struct> struct2dbtable('hello');

0 commit comments

Comments
 (0)