Write 10 excel for loop
1 view (last 30 days)
Show older comments
Now I have one original.xlsx, I want to write Data in Sheet1 from E1.
xlswrite('Original.xlsx',Data,'Sheet1','E1')
How do I write 10 different Data{#} into Original.xlsx as different excel name, all in Sheet1 from E1.
for i=1:10
xlswrite('Original.xlsx',Data{i},'Sheet1','E1')?
2 Comments
Walter Roberson
on 4 Mar 2016
To confirm, you want to produce just one output file, right? Do you want the ten in different sheets, or do you want them to go into E1 then E2 then E3 and so on? Or do you want the first to start at E1 and then the second to be in E but below where-ever the first finished? Or ...?
Accepted Answer
Walter Roberson
on 4 Mar 2016
filenames = {'January.xlsx', 'February.xlsx', 'Cheezburger.xlsx', 'Durango95.xlsx', 'EschewObfuscation.xlsx', ....};
for i=1:10
xlswrite(filenames{i}, Data{i}, 'Sheet1','E1');
end
3 Comments
Walter Roberson
on 4 Mar 2016
I am not sure what you mean; perhaps you mean something like,
filenames = {'January.xlsx', 'February.xlsx', 'Cheezburger.xlsx', 'Durango95.xlsx', 'EschewObfuscation.xlsx', ....};
[~, ~, orig_raw] = xlsread('Original.xlsx', 'Sheet1');
for i=1:10
xlswrite(filenames{i}, orig_raw, 'Sheet1');
xlswrite(filenames{i}, Data{i}, 'Sheet1','E1');
end
However if you were going to do that then I would use
filenames = {'January.xlsx', 'February.xlsx', 'Cheezburger.xlsx', 'Durango95.xlsx', 'EschewObfuscation.xlsx', ....};
[~, ~, orig_raw] = xlsread('Original.xlsx', 'Sheet1');
for i=1:10
outdata = orig_raw;
new_data = Data{i};
outdata(1:length(new_data),5) = new_data;
xlswrite(filenames{i}, outdata, 'Sheet1');
end
Note: if Data{i} a numeric vector then you would use
for i=1:10
outdata = orig_raw;
new_data = num2cell(Data{i});
outdata(1:length(new_data),5) = new_data;
xlswrite(filenames{i}, outdata, 'Sheet1');
end
But if Data{i} is a numeric scalar or string then you would use
for i=1:10
outdata = orig_raw;
new_data = Data{i};
outdata{1,5} = new_data;
xlswrite(filenames{i}, outdata, 'Sheet1');
end
More Answers (1)
Joachim Schlosser
on 4 Mar 2016
You can write the whole cell array at once. See the cell array example in the online help: http://www.mathworks.com/help/releases/R2015b/matlab/ref/xlswrite.html
If you are asking about the for loop because you first are calculating each cell before writing, I'd advise to export to Excel in a separate step after the loop for performance reasons.
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!