help with xlswrite

34 次查看(过去 30 天)
Abra dog
Abra dog 2011-11-1
评论: YUN ZHAO 2020-7-21
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?

采纳的回答

Walter Roberson
Walter Roberson 2011-11-1
编辑:John Kelly 2015-2-27
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 个评论
Abra dog
Abra dog 2011-11-1
if i typed the code even with the error can i still use the code on a PC?
Walter Roberson
Walter Roberson 2011-11-2
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.

请先登录,再进行评论。

更多回答(2 个)

Xing Zhang
Xing Zhang 2013-2-17
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 个评论
Walter Roberson
Walter Roberson 2013-2-17
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.

请先登录,再进行评论。


Stacey
Stacey 2020-1-9
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')

标签

Community Treasure Hunt

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

Start Hunting!

Translated by