[转帖]Matlab 如何读取 Excel 表格数据
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 = ; <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(, 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
页:
[1]