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

조회 수: 1 (최근 30일)
Jo
Jo 2014년 12월 4일
편집: Ingrid 2014년 12월 5일
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
Yona
Yona 2014년 12월 4일
Do you run it? what was the result? do you get an error?
Jo
Jo 2014년 12월 5일
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).

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

채택된 답변

Ingrid
Ingrid 2014년 12월 5일
편집: Ingrid 2014년 12월 5일
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개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by