Search code examples
stringexcelmatlabmatrixcell-array

Store MATLAB Strings contained in cell array in excel Spreadsheet


I'm storing MATLAB strings in Excel spreadsheet.

My data is like:

Matrix = 
[644x16 char]
[686x16 char]
[569x13 char]
[660x13 char]
[619x13 char]
[599x13 char]
[607x13 char]
[702x13 char]
[633x13 char]
[623x11 char]
[645x11 char]
[697x11 char]
...

I want to save these strings in an Excel column. Due to not all the strings having the same number of character, I was forced to store this data on a cell array.

Any ideas? Thank you so much mates.


Solution

  • To write multiple matrices of different size to an Excel file with xlswrite, specify the starting cell for each submatrix:

    xlsWriteMatCell.m

    function xlsWriteMatCell(fileName,M)
    
    startCells = cumsum([1 cellfun(@(c)size(c,1),M)]);
    startCells(end) = [];
    for ic = 1:numel(startCells),
        xlswrite(fileName,M{ic},1,['A' num2str(startCells(ic))]);
    end
    

    Test

    Matrix = {rand(5,4),rand(6,6),rand(3,3)};
    xlsWriteMatCell('test.xlsx',Matrix)
    

    enter image description here

    With characters:

    Matrix = {char(randi(25,5,4)+64),char(randi(25,6,6)+64),char(randi(25,3,3)+64)}
    xlsWriteHetro('testChars.xlsx',Matrix)
    

    enter image description here

    This will be much slower than writing to a raw csv file, but it will give you an Excel spreadsheet.