more efficient code while using xlswrite and xlsread

2 views (last 30 days)
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

Accepted Answer

Jon
Jon on 23 Sep 2019
Edited: Jon on 23 Sep 2019
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 Comments
Jon
Jon on 24 Sep 2019
Edited: Jon on 24 Sep 2019
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 on 24 Sep 2019
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!

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!