How to compare an excel cell with a particular data and find the index

7 次查看(过去 30 天)
Hi can someone please help me!
i have an excel file containing multiple rows and columns .i want to go through a particular column and check row after row if any element of that column matches with my Data2.
Data2 here is a cell containing names eg Data2 = {'BAR23','LY46','BER35',Aal23'','JO234'}. and if Data2 matches to any row of my specified column ,i would like to know the index of the row iand read the elements of that row in to matlab
thanks in advance
line_start=1;
line_stop=50;
names='B';
sheet = 1;
range_names=[column_profile_names num2str(line_start) ':' column_profile_names num2str(line_stop)];
file ='C:\Users\MIX\Documents\Example.xlsx';
[~,profile_names,raw]=xlsread(file,sheet,range_names,'basic');
Data2 = {'BAR23','LY46','BER35',Aal23'','JO234'}
for i=1:length(profile_names)
index = find( strcmp(profile_names,Data2));
end
this is what i have till yet
i always get the error ! indice exceeds matrix dimension

采纳的回答

Vineet Joshi
Vineet Joshi 2021-3-23
As per my understanding of your question, you want to find indices of rows in a particular column with data from the Data2 cell.
You can use the ismember function.
You can refer the following sample excel file & sample code for reference.
%Load the file.
file ='Example.xlsx';
[~,TestCols,~]=xlsread(file);
%Given Cell.
Data2 = {'BAR23','LY46','BER35','Aal23','JO234'};
%Check In Column 4.
ismember(TestCols(:,4),Data2)
%Output
ans =
10×1 logical array
0
1
0
1
1
0
1
0
0
0
%Check in Column 5
ismember(TestCols(:,5),Data2)
%Output
ans =
10×1 logical array
0
1
0
0
1
1
0
0
1
1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by