Return rows (not just row number)

3 views (last 30 days)
Snowfall
Snowfall on 29 Jan 2016
Edited: Snowfall on 7 Feb 2016
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):
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
And I don't even know how to begin. Could anyone help?

Accepted Answer

Ingrid
Ingrid on 29 Jan 2016
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,:);

More Answers (3)

Walter Roberson
Walter Roberson on 29 Jan 2016
match = ismember(var(:,1), 'Cat');
extended_match = match | [true; match(1:end-1)]; %row and following row; and also header
extracted = var(extended_match, :);

Snowfall
Snowfall on 29 Jan 2016
Edited: Snowfall on 29 Jan 2016
Thank you, these help me a lot :)
But it also raised a new question... What should I do if i want to find a word which contains word 'Cat'. For example in that table (above) I would have words 'TigerCat' and 'LionCat' instead of just 'Cat' (like in previous question) and I would like to return all rows which contains 'Cat'?
Is Matlab capable of doing that?
  2 Comments
Snowfall
Snowfall on 3 Feb 2016
Thank you for your answer. I begin to try these in practice.

Sign in to comment.


Snowfall
Snowfall on 5 Feb 2016
How about a case where I would like to find all words which do not include 'Cat' or 'Guinea pig'?
  2 Comments
Snowfall
Snowfall on 7 Feb 2016
Ok, thank you for your answer. Next time I'll write a new question :)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!