Using a for loop how could I export my data values in an the same Excel sheet without overwitting?

Dear all, I am using a FOR loop to export data to an excel sheet, In the 1st iteration I would like to exported xdis and ydis values to be written in columns A and B , while in the 2nd iteration to be written in columns C and D, while in the 3rd iteration in columns E and F, and so on. but I face a problem that each time the excel sheet columns A and B is overwritten, my code can not write in the following successive columns.
for i = 1:length(str)
xdis = str(i).x;
ydis = str(i).y();
xlswrite('StreamLines.xls',xdis,1,'A1')
xlswrite('StreamLines.xls',ydis,1,'B1')
end
Any suggestion on how I can solve that issue, so I can get the xdis and ydis values for the 2nd iteration written in columns C and D, and for the 3rd iteration written in columns E and F, and so on. thanks.

Answers (1)

I think there are 2 ways to do this, as shown in the following. If your data size is not so big, I prefer the first one.
(1)
% Assign memory space to keep all the data
data = zeros(10,8);
for i = 1:4
xdis = [1:10]';
ydis = rand(10,1);
data(:,i*2-1:i*2) = [xdis,ydis];
end
xlswrite('StreamLines.xls',data,1);
(2)
pos = {'A1','B1','C1','D1','E1','F1','G1','H1'};
for i = 1:4
xdis = [1:10]';
ydis = rand(10,1);
xlswrite('StreamLines.xls',xdis,1,pos{i*2-1})
xlswrite('StreamLines.xls',ydis,1,pos{i*2})
end

Categories

Products

Asked:

s s
on 24 Aug 2017

Edited:

on 4 Sep 2017

Community Treasure Hunt

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

Start Hunting!