Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档 > MATLAB技术文章
MATLAB技术文章 MATLAB Technical Articles From Mathworks
回复
 
主题工具 显示模式
旧 2008-01-06, 16:32   #1
TechnicalArticles
游客
 
帖子: n/a
默认 Using ActiveX to Connect MATLAB to Excel (MATLAB Digest)

Technical Solutions

Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?


Date Last Modified: 20 Jun 2007 Solution ID: 1-17PWCProduct: MATLAB®Reported in Release: R11Fixed in Release: R14SP3Platform: WindowsOperating System: Windows Any

Subject:

Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?
Problem Description:

I am trying to control Excel from MATLAB using ActiveX. I would like some examples that show how to use the ActiveX automation interface from Excel to do this.
Solution:

This enhancement has been incorporated into the documentation for Release 14 Service Pack 3 (R14SP3). You can find an example in the "External Interfaces" manual in the chapter "COM and DDE Support (Windows Only)" under "Using MATLAB as an ActiveX Client" or on the web:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bqdwu3j.html#bqd_o32

For previous product releases, please see the example below:

Most of the functionality that you get from ActiveX is dependent on the object model, which the external application implements. Consequently, we are usually unable to provide much information about the functions that you need to use in the remote application to perform a particular function. We do, however, have an example that shows how to perform common functions in Excel.

We also recommend that you become more familiar with the Excel object model in order to better use Excel's ActiveX automation interface from MATLAB. You can find more information on this interface by selecting the "Microsoft Excel Visual Basic Reference" topic in the Microsoft Excel Help Topic dialog. This topic area contains a searchable description of Excel methods and properties.

The following example demonstrates how to insert MATLAB data into Excel. It also shows how to extract some data from Excel into MATLAB. For more information, refer to the individual comments for each code segment.

% Open Excel, add workbook, change active worksheet,
% get/put array, save, and close

% First open an Excel Server
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);

% Insert a new workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');

% Make the second sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet2 = get(Sheets, 'Item', 2);
invoke(sheet2, 'Activate');

% Get a handle to the active sheet
Activesheet = Excel.Activesheet;

% Put a MATLAB array into Excel
A = [1 2; 3 4];
ActivesheetRange = get(Activesheet,'Range','A1:B2');
set(ActivesheetRange, 'Value', A);

% Get back a range. It will be a cell array,
% since the cell range can
% contain different types of data.
Range = get(Activesheet, 'Range', 'A1:B2');
B = Range.value;

% Convert to a double matrix. The cell array must contain only scalars.
B = reshape([B{:}], size(B));

% Now save the workbook
invoke(Workbook, 'SaveAs', 'myfile.xls');

% To avoid saving the workbook and being prompted to do so,
% uncomment the following code.
% Workbook.Saved = 1;
% invoke(Workbook, 'Close');

% Quit Excel
invoke(Excel, 'Quit');

% End process
delete(Excel);

There are several options for connecting MATLAB with Excel. For an example that shows how to connect MATLAB with Excel using Excel Link, refer to the "Related Solutions" below:

For an example that shows how to connect MATLAB with Excel using DDE, refer to the "Related Solutions" below:

For information on how to use the XLSREAD function to read .xls files, please refer to the following URL:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.shtml



更多...
  回复时引用此帖
回复

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 05:14


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.