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:

  • How to add pivot table with Excel::Writer::XLSX
  • Count columns based on date range
  • Importing multiple CSV to multiple worksheet in a single workbook
  • NPOI : How To Read File using NPOI
  • Using power function in tableau
  • Read XLSX file in Java
  • 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?

  • VLookup then nested If's
  • Using ADODB in VBScript to find the number of rows in an Excel sheet?
  • ClosedXml “display value” and “real value”
  • OLEDB error with OLAP cube offline
  • hide a barchart section in excel graph but display it data table..vb winforms
  • AMPL / Gurobi model conversion to Excel / Solver / @Risk
  • 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); 
    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.

    MS Excel Spreadsheet is the best Office Software, Excel VBA and Excel Formulas make Spreadsheet work faster.