How to use XLSWRITE function in the for loop?
2 views (last 30 days)
Show older comments
I have a code (attached file) which requires inputing any number detected in the matlab code into rows and columns within the same dimensions provided in the MATLAB code. I know the syntax is
xlswrite(filename, M)
But how do I make it write in different rows and columns on the excel file. Attached is the code for further clarification. I need help determining where exactly I'm to place this line of code so that the code runs perfectly. And the new inputted value doesn't overlap the previous one.
Attached are all the required codes,images and source files.
Thank you
5 Comments
Walter Roberson
on 6 Feb 2019
When you are looping writing to different locations in the same excel file, then a lot of the time the best approach is Don't Do That. That is, most of the time it is more efficient to read in the entire file, change the locations in memory, and then write out the entire new version once at the end. This is especially true for .xlsx files, which are really a zip'd directory of text XLM files that have to be updated as text each time you make a change.
Answers (1)
Shawn Duenas
on 6 Feb 2019
Keep a counter of the number of rows:
insertRow=1;
for iMat=1:nMat
nRow=size(M,1);
xlsRange=['A',int2str(insertRow),':Z',int2str(insertRow+nRow-1)]
xlswrite(filename, M, xlsRange);
insertRow=insertRow+nRow;
end
Note that if the width of M is 26 then printing from A:Z works, but you'll need to find an algorithm that gives you the right column letter for your matrix width (size(M,2)).
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!