# I have 15 columns of different size. I want to write it in a single excel sheet with 15 columns and different row sizes. When I run the following code, I got a message "Conversion to cell from double is not possible". How to resolve this issue?

6 views (last 30 days)
Pankaj Dey on 4 Aug 2016
Commented: dpb on 5 Aug 2016
% Storing the data in a single sheet
a1=data1(:,4);
a2=data2(:,4);
a3=data3(:,4);
a4=data4(:,4);
a5=data5(:,4);
a6=data6(:,4);
a7=data7(:,4);
a8=data8(:,4);
a9=data9(:,4);
a10=data10(:,4);
a11=data11(:,4);
a12=data12(:,4);
a13=data13(:,4);
a14=data14(:,4);
a15=data15(:,4);
n=max([length(a1),length(a2),length(a3),length(a4),length(a5),length(a6),length(a7),length(a8),length(a9),length(a10),length(a11),length(a12),length(a13),length(a14),length(a15)]);
R=cell(15052,15);
R(1,1:length(a1))=a1;
R(2,1:length(a2))=a2;
R(3,1:length(a3))=a3;
R(4,1:length(a4))=a4;
R(5,1:length(a5))=a5;
R(6,1:length(a6))=a6;
R(7,1:length(a7))=a7;
R(8,1:length(a8))=a8;
R(9,1:length(a9))=a9;
R(10,1:length(a10))=a10;
R(11,1:length(a11))=a11;
R(12,1:length(a12))=a12;
R(13,1:length(a13))=a13;
R(14,1:length(a14))=a14;
R(15,1:length(a15))=a15;
xlswrite('annualmean.xls',R)
dpb on 5 Aug 2016
"...write it in a single excel sheet with 15 columns and different row sizes"
NB: The orientation of your array as written will write in rows with different column lengths instead.

dpb on 4 Aug 2016
Edited: dpb on 4 Aug 2016
Learn to use arrays (or named structure fields) rather than creating all those named variables
I'd do something more like
d=dir('Gauge-Discharge Data*.xlsx'); % get all the wanted files..
colmn='A'; % initial column; modify as needed
for i=1:length(d) % iterate over the collection
R=xlsread(d(i).name); % get each workbook in turn...
rnge=sprintf('%c:%c',colmn,colmn); % create range expression 'A:A', 'B:B', ...
xlswrite('annualmean.xls',R(:,4),rnge) % and write that column
colmn=colmn+1; % increment column for next iteration
end
NB: Air code; no guarontee against typos or dumb mistakes like mismatched/missing parens, etc., altho did check the string generation for the column...debug as needed and as always "salt to suit" :)

Walter Roberson on 4 Aug 2016
R(1,1:length(a1)) = num2cell(a1);
dpb on 5 Aug 2016
Sidebar note...I just learned xlswrite is cell array enabled; I had presumed that it would be limited to "normal" arrays.
I still think creating a zillion variables instead of a single array is a_bad_idea(tm)_, but could use a cell array to accumulate in previous solution instead of writing each column one-at-a-time.