Main Content
Connect to Existing Excel Application
This example shows how to read data from an
open file, weekly_log.xlsx
, in MATLAB®.
MATLAB can access a file that is open by another application by creating a COM server from the MATLAB client, and then opening the file through this server.
Navigate to a folder containing an Excel® file, for
example, weekly_log.xlsx
. Open the file in the Excel program.
Open the same file in MATLAB.
excelapp = actxserver('Excel.Application'); wkbk = excelapp.Workbooks; wdata = Open(wkbk,'c:\work\weekly_log.xlsx');
Read data in the range D1
and F6
from
sheet 2.
sheets = wdata.Sheets; sheet12 = Item(sheets,2); range = get(sheet12,'Range','D1','F6'); range.value
ans = 'Temp.' 'Heat Index' 'Wind Chill' [78.4200] [ 32] [ 37] [69.7300] [ 27] [ 30] [77.6500] [ 17] [ 16] [74.2500] [ -5] [ 0] [68.1900] [ 22] [ 35]
Close(wkbk) Quit(excelapp)