can use xlswrite in for loop without overwriting?

조회 수: 5 (최근 30일)
sandy
sandy 2013년 8월 13일
댓글: Anand Ganesan 2020년 4월 6일
i need to write values of every iteration from loop and get saved in excel file one by one order..its like appending..is it possible in matlab?
  댓글 수: 2
SANDEEP KUMAR
SANDEEP KUMAR 2018년 6월 20일
편집: SANDEEP KUMAR 2018년 6월 20일
Yes it is possible. follow the code, Here objective is writing these variables value in excel sheet.%%CODE START FROM HERE%%
x1=[1,2,3,4,5]; x2=[5,6,7,8,9];
for ii=1:5 X1=x1(1,ii); X2=x2(1,ii); filename = 'Results.xlsx'; Variables= {X1,X2}; sheet = 1; xlRange= sprintf( 'A%s',num2str(ii) ); xlswrite(filename,Variables,sheet,xlRange) end %% Arranging row wise
Image Analyst
Image Analyst 2018년 6월 20일
Sandeep, can you put your answer (to this 5 year old question) down in the Answers section below (so you can get credit for it)? Though please format your code first.

댓글을 달려면 로그인하십시오.

답변 (2개)

David Sanchez
David Sanchez 2013년 8월 13일
With xlswrite, you can define the cell where your data will be written, like in the following example, that write mixed text and numeric data to testdata2.xls, starting at cell E1 of Sheet1:
d = {'Time','Temperature'; 12,98; 13,99; 14,97};
xlswrite('testdata2.xls', d, 1, 'E1')
Change the destination cell dynamically in your for-loop. Example:
for k=1:20
x = rand;
my_cell = sprintf( 'A%s',num2str(k) );
xlswrite('my_xls.xls',x,my_cell);
end
  댓글 수: 2
sandy
sandy 2013년 8월 13일
Anand Ganesan
Anand Ganesan 2020년 4월 6일
To get it to write into one column:
for k=1:3
x = rand;
my_cell = sprintf( 'A%s',num2str(k) )
my_cell=[my_cell ':' my_cell];
xlswrite('my_xls.xls',x,my_cell);
end
for k=1:3
x=rand
my_cell=sprintf('A%s,num2str(k)')
my_cell=[my_cell ':' my_cell]
xlswrite('my_xls.xls',x,my_cell)
end

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2013년 8월 13일
편집: Image Analyst 2018년 6월 20일
You definitely don't want to use xlswrite() in a loop of more than about 3 iterations unless you are willing to wait forever. Use xlswrite1() in the File Exchange, where you can use ActiveX - it will be so much faster.
  댓글 수: 3
Image Analyst
Image Analyst 2013년 8월 14일
Yes it can, if you specify different destination cells. If you specify cells with entries in them already then, of course, those would get overwritten. The problem with your code is that you are not specifying the destination cell(s) location in the workbook so they all get written starting at cell A1 - the upper left.
sandy
sandy 2013년 8월 14일
I use s (k,:) instead of s in my code..showing error as, variable s appears to change size on every loop iteration.consider preallocating for speed..how to come over this error...?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by