Search string array column for a specific string

조회 수: 20 (최근 30일)
Scott Duffey
Scott Duffey 2021년 1월 11일
댓글: Scott Duffey 2021년 1월 11일
I have a 1000 x 30 string array. I'm trying to search column 5 for a specific set of strings and output the rows in which they're found.
Example Code:
I've put a sample below. I'm trying to only search column 5 of X for the terms in search and spit out which row they're in. I'm not sure how to go about this. I've tried
while i < size(search(1,:),2)
[row,col] = find(strcmp(X,search((i)));
i = i + 1;
end
but this searches the entire row and not just column 5 for the terms. I also need the rows which include other terms in addition to the search terms like the 1st row.
>> search =
1×4 cell array
{'apple'} {'banana'} {'orange'} {'grapefruit'}
>> X(1:8,1:6)
ans =
8×6 string array
"0" "0" "0" "0" "apple peanut" "0"
"0" "0" "apple" "0" "apple" "0"
"0" "0" "0" "apple" "apple" "0"
"0" "0" "apple" "0" "banana" "0"
"0" "0" "0" "0" "banana" "0"
"0" "0" "banana" "0" "orange pickle" "0"
"0" "0" "pickle" "0" "orange" "0"
"0" "0" "0" "0" "grapefruit" "0"

채택된 답변

Ive J
Ive J 2021년 1월 11일
Have you tried ismember? You only need column 5, but you fed first argument of strcmp with the whole string array.
rowIdx = find(ismember(X(:, 5), search)); % row indices of X which contain any of search elements
  댓글 수: 3
Ive J
Ive J 2021년 1월 11일
If your entire string array follows your example, so you can use contains:
rowIdx = find(contains(X(:, 5), search));
But if the text pattern is more complex, you can use regexp with some efforts.
Scott Duffey
Scott Duffey 2021년 1월 11일
Great, thanks!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 1월 11일
search = {'apple', 'banana', 'orange', 'grapefruit'}
search = 1x4 cell array
{'apple'} {'banana'} {'orange'} {'grapefruit'}
searchcol = 3
searchcol = 3
X = [
"0" "0" "0" "0" "apple" "0"
"0" "0" "apple" "0" "apple" "0"
"0" "0" "0" "apple" "apple" "0"
"0" "0" "apple" "0" "banana" "0"
"0" "0" "0" "0" "banana" "0"
"0" "0" "banana" "0" "orange" "0"
"0" "0" "pickle" "0" "orange" "0"
"0" "0" "0" "0" "grapefruit" "0"
]
X = 8×6 string array
"0" "0" "0" "0" "apple" "0" "0" "0" "apple" "0" "apple" "0" "0" "0" "0" "apple" "apple" "0" "0" "0" "apple" "0" "banana" "0" "0" "0" "0" "0" "banana" "0" "0" "0" "banana" "0" "orange" "0" "0" "0" "pickle" "0" "orange" "0" "0" "0" "0" "0" "grapefruit" "0"
[wasfound, fruitidx] = ismember(X(:,searchcol), search)
wasfound = 8x1 logical array
0 1 0 1 0 1 0 0
fruitidx = 8×1
0 1 0 1 0 2 0 0
matching_rows = find(wasfound)
matching_rows = 3×1
2 4 6
matching_fruit_number = fruitidx(wasfound)
matching_fruit_number = 3×1
1 1 2
  댓글 수: 1
Scott Duffey
Scott Duffey 2021년 1월 11일
Thanks! I realized I needed to be more specific. Some of the cells in column 5 contain both the search term and something else. I need to make sure I get those rows as well.

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

카테고리

Help CenterFile Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by