how to write a matrix in excel by changing cells after loop

1 view (last 30 days)
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
Yona
Yona on 4 Dec 2014
Do you run it? what was the result? do you get an error?
Jo
Jo on 5 Dec 2014
for I=1 it runs ok and my data are written at the right cell 'C2'. my question is how to write the data at the cell 'D2' at the next cycle, when I=2 (at 'E2' when I=3, etc).

Sign in to comment.

Accepted Answer

Ingrid
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!