more efficient code while using xlswrite and xlsread

조회 수: 2 (최근 30일)
alex
alex 2019년 9월 23일
댓글: alex 2019년 9월 24일
Hello!
I use the code below,which works fine, but i believe there is a more sufficient way to write the code.
function Copydata(excelfile1,excelfile2,n)
for i=1:n*3
T(:,i)=xlsread(excelfile1,i,'A43:A51');
end
i=1;
for j=1:n
xlswrite(excelfile2,T(:,i),j,'O17:O25')
i=i+1;
xlswrite(excelfile2,T(:,i),j,'P17:P25')
i=i+1;
xlswrite(excelfile2,T(:,i),j,'Q17:Q25')
i=i+1;
end
i tried readtable() and writetable() but they are a bit slower!
Any idea how to rewrite the code either using the same commands(xlswrite,xlsread), or using something else like ActiveX?
Thank you in advance!
P.S: I am using Matalab R2014a

채택된 답변

Jon
Jon 2019년 9월 23일
편집: Jon 2019년 9월 23일
Can you use just one call as follows
xlswrite(excelfile2,T(:,1:3),j,'O17:Q25')
or assuming that that T has only 3 columns
xlswrite(excelfile2,T,j,'O17:Q25')
p.s. In your question title, I think you mean to say more efficient rather than more sufficient
  댓글 수: 4
Jon
Jon 2019년 9월 24일
편집: Jon 2019년 9월 24일
Are you sure you have the range as 'O17:Q25' and not 'O17:O25' ? O and Q look very similar could be an easy one to overlook.
When I do a simple test of this approach it seems to work fine.
For example if I type on the command line
T = randn(9,10),i=4,j=3,xlswrite('junk.xlsx',T(:,i:i+2),j,'O17:Q25')
2019-09-24 08_10_27-Microsoft Excel - junk.xlsx.png
Note you may have to scroll the window to the right to see the O,P,Q columns in the screen shot. Seems like it works here. Is this exactly what you are doing within your code?
P.S. Please know that I was just trying to clarify on the sufficient/efficient wording, I know English can be really difficult and I admire your efforts to master it.
alex
alex 2019년 9월 24일
I run the same code to another computer with R2019 matlab and it works fine. in my computer it only xlswrites in the "O" column.
Maybe it is because i have an old version of Matlab.
Thanks again Jon!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 ActiveX에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by