Matlab 如何读取 Excel 表格数据

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

« 上一篇: 在VC++程序中输出空心文字下一篇: My ANN program »

Matlab 如何读取 Excel 表格数据

Cyberr @ 2005-03-31 12:44

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. 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 = 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, please refer

相关文档
最新文档