matlab multidimensional array to excel
I have a Matlab script with an output of a multidimensional array LCOE (3 dimensions) of size 16:12:34. This output needs to be written to Excel, therefore I use xlswrite.
I have tried this:
T = LCOE(:,:,1); xlswrite('filename', T, 'sheetname', 'B2');
This does what it’s supposed to, but only writes one table to excel, and I would like to write all 34 tables to excel underneath each other, spaced by `2 blank rows.
Then, I tried this:
for y = 1:34 T = LCOE(:,:,y) xlswrite('filename', T, 'sheetname', strcat('B', num2str(2+(y-1)*18)));
This works, but is very slow, since matlab writes each table separately to excel. Is there a faster way to do this?
One Solution collect form web for “matlab multidimensional array to excel”
Instead of using
xlswrite again and again. Dump all the values of a 3D matrix into a 2D matrix and add rows of
NaNs so that when you write that to excel file, you get 2 blank rows.
Following code improves the execution time by the factor of more than 10.
LCOE = 100*rand(16,12,34); % Taking random values for LCOE T = NaN(18*34-2 ,12); % 1. Pre-allocation 2. 16+2 = 18 % Following loop dumps all the values of 3D matrix into a 2-D followed by 2 rows of NaN % to leave 2 blank rows in excel file. for k = 1:34 T(18*(k-1)+[1:16], :) = LCOE(:,:,k); end xlswrite('filename', T, 'sheetname', 'B2'); % Writing the Excel file
In my system, my code takes about 1 second to execute while your code takes about 10.5 seconds. So that’s a significant difference.