How do I selectively write from struct to excel

2 views (last 30 days)
I have a 35x1 struct with character arrays, double arrays (just a number), datetime array, and an Nx2 double array (N being variable). I uploaded a table of that data sans the Nx2 double array.
I want to send that to excel transposed and with empty columns between data columns, for easier viewing and to accomodate the Nx2 double array that should not be transposed. I can move the Nx2 to its own container instead of putting it in the struct.
Any strategies for doing this? I think converting to a table gets me part of the way there. Transposing and adding columns and row skips is the challenge (for me).

Accepted Answer

dpb
dpb on 21 Jul 2023
whos -file mystruct
Name Size Bytes Class Attributes mystruct - 11255 table
load mystruct
whos mystruct
Name Size Bytes Class Attributes mystruct 35x13 11255 table
head(mystruct)
name meas_type V1 T1 V2 T2 T_lps Calc1 Caltot Calc2 Calc3 t0 fs _______________ ______________ ______ ______ ______ ______ ______ ______ __________ __________ __________ ____________________ ______ "cba_1457.wdf" "Type1" 930.67 18.098 50.667 28.84 10.742 11.356 1.3333e+06 8.5169e-06 2.839e-06 27-Jun-2023 14:56:44 0.0005 "cba_1459.wdf" "Type1" 932 20.345 50.667 29.283 8.938 10.903 1.3333e+06 8.1773e-06 2.7258e-06 27-Jun-2023 14:59:17 0.0005 "cba_1501.wdf" "Type1" 930.67 23.608 52 32.857 9.2495 11.277 1.3333e+06 8.4574e-06 2.8191e-06 27-Jun-2023 15:00:37 0.0005 "cba_1502.wdf" "Type1" 930.67 28.669 50.667 40.036 11.367 11.502 1.3333e+06 8.6262e-06 2.8754e-06 27-Jun-2023 15:01:57 0.0005 "cba_1503.wdf" "Type1" 930.67 19.607 50.667 28.415 8.8075 10.872 1.3333e+06 8.154e-06 2.718e-06 27-Jun-2023 15:03:25 0.0005 "cbau_1515.wdf" "Single Type1" 978.67 30.148 105.33 39.966 9.8185 22.889 1.3333e+06 1.7167e-05 0 27-Jun-2023 15:14:20 0.0005 "cbau_1523.wdf" "Single Type1" 930.67 45.293 52 51.391 6.098 10.342 1.3333e+06 7.7566e-06 0 27-Jun-2023 15:22:22 0.001 "cbau_1524.wdf" "Single Type1" 930.67 17.436 50.667 21.897 4.4615 9.4874 1.3333e+06 7.1155e-06 0 27-Jun-2023 15:24:02 0.0005
A=table2array(rows2vars(mystruct));
whos A
Name Size Bytes Class Attributes A 13x36 62984 cell
A(1:10,:)
ans = 10×36 cell array
Columns 1 through 9 {'name' } {["cba_1457.wdf"]} {["cba_1459.wdf"]} {["cba_1501.wdf"]} {["cba_1502.wdf"]} {["cba_1503.wdf"]} {["cbau_1515.wdf"]} {["cbau_1523.wdf"]} {["cbau_1524.wdf"]} {'meas_type'} {["Type1" ]} {["Type1" ]} {["Type1" ]} {["Type1" ]} {["Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {'V1' } {[ 930.6667]} {[ 932]} {[ 930.6667]} {[ 930.6667]} {[ 930.6667]} {[ 978.6667]} {[ 930.6667]} {[ 930.6667]} {'T1' } {[ 18.0975]} {[ 20.3450]} {[ 23.6075]} {[ 28.6690]} {[ 19.6070]} {[ 30.1475]} {[ 45.2930]} {[ 17.4355]} {'V2' } {[ 50.6667]} {[ 50.6667]} {[ 52]} {[ 50.6667]} {[ 50.6667]} {[ 105.3333]} {[ 52]} {[ 50.6667]} {'T2' } {[ 28.8400]} {[ 29.2830]} {[ 32.8570]} {[ 40.0360]} {[ 28.4145]} {[ 39.9660]} {[ 51.3910]} {[ 21.8970]} {'T_lps' } {[ 10.7425]} {[ 8.9380]} {[ 9.2495]} {[ 11.3670]} {[ 8.8075]} {[ 9.8185]} {[ 6.0980]} {[ 4.4615]} {'Calc1' } {[ 11.3559]} {[ 10.9030]} {[ 11.2766]} {[ 11.5016]} {[ 10.8720]} {[ 22.8890]} {[ 10.3422]} {[ 9.4874]} {'Caltot' } {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {'Calc2' } {[ 8.5169e-06]} {[ 8.1773e-06]} {[ 8.4574e-06]} {[ 8.6262e-06]} {[ 8.1540e-06]} {[ 1.7167e-05]} {[ 7.7566e-06]} {[ 7.1155e-06]} Columns 10 through 17 {["cbau_1525.wdf"]} {["cbau_1527.wdf"]} {["cbav_1533.wdf"]} {["cbav_1534.wdf"]} {["cbav_1535.wdf"]} {["cbaw_1507.wdf"]} {["cbaw_1508.wdf"]} {["cbaw_1510.wdf"]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {["Single Type1" ]} {[ 932]} {[ 932]} {[ 930.6667]} {[ 930.6667]} {[ 932]} {[ 930.6667]} {[ 930.6667]} {[ 932]} {[ 22.9070]} {[ 24.7215]} {[ 32.7730]} {[ 25.6620]} {[ 27.2620]} {[ 21.7725]} {[ 20.9990]} {[ 22.6790]} {[ 50.6667]} {[ 52]} {[ 50.6667]} {[ 52]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 28.1255]} {[ 29.4765]} {[ 35.8245]} {[ 28.4755]} {[ 29.9110]} {[ 26.6270]} {[ 25.7425]} {[ 27.1070]} {[ 5.2185]} {[ 4.7550]} {[ 3.0515]} {[ 2.8135]} {[ 2.6490]} {[ 4.8545]} {[ 4.7435]} {[ 4.4280]} {[ 9.7715]} {[ 9.8520]} {[ 8.8574]} {[ 8.9632]} {[ 8.6415]} {[ 9.6398]} {[ 9.5975]} {[ 9.4715]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 1.3333e+06]} {[ 7.3286e-06]} {[ 7.3890e-06]} {[ 6.6430e-06]} {[ 6.7224e-06]} {[ 6.4811e-06]} {[ 7.2298e-06]} {[ 7.1982e-06]} {[ 7.1036e-06]} Columns 18 through 25 {["gre_1150.wdf"]} {["gre_1156.wdf"]} {["gre_1159.wdf"]} {["gre_1201.wdf"]} {["gre_1202.2.wdf"]} {["gre_1202.wdf"]} {["gre_1207.2.wdf"]} {["gre_1207.wdf"]} {["Type2" ]} {["Type2" ]} {["Type2" ]} {["Type2" ]} {["Type2" ]} {["Type2" ]} {["Type2" ]} {["Type2" ]} {[ 930.6667]} {[ 930.6667]} {[ 930.6667]} {[ 930.6667]} {[ 930.6667]} {[ 930.6667]} {[ 930.6667]} {[ 933.3333]} {[ 58.3890]} {[ 57.5720]} {[ 22.5430]} {[ 23.1870]} {[ 14.0432]} {[ 13.6916]} {[ 3.7632]} {[ 4.5173]} {[ 557.3333]} {[ 52]} {[ 52]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 58.3960]} {[ 59.3040]} {[ 24.2905]} {[ 24.9310]} {[ 15.7944]} {[ 15.4204]} {[ 5.5870]} {[ 6.2514]} {[ 0.0070]} {[ 1.7320]} {[ 1.7475]} {[ 1.7440]} {[ 1.7512]} {[ 1.7288]} {[ 1.8238]} {[ 1.7341]} {[ 47.2407]} {[ 8.2715]} {[ 8.2833]} {[ 8.0683]} {[ 8.0736]} {[ 8.0571]} {[ 8.1262]} {[ 8.0573]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 3.8462e+06]} {[ 1.2283e-05]} {[ 2.1506e-06]} {[ 2.1536e-06]} {[ 2.0977e-06]} {[ 2.0991e-06]} {[ 2.0948e-06]} {[ 2.1128e-06]} {[ 2.0949e-06]} Columns 26 through 33 {["gre__1208.wdf"]} {["grecba_1041.wdf"]} {["grecba_1043.wdf"]} {["grecba_1044.wdf"]} {["grecba_1055.wdf"]} {["grecba_1059.wdf"]} {["grecba_1100.wdf"]} {["grecba_1102.wdf"]} {["unknown" ]} {["Type2 + Type1" ]} {["Type2 + Type1" ]} {["Type2 + Type1" ]} {["Type2 + Type1" ]} {["Type2 + Type1" ]} {["Type2 + Type1" ]} {["Type2 + Type1" ]} {[ 930.6667]} {[ 932]} {[ 932]} {[ 930.6667]} {[ 932]} {[ 932]} {[ 930.6667]} {[ 932]} {[ 0.2785]} {[ 8.7515]} {[ 4.1075]} {[ 6.1640]} {[ 6.6710]} {[ 11.4020]} {[ 13.7140]} {[ 10.9340]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 50.6667]} {[ 2.0312]} {[ 35.0185]} {[ 29.9735]} {[ 32.5580]} {[ 39.0275]} {[ 48.1925]} {[ 47.8365]} {[ 49.1485]} {[ 1.7528]} {[ 26.2670]} {[ 25.8660]} {[ 26.3940]} {[ 32.3565]} {[ 36.7905]} {[ 34.1225]} {[ 38.2145]} {[ 8.0747]} {[ 14.1962]} {[ 14.1353]} {[ 14.2212]} {[ 15.0770]} {[ 15.6761]} {[ 15.3259]} {[ 15.8625]} {[ 1]} {[ 1.0000e-08]} {[ 1.0000e-08]} {[ 1.0000e-08]} {[ 1.0000e-08]} {[ 1.0000e-08]} {[ 1.0000e-08]} {[ 1.0000e-08]} {[ 8.0747]} {[ 1.4196e+09]} {[ 1.4135e+09]} {[ 1.4221e+09]} {[ 1.5077e+09]} {[ 1.5676e+09]} {[ 1.5326e+09]} {[ 1.5862e+09]} Columns 34 through 36 {["Type3_1123.wdf"]} {["Type3_1124.wdf"]} {["Type3_1126.wdf"]} {["Type3" ]} {["Type3" ]} {["Type3" ]} {[ 957.3333]} {[ 932]} {[ 936]} {[ 37.7050]} {[ 9.1402]} {[ 0.1775]} {[ 50.6667]} {[ 52]} {[ 50.6667]} {[ 37.7140]} {[ 9.1486]} {[ 0.1859]} {[ 0.0090]} {[ 0.0084]} {[ 0.0084]} {[ 4.3774]} {[ 4.4763]} {[ 4.3599]} {[ 4000000]} {[ 4000000]} {[ 4000000]} {[ 1.0943e-06]} {[ 1.1191e-06]} {[ 1.0900e-06]}
B=cell(size(A,1),3*(size(A,2)-1));
B(:,[1 2:3:size(B,2)])=A;
B(1:10,1:7)
ans = 10×7 cell array
{'name' } {["cba_1457.wdf"]} {0×0 double} {0×0 double} {["cba_1459.wdf"]} {0×0 double} {0×0 double} {'meas_type'} {["Type1" ]} {0×0 double} {0×0 double} {["Type1" ]} {0×0 double} {0×0 double} {'V1' } {[ 930.6667]} {0×0 double} {0×0 double} {[ 932]} {0×0 double} {0×0 double} {'T1' } {[ 18.0975]} {0×0 double} {0×0 double} {[ 20.3450]} {0×0 double} {0×0 double} {'V2' } {[ 50.6667]} {0×0 double} {0×0 double} {[ 50.6667]} {0×0 double} {0×0 double} {'T2' } {[ 28.8400]} {0×0 double} {0×0 double} {[ 29.2830]} {0×0 double} {0×0 double} {'T_lps' } {[ 10.7425]} {0×0 double} {0×0 double} {[ 8.9380]} {0×0 double} {0×0 double} {'Calc1' } {[ 11.3559]} {0×0 double} {0×0 double} {[ 10.9030]} {0×0 double} {0×0 double} {'Caltot' } {[ 1.3333e+06]} {0×0 double} {0×0 double} {[ 1.3333e+06]} {0×0 double} {0×0 double} {'Calc2' } {[ 8.5169e-06]} {0×0 double} {0×0 double} {[ 8.1773e-06]} {0×0 double} {0×0 double}
mystruct isn't a struct at all, it's a table...so, not knowing what the deal is with that, the above takes it and creates the requested format to write to the Excel file for that data.
The array data are nowhere in sight, but simply do something similar with them and then write to the sheet in the desired location separately or append to B in the correct locations desired.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!