Return rows (not just row number)
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello
I would be happy if anyone could help me with following problem.
I have an text (or excel) file which is in the following form (or course in reality it is in larger form but anyway the basic idea is):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153669/image.jpeg)
And now I would like to find all the 'Cat'-rows and return that row and the following row.
So in the end my answer should be
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153670/image.jpeg)
And I don't even know how to begin. Could anyone help?
댓글 수: 0
채택된 답변
Ingrid
2016년 1월 29일
just use strfind or strcmp to find the indices
varNames = var(:,1);
IndexC = strfind(varNames, 'Cat');
Index = find(not(cellfun('isempty', IndexC)));
Index = sort([Index; Index+1]);
extracted = var(Index,:);
댓글 수: 0
추가 답변 (3개)
Walter Roberson
2016년 1월 29일
match = ismember(var(:,1), 'Cat');
extended_match = match | [true; match(1:end-1)]; %row and following row; and also header
extracted = var(extended_match, :);
댓글 수: 0
Snowfall
2016년 1월 29일
편집: Snowfall
2016년 1월 29일
댓글 수: 2
Walter Roberson
2016년 1월 30일
Ingrid's strfind solution will find 'Cat' anywhere in the string. It is, though, case sensitive. If you want a case-insensitive solution then you could use Ingrid's solution modified slightly to
IndexC = strfind(lower(varNames), lower('Cat'));
The function I used, ismember(), does not extend to locating strings within other strings. I would probably modify my solution to
match = ~cellfun(@isempty,regexpi(varNames,'Cat'));
Snowfall
2016년 2월 5일
댓글 수: 2
Ingrid
2016년 2월 5일
it is better if you make a new question for this as this one already has an accepted answer so you are less likely to get an answer but here goes:
varNames = var(:,1);
IndexCat = strfind(varNames, 'Cat');
Index1 = find(not(cellfun('isempty', IndexCat)));
IndexPig = strfind(varNames,'Guinea pig');
Index2= find(not(cellfun('isempty',IndexPig)));
Index =~(Index1|Index2);
Index = sort([Index; Index+1]);
extracted = var(Index,:);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!