Search code examples
matlabmatrixformattingexportexport-to-excel

How to write matrices from matlab to .xlsx with special formatting tables


I have one problem with exporting matrices from Matlab to Excel. This is not a problem, but I need some formatting. I made matrices A and B and I printed them to .xlsx document.

filename = 'example.xlsx'; 
A;
sheet = 1;                 
xlRange = 'A9';           
xlswrite(filename,A,sheet,xlRange)  

B;
xlRange2= 'B9';
xlswrite(filename,B,sheet,xlRange2)

And i get the example.xlsx file with this formating:

400 4.56
500 5.12
600 6.76
700 7.98
800 8.21
900 9.21
1000 10.12
1100 11.23
1200 12.43
1300 13.89
1400 14.54
1500 15.21
1600 16.23
1700 17.53

I need this kind of formating:

400 4.56

500 5.12
600 6.76
700 7.98
800 8.21
900 9.21

1000 10.12
 100 11.23
 200 12.43
 300 13.89
 400 14.54

 500 15.21
 600 16.23
 700 17.53

Steps are on 500, 1000, 1500, 2000, 2500... How to put one empty row and how to make this kind of formating?


Solution

  • This code provides the cell as required for xlswrite:

    M=[400 4.56
    500 5.12
    600 6.76
    700 7.98
    800 8.21
    900 9.21
    1000 10.12
    1100 11.23
    1200 12.43
    1300 13.89
    1400 14.54
    1500 15.21
    1600 16.23
    1700 17.53
    900 9.21
    1000 10.12
    1100 11.23
    1200 12.43
    1300 13.89
    1400 14.54
    1500 15.21
    1600 16.23
    1700 17.53];
    gaps=[500, 1000, 1500, 2000, 2500];
    %calculates a group indx. 0 is below first gap, 1 between first and second etc..
    group=sum(bsxfun(@ge,M(:,1),gaps),2);
    %whenever group increases a line must be jumped, calculate indices
    index=cumsum(ones(size(M,1),1)+[0;diff(group)>0]);
    %allocate empty cell
    X=cell(max(index),size(M,2));
    %fill data
    X(index,:)=num2cell(M);
    xlswrite('a.xlsx',X)