MATLAB: XLSWRITE not behaving as expected

csvdlmwriteheadersxlswrite

I am trying to run the code
c1 = {'string';'data';'cell'};
c2 = {1;2;3};
xlswrite('features.xls',[c1,c2]);
but get the error Error using xlswrite (line 194) An error occurred on data export in CSV format.
Caused by:
Error using dlmwrite (line 104)
The input cell array cannot be converted to a matrix.
This happens because, dlmwrite tries to
if (iscell(m))
try
m = cell2mat(m);
catch
error(message('MATLAB:dlmwrite:CellArrayMismatch'));
end
end
I am using Matlab 2018a on OSX.
Am I missing something here or has the source code changed over the years?

Best Answer

"Am I missing something here or has the source code changed over the years?"
What you're missing in dlmwrite is
dlmwrite(filename,M) writes numeric data in array M to an ASCII format file, filename, using
the default delimiter (,) to separate array elements. ...
The key is numeric data; it's documented to not work on anything else and it's been that way "since forever". It's one of the earliest "high level" i/o functions that far predates any of the fancy stuff like cell arrays.
I wasn't aware that xlswrite calls dlmwrite for .csv output but makes sense for certain data. What I don't understand is what made it think it was writing .csv data.
>> delete test.xls
>> xlswrite('test.xls',[c1 c2])
>> [~,~,r]=xlsread('test.xls')
r =
3×2 cell array
{'string'} {[1]}
{'data' } {[2]}
{'cell' } {[3]}
>>
worked as expected here (or at least as I would expect); have you tried the expedient of just restarting Matlab, sometimes the COM server gets confused and I'm thinking something of the sort might be happening here...you do have Excel on the machine, right???
ADDENDUM
NB:
Limitations
If your computer does not have Excel for Windows®, or if the COM server (part of
the typical installation of Excel) is unavailable, then the xlswrite function:
Writes array A to a text file in comma-separated value (CSV) format.
A must be a numeric matrix.
...
That may be why you got the call to dlmwrite for a .xls extension; in fact I'd almost bet it is...