how to write a matrix in excel by changing cells after loop
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!