problem with writting data to excel from matlab

1 次查看(过去 30 天)
i have a problem with xlswrite function when the data is being writting in excel file i get the following error:
#N/A
here' the code that i'm using
d = {'A', 'B','C','D'; A, B, C, D}
xlswrite('my.xlsx',d);
with A,B,C and D are columns with different data
thank you

采纳的回答

dpb
dpb 2017-5-10
>> help xlswrite
...
A — Data to write
matrix | cell array
Data to write, specified as a two-dimensional numeric or character array,
or, if each cell contains a single element, a cell array.
If A is a cell array containing something other than a scalar numeric or a
string, then xlswrite silently leaves the corresponding cell in the spreadsheet empty.
...
d = {'A', 'B','C','D'};
xlswrite('my.xlsx',[d;num2cell([A, B, C, D]]);
will work, however.
You can arrange the construction of the end cell array however is convenient; I just used the variables as you had them defined to illustrate what you need to satisfy xlswrite requirements.
Demo:
>> a=randn(5,2); % dummy data set
>> d = {'A', 'B'}; % column headers
>> c=[d;num2cell(a)] % what looks like in cell array
c =
'A' 'B'
[-1.7502] [-0.5336]
[-0.2857] [-2.0026]
[-0.8314] [ 0.9642]
[-0.9792] [ 0.5201]
[-1.1564] [-0.0200]
>> xlswrite('best15.xls',c) % write it out
>> [~,~,r]=xlsread('best15.xls') % read it back to be sure -- raw data
r =
'A' 'B'
[-1.7502] [-0.5336]
[-0.2857] [-2.0026]
[-0.8314] [ 0.9642]
[-0.9792] [ 0.5201]
[-1.1564] [-0.0200]
>>
  2 个评论
best16 programmer
best16 programmer 2017-5-11
thank you , it works great but when i use columns with different dimensions it don't work.
dpb
dpb 2017-5-11
编辑:dpb 2017-5-11
It'll work as long as the columns you're concatenating are consistent in the direction of the catenation. Everything's got to be regular from which to build a rectangular 2D cell array. If you must augment some dimension with empty cells to make that occur, so be it; either that or write multiple sections independently (which requires keeping track of where to put them and all that...)

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by