help with xlswrite

42 views (last 30 days)
Abra dog
Abra dog on 1 Nov 2011
Commented: YUN ZHAO on 21 Jul 2020
I'm trying to put a column of words and a column of numbers together on a excel spread sheet. I wrote:
G = column of words
F = column of number
xlswrite('filename',G,F)
Matlab returns with the error:
??? Error using ==> xlswrite at 179
An error occurred on data export in CSV format.
Caused by:
Error using ==> dlmwrite at 112
The input cell array cannot be converted to a matrix.
Is there anyway for me to do this without turning the words into numbers with dlmwrite?

Accepted Answer

Walter Roberson
Walter Roberson on 1 Nov 2011
Edited: John Kelly on 27 Feb 2015
If xlswrite() delegated to dlmwrite() then the implication is that you are either not using an MS Windows machine or else that you do not have Excel installed. In such cases, xlswrite() is restricted to writing numeric arrays.
It is possible to write text using dlmwrite(), by submitting an array of pure characters and using a character format in the Precision parameter and setting the delimiter to be empty.
But if you are going to do that then you might as well follow the recommendations linked to in the dlmwrite documentation to use low-level operations: http://www.mathworks.com/help/matlab/ref/dlmwrite.html
  4 Comments
Abra dog
Abra dog on 1 Nov 2011
if i typed the code even with the error can i still use the code on a PC?
Walter Roberson
Walter Roberson on 2 Nov 2011
I do not understand that question.
The first argument to xlswrite() must be a file name. The second argument must be a numeric array (all versions) or alternately a cell array (MS Windows with Excel installed only). The third argument if provided must be the sheet or range specification.
A range specification must be a string, and your G is not a string, so your G cannot act as a range.
A sheet specification can be a string, or a scalar positive integer value that specifies the sheet index. If your G *happens* to consist of a single entry that is a positive integer, then it would be accepted as a range, but if your G is 0 or negative or is not a scalar value, then your G would not be accepted as a range.
On systems other than MS Windows, or on MS Windows systems that do not have Excel installed, the sheet and range would be ignored; those on those systems your G would be ignored. On MS Windows systems with Excel, if your G did not happen to be a positive integer, then you would get an error.
Your xlswrite() call as written is thus unlikely to work on any system: on MS Windows systems with Excel it is going to error because of the G, and on anything else it is going to error because on those systems xlswrite() cannot write character arrays.

Sign in to comment.

More Answers (2)

Xing Zhang
Xing Zhang on 17 Feb 2013
But I do have Microsoft Office 2011 for Mac installed on my Macbook Pro 15 inch, with Mountain Lion installed, and I am having exactly the same problem.
The code is running with no problem in my HP PC (Windows 8, Matlab 2012b), but when I run this code in Macbook Pro (Mountain Lion 10.8.2, Matlab 2012b) I got the same error.
Can someone help shoot this bug?
Thanks
  1 Comment
Walter Roberson
Walter Roberson on 17 Feb 2013
xlswrite() can only communicate directly with Excel on MS Windows.
On machines with other operating systems no matter what they have installed, or on MS Windows machines that do not have Excel installed, xlswrite() is restricted to "basic" mode, which can only handle numeric arrays.

Sign in to comment.


Stacey
Stacey on 9 Jan 2020
From 2013b up, you can use writetable instead:
col_num = [1;2;3]
col_word = ['a';'b';'c']
writetable(table(col_num,col_word),'filename.xlsx')
  1 Comment
YUN ZHAO
YUN ZHAO on 21 Jul 2020
It's working for me. Much appreciate!

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!