how to write a matrix in excel by changing cells after loop
2 views (last 30 days)
Show older comments
Hello
I have a large spread sheet of data and I use a loop I=1:52 in order to do some calculations. After each loop, an array v(:,1) is formed. I want to export this array in the excel after each cycle. for I=1, I use the function xlswrite, and the array is exported at the right position 'C2'.
name=Acdelta(:,1); %contains the name-data
site=Acdelta(:,3); %contains the sites-data
T={'subjects';1:52};
xlswrite('TEST.xlsx',T,'MI','A1');
xlswrite('TEST.xlsx',1:52 'MI','C1'); %write the numbers 1:52, starting from the cell C1
when I=1, I write the array at the cell, 'C2', etc
xlswrite('TEST.xlsx',v(:,1),'MI','C2');
when I=2, I want to write the new array at the next cell, 'D2', etc
Many thanks in advance.
2 Comments
Accepted Answer
Ingrid
on 5 Dec 2014
Edited: Ingrid
on 5 Dec 2014
to speed up your code you can use the actxserver instead of xlswrite
%Open an ActiveX connection to Excel
h = actxserver('excel.application');
%Create a new work book (excel file)
wb=h.WorkBooks.Add();
%Write the data
h.Activesheet.Range('C2').value = dataValues;
% save the file with the given file name, close Excel
wb.SaveAs(file);
wb.Close;
h.Quit;
h.delete;
To find in which column to write your results (i.e. move the letter) I find the following code on file exchange very helpful: http://www.mathworks.com/matlabcentral/fileexchange/27182-excel-column-conversion
Just call it first to convert letter to number, than add 1, and than call again to find the next letter so for your specific case:
colNum = ExcelCol('C');
newColLetter = ExcelCol(colNum + 1);
xlswrite('TEST.xlsx',v(:,1),'MI',sprintf(%s2,newColLetter));
But as indicated before it is much faster to use the actxserver than to use xlswrite and it has the added advantage that you can manipulate much more things
0 Comments
More Answers (0)
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!