problem with writting data to excel from matlab

3 views (last 30 days)
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

Accepted Answer

dpb
dpb on 10 May 2017
>> 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 Comments
best16 programmer
best16 programmer on 11 May 2017
thank you , it works great but when i use columns with different dimensions it don't work.
dpb
dpb on 11 May 2017
Edited: dpb on 11 May 2017
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...)

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!