How to extract a specific position of excel cell in an iterating matlab file

조회 수: 7 (최근 30일)
Ace_ventura
Ace_ventura 2015년 3월 8일
답변: Guillaume 2015년 3월 8일
Guys, I have a matlab file that takes data from an excel file and does the analysis. My excel file has 65 rows and 16 columns. My rows denote some parameters that I wish to get and my columns have headers which denote input to the code(such as 'area' ,'z value' , 'r value' etc).
In my code the input is area and Z value and it gives me the parameter corresponding to my row, while the values in other headers are used to check certain conditions. When I run analysis,I get some value of area and z value.I want to pick the r value in my input corresponding to the area and z value without opening excel file again and again as it goes for many iterations. How do I locate the exact position of cell containing that r value?I hope my question is clear.If not please let me know.
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2015년 3월 8일
Chriss - if your Excel file has only 65 rows and 16 columns, then why not read (load) all of the data from the one time before you start iterating? That way you have all of the data and then you can start doing your iterations, referring to the loaded data instead of opening the Excel file on each iteration.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2015년 3월 8일
You will have to use matlab's interface to COM objects (which xlsread uses under the hood). The following code is an example of reading a range in an excel file:
excelapp = actxserver('Excel.Application'); %connect to excel
workbook = excelapp.Workbooks.Open('somefile.xlsx'); %open workbook, specify the full path
worksheet = workboox.Sheets.Item('Sheet1'); %get a worksheet by name
%worksheet = workboox.Sheets.Item(1); %or by number
rangecontent = worksheet.Range('A1:C5').Value; %get content of a range
%access more worksheets, ranges, etc.
workbook.Close; %close workbook when done
excelapp.Quit; %end excel
delete(excelapp); %and disconnect.
Adapt as required. Also have a look at Microsoft documentation of COM interface https://msdn.microsoft.com/en-us/library/Microsoft.Office.Interop.Excel.aspx, particularly, the Worksheet interface and the Range interface.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by