Skip to content

Commit 02967fe

Browse files
committed
add table2struct
1 parent fb87a6f commit 02967fe

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

inst/dbtable.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ function summary (this)
435435
y = true;
436436
endfunction
437437

438+
function st = table2struct(this)
439+
names = this.Properties.VariableNames;
440+
st = {};
441+
for idx=1:numel(names)
442+
st.(names{idx}) = this._data{idx};
443+
endfor
444+
endfunction
445+
438446
# head tail - create sub tables
439447
function tdata = head(this, rows)
440448
nrows = size(this, 1);
@@ -541,3 +549,10 @@ function summary (this)
541549
%! assert(t.V1, V1);
542550
%! assert(t.V2, V2);
543551

552+
%!test
553+
%! V1 = [0;1;3];
554+
%! V2 = [2;4;6];
555+
%! t = dbtable(V1, V2);
556+
%! s = table2struct(t);
557+
%! assert(fieldnames(s), {'V1'; 'V2'});
558+
%! assert(s.V1, [0;1;3]);

0 commit comments

Comments
 (0)