Search Excel spreadsheet for specific value

조회 수: 12 (최근 30일)
Amanda Boone
Amanda Boone 2018년 10월 19일
답변: Bereketab Gulai 2020년 5월 27일
Hi, I'm looking to create a program that will search through an excel spreadsheet in the first column to find a specific value. Once that value is found, import the entire row, and break down each item in that row to assign it to a variable. For example:
If I excel file looks like this:
375 10 6745 6.898
380 11 6890 7.001
390 12 7001 7.102
and T=380, I need Matlab to search for the row where the first column is equal to T. Once the row is imported, assign P=11, h=6890, and s=7.001
I'm just not sure how to do this with Matlab. I've looked around and found xlsread('filename') to actually read the spreadsheet, but I don't know how to pull the row and break it apart. Please, any help would be appreciated.

채택된 답변

YT
YT 2018년 10월 19일
Something like this?
%read your sheet
X = xlsread('my-excel-sheet.xlsx');
%the value your looking for
T = 380;
%get entire row where first column value = T
V = X(X(:,1) == T,:);
%your other values assigned here
P = V(2);
h = V(3);
s = V(4);
  댓글 수: 3
Kyuri Kim
Kyuri Kim 2020년 5월 20일
Hi, may I ask you something?
I'm keep making errors in
V = X(X(:,1) == T,:);
It says "The index at position 2 exceeds the array boundary."
Thank you for your help :)
Walter Roberson
Walter Roberson 2020년 5월 20일
Your X has 0 columns so X(:,1) does not exist.
The array that is denoted as [] has 0 rows and 0 columns so if you initialized to [] then it is still []

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

추가 답변 (1개)

Bereketab Gulai
Bereketab Gulai 2020년 5월 27일
Here is answer using actxserver:
excelApp = actxserver("excel.Application");
excelApp.Visible = false;
book1 = excelApp.Workbooks.Open('D:\Temporary stuff\Book1.xlsx');
% Sheet item 1 ...
sheetOne = book1.Sheets.Item(1);
foundInterfaceObj = sheetOne.Range("A:A").Find('Eric');
The returned Interface provides lots functions you may need. Row will give the row number, Value for the cell...

카테고리

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

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by