Is there a way to export table with sub structures to excel w/o looing the structure?

1 view (last 30 days)
I have a table that looks like this:
patient =
2×3 table
name billing test
__________ _______ ____________
'John Doe' 127 [3×3 double]
'Ann Lane' 28.5 [3×3 double]
I would like for it to be exportet to excel like this:
Capture.PNG
but using writetable(patient) it comes out like this:
Capture2.PNG
is there any solution for this?
(this is examlpe data, my real dataset is much larger, so restructuring manually would be a pain in the a**...)

Answers (1)

Guillaume
Guillaume on 13 Aug 2019
Edited: Guillaume on 13 Aug 2019
Matlab will certainly never write your original table the way you want. You will have to convert it into a new table.
The following assumes that the number of rows in each test matrix can vary from table row to table row but that the number of columns is always 3.
%demo table:
t = table({'John Doe'; 'Ann Lane'}, [127;28.5], {[79, 180, 220; 75, 178, 210; 73, 177.5, 205]; [68, 118, 172; 70, 118, 170;68, 119, 168]}, 'VariableNames', {'name', 'billing', 'test'})
%get the height of each matrix in test
matheight = cellfun('size', t.test, 1);
%replicate the content of each variable but test according to the height the matrices
twrite = varfun(@(var) repelem(var, matheight), t, 'InputVariables', ~strcmp(t.Properties.VariableNames, 'test'));
%split the rows of the test matrices across the dupliced rows
twrite.test = vertcat(t.test{:});
%split the columns of test into 3 new variables
twrite = splitvars(twrite, 'test', 'NewVariablenames', {'RepA', 'RepB', 'RepC'});
%write table
writetable(twrite, 'somename.xlsx');
  3 Comments
Guillaume
Guillaume on 13 Aug 2019
In that case:
%get the height of each matrix in test
matheight = cellfun('size', t.test, 1);
%create filler as a row cell array
filler = arrayfun(@(height) repelem({[]}, height, 1), matheight-1, 'UniformOutput', false)';
%create new table
name = [t.name'; filler];
billing = [num2cell(t.billing.'); filler];
twrite = table(vertcat(name{:}), vertcat(billing{:}), vertcat(t.test{:}), 'VariableNames', {'name', 'billing', 'test'});
twrite = splitvars(twrite, 'test', 'NewVariablenames', {'RepA', 'RepB', 'RepC'})

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!