How to use xlswrite with cell arrays in a loop?

1 view (last 30 days)
Hi everyone,
Suppose that I have the following cell array:
AA = cell(10,1);
for i = 1 : 10
A = randn(20,1);
AA{i,1} = A;
end
And I want to write in Excel The rows of AA in spreadsheets j. For example,
for i=1
xlswrite('Example.xls', AA{1,1},'Sheet1','a1')
for i=2
xlswrite('Example.xls', AA{2,1},'Sheet1','b1')
and so on
How could I use a loop to avoid re-writing the same command 10 times?

Accepted Answer

Jan
Jan on 17 Jan 2019
Edited: Jan on 17 Jan 2019
for k = 1:10
Range = [char('a' + k - 1), '1']
xlswrite('Example.xls', AA{k,1}, 'Sheet1', Range)
end
The conversion to the Excel range fails for more than 27 columns. Prefer a stable function instead, e.g. https://www.mathworks.com/matlabcentral/fileexchange/58917-getexcelrange-rowlimits-columnlimits
  1 Comment
gsourop
gsourop on 17 Jan 2019
Thank you for your answer!
However, this command can support only up to 'z', whereas Excel contains columns AA, AB etc.. . So, if AA is a cell(32,1) and i = 1:32, for example, I cannot generalize this command.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!