Search code examples
excelmatlabmatrixcell-array

Writing a char matrix to Excel using xlswrite


Could someone assist in writing a char matrix to Excel? The following is a <17x6 char>

A =

SPK01a
SPK03a
SPK03b
SPK05a
SPK07a
SPK07b
SPK07c
SPK09a
SPK11a
SPK15a
SPK17a
SPK19a
SPK21a
SPK23a
SPK25a
SPK27a
SPK29a

I was trying to do xlswrite(filename, {A}) but receive the error:

Error using xlswrite (line 220)
ActiveX - Element of a cell array cannot be a character matrix.

I need the strings to be in cells, so in this case, in the first column in Excel, in 17 separate cells.


Solution

  • You can use cellstr to make a properly shaped cell array:

    xlswrite(fileName,cellstr(A))
    

    cellstr provides a quick way to place each row of a character array into a separate cell, and it will remove trailing spaces at the end of each row.