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

7 views (last 30 days)
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 Comment
Geoff Hayes
Geoff Hayes on 8 Mar 2015
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.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 8 Mar 2015
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.

Community Treasure Hunt

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

Start Hunting!