每个循环后将信息写入Excel [英] write information into Excel after each loop

查看:134
本文介绍了每个循环后将信息写入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个6x7矩阵(A)和一些我正在做的操作k = 1:50(循环数)。
我知道为了在excel文件中编写一个矩阵,我需要:

  xlswrite('Filename。 xls',A,'A1')

如何在每个循环后写入信息,以这种方式,每个循环都在Excel中的位置A1 +((k-1)* 6 + 1)处开始,并且我将Matrix(A)的所有结果列在彼此之间。



谢谢!

解决方案

使用 XLSWRITE 功能,您可以避免每次通过指定页数和范围来计算Excel单元格范围,其中范围只给出第一个单元格开始。



如果矩阵改变每个迭代的大小,这是特别有用的;我们只需要使用最后一个单元格并将其移动矩阵的行数。



示例:

  offset = 1; 
为i = 1:5
%#生成不同大小的矩阵每个循环
A =个(randi(4),randi(4))。

%#在第1张表格中的Excel中插入矩阵,从单元格开始指定
xlswrite('filename.xls',A,1,sprintf('A%d',offset)) ;

%#increment offset
offset = offset + size(A,1);
end

这将创建一个包含以下内容的Excel文件:

  1 1 
1 1
1 1
2 2 2 2
3 3
3 3
4
4
4
4
5 5 5
5 5 5
5 5 5
5 5 5

请注意,每次XSLWRITE调用时,都会连接到Excel,然后关闭。这有一个很大的开销。



一个更好的方法是打开Excel一次,并重复使用相同的会话来写入所有的数据,然后一旦完成,关闭它。但是,您必须自己调用Office Interop功能。



由于上述提到的技巧现在不起作用,我将使用 计算Excel范围 函数来计算单元格范围(还有许多FEX的其他实现) p>

这是代码(从 previous answer ):

 %#输出文件名
fName = fullfile(pwd,'file.xls');

%#创建Excel COM Server
Excel = actxserver('Excel.Application');
Excel.Visible = true;

%#删除现有文件
如果存在(fName,'file'),delete(fName);结束

%#创建新的XLS文件
wb = Excel.Workbooks.Add();
wb.Sheets.Item(1).Activate();

%#iterations
offset = 0;
为i = 1:50
%#生成不同大小的矩阵每个循环
A =个(randi(4),randi(4))。

%#计算单元格范围以适应矩阵(放置在前一个下方)
cellRange = xlcalcrange('A1',offset,0,size(A,1),size(A,2 ));
offset = offset + size(A,1);

%#在表单
中插入矩阵Excel.Range(cellRange).Select();
Excel.Selection.Value = num2cell(A);
end

%#保存XLS文件
wb.SaveAs(fName,1);
wb.Close(false);

%#关闭Excel
Excel.Quit();
Excel.delete();

事实上,我运行了两个版本的50次迭代,并将时间与TIC / TOC进行了比较: p>

 已用时间为68.965848秒。 %#调用XLSWRITE 
经过的时间是2.221729秒。 %#直接调用Excel COM


I have a 6x7 matrix(A) and some operation I'm doing on it for k=1:50 (number of loops). I know that in order to write a single matrix in an excel file I need:

xlswrite('Filename.xls', A, 'A1')

How is it possible to write the information after each loop, in that way that each loop starts at position A1+((k-1)*6+1) in Excel and I have all results of Matrix(A) listed one after each other.

Thank you!

解决方案

When using the XLSWRITE function, you can avoid having to compute Excel cell ranges each time, by specifying both sheet number and range, where range only gives the first cell to start at.

It is especially useful if the matrix changes size each iteration; we simply take the last cell used and shift it by the number of rows of the matrix.

Example:

offset = 1;
for i=1:5
    %# generate different size matrices each loop
    A = ones(randi(4),randi(4)).*i;

    %# insert matrix in Excel inside the 1st sheet, starting at cell specifed
    xlswrite('filename.xls', A, 1, sprintf('A%d',offset));

    %# increment offset
    offset = offset + size(A,1);
end

this creates an Excel file with the following content:

1    1        
1    1        
1    1        
2    2    2    2
3    3
3    3
4            
4            
4            
4            
5    5    5    
5    5    5    
5    5    5    
5    5    5    

Note that a connection is made to Excel then closed each time XLSWRITE is called. This has a significant overhead.

A better approach would be to open Excel once, and reuse the same session to write all of your data, then close it once we are done. However, you will have to call the Office Interop functions yourself.

Since the trick I mentioned above won't work now, I will be using the "Calculate Excel Range" function to compute the cell ranges (there are many other implementations of FEX).

Here is the code (reusing some code from a previous answer):

%# output file name
fName = fullfile(pwd, 'file.xls');

%# create Excel COM Server
Excel = actxserver('Excel.Application');
Excel.Visible = true;

%# delete existing file
if exist(fName, 'file'), delete(fName); end

%# create new XLS file
wb = Excel.Workbooks.Add();
wb.Sheets.Item(1).Activate();

%# iterations
offset = 0;
for i=1:50
    %# generate different size matrices each loop
    A = ones(randi(4),randi(4)).*i;

    %# calculate cell range to fit matrix (placed below previous one)
    cellRange = xlcalcrange('A1', offset,0, size(A,1),size(A,2));
    offset = offset + size(A,1);

    %# insert matrix in sheet
    Excel.Range(cellRange).Select();
    Excel.Selection.Value = num2cell(A);
end

%# save XLS file
wb.SaveAs(fName,1);
wb.Close(false);

%# close Excel
Excel.Quit();
Excel.delete();

In fact, I ran both version with 50 iterations, and compared timings with TIC/TOC:

Elapsed time is 68.965848 seconds.      %# calling XLSWRITE
Elapsed time is 2.221729 seconds.       %# calling Excel COM directly

这篇关于每个循环后将信息写入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆