声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3278|回复: 0

[混合编程] [转帖]Matlab 如何读取 Excel 表格数据

[复制链接]
发表于 2005-10-18 17:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
Subject: <BR>Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel? <BR><BR><BR>Problem Description <BR>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? <BR><BR>Solution: <BR>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. <BR><BR>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. <BR><BR>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. <BR><BR><BR><BR><BR>% Open Excel, add workbook, change active worksheet, <BR>% get/put array, save, and close <BR><BR>% First open an Excel Server <BR>Excel = actxserver('Excel.Application'); <BR>set(Excel, 'Visible', 1); <BR><BR>% Insert a new workbook <BR>Workbooks = Excel.Workbooks; <BR>Workbook = invoke(Workbooks, 'Add'); <BR><BR>% Make the second sheet active <BR>Sheets = Excel.ActiveWorkBook.Sheets; <BR>sheet2 = get(Sheets, 'Item', 2); <BR>invoke(sheet2, 'Activate'); <BR><BR>% Get a handle to the active sheet <BR>Activesheet = Excel.Activesheet; <BR><BR>% Put a MATLAB array into Excel <BR>A = [1 2; 3 4]; <BR>ActivesheetRange = get(Activesheet,'Range','A1:B2'); <BR>set(ActivesheetRange, 'Value', A); <BR><BR>% Get back a range. It will be a cell array, <BR>% since the cell range can <BR>% contain different types of data. <BR>Range = get(Activesheet, 'Range', 'A1:B2'); <BR>B = Range.value; <BR><BR>% Convert to a double matrix. The cell array must contain only scalars. <BR>B = reshape([B{:}], size(B)); <BR><BR>% Now save the workbook <BR>invoke(Workbook, 'SaveAs', 'myfile.xls'); <BR><BR>% To avoid saving the workbook and being prompted to do so, <BR>% uncomment the following code. <BR>% Workbook.Saved = 1; <BR>% invoke(Workbook, 'Close'); <BR><BR>% Quit Excel <BR>invoke(Excel, 'Quit'); <BR><BR>% End process <BR>delete(Excel); <BR><BR><BR><BR><BR>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: <BR><BR>http://www.mathworks.com/support/solutions/data/27338.shtml <BR><BR>For an example that shows how to connect MATLAB with Excel using DDE, please refer to the following URL: <BR><BR>http://www.mathworks.com/support/solutions/data/31072.shtml <BR><BR>For information on how to use the XLSREAD function to read .xls files, please refer to the following URL: <BR><BR>http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.shtml
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-14 10:02 , Processed in 0.059192 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表