Matlab中使用Excel数据

 Problem Description
I am trying to control Excel from MATLAB using ActiveX. Are there any examples that show how to use the ActiveX automation interface from Excel to do this?

Solution:
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 tp 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 do 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操作对象
Excel = actxserver('Excel.Application');
%使Excel对象可见,即打开Excel窗口,实际应用时设置其为不可见的
set(Excel, 'Visible', 1);

% Insert a new workbook
%创建工作本组对象
Workbooks = Excel.Workbooks;
%添加一个工作本
Workbook = invoke(Workbooks, 'Add');

% Make the second sheet active
%获取当前活跃工作本的表单组,一个工作本共有3个表单(sheets)
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];
%设置写到Excel中的范围
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现在是符合矩阵,将其转换成数值矩阵
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
%退出Excel
invoke(Excel, 'Quit');

% End process
%结束Excel进程
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, please refer to the following URL:

http://www.mathworks.com/support/solutions/data/27338.shtml

For an example that shows how to connect MATLAB with Excel using DDE, please refer to the following URL:

http://www.mathworks.com/support/solutions/data/31072.shtml

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值