write a cell information in excel file

2 views (last 30 days)
Dear all,
could you please advise on how I could write cell information in excel file. here is some part of the code.
out_weight=num2cell(1:100)
for i=1:100
xlswrite('P:\....excelfile.xls',out_weight{i}, 'Sheet2','b9');
end
out_weight{i} represents the cell. The matrices in the cell do not have the same size so unfortunately I cannot convert my cell into a matrix and write it in excel. As you can see in my code I used a loop which I am sure is not the most efficient way to do what I want to do. Any thought on this?
My second problem is regarding the loop, every time it loops the column of the cell changes but I do not manage to change the location in excel file. What I mean is that every time it loops it should change from b9 to c9 to d9 etc unfortunately I do not manage to include that in my code. Any advice is most welcome. Thanks a lot
S

Accepted Answer

Walter Roberson
Walter Roberson on 11 Dec 2012
Edited: Walter Roberson on 11 Dec 2012
AZ = 'A':'Z';
for K = 1 : length(out_weight)
if K <= length(AZ)
colname = AZ(K);
else
d1 = floor( (K-1) / length(AZ) ) + 1;
d2 = mod( K - 1, length(AZ) ) + 1;
colname = AZ([d1 d2]);
end
xlswrite(TheFileName, out_weight{K}, 'Sheet2', [colname '9']);
end
This could would need to be enhanced a bit if there are more than 676 items.
Note: using xlswrite() this many times is not recommended on MS Windows systems. Instead, it is recommended that you open an ActiveX session that talks to Excel, then use the appropriate Excel ActiveX methods in the loop to do the writing, and afterwards end the ActiveX session.

More Answers (1)

Saad
Saad on 11 Dec 2012
Thanks a lot Walter...I have tried the code and it is running nicely although I haven't fully understood it. Just a quick question please, i was just wondering whether when matlab writes the data in excel, it preserves the ordering of the column?
I am currently exploring Activex that you mentioned. Is it supposed to be faster and more stable that using a loop with xlswrite?
I have tried this code but it didn't write anything on excel (maybe I am missing something)
excel = actxserver('Excel.Application'); Excel.Visible = 1; file = excel.Workbooks.Open('P:...excelfile.xls');
sheet1=excel.Worksheets.get('Item', 'Sheet1');
range1=get(sheet1,'Range','A1:C3');
range1.Value=rand(3);
i didn't write anything on A1:C3...
Thank you
Regards
S
S

Community Treasure Hunt

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

Start Hunting!