How can I extract the all the rows that contain a certain string from a cell array?

10 views (last 30 days)
I am a MATLAB novice and stumped by a seemingly easy thing: I am analysing frequency reserve and unfortunately my Excel data for a certain amount of time looks like this:
frequency_type_a 190 35 5
frequency_type_a 50 55 6
frequency_type_a 60 200 10
frequency_type_b 170 89 6
frequency_type_b 83 35 15
frequency_type_c 200 64 17
frequency_type_c 89 32 15
So the problem is that all the different types of reserve are in the file - however I just want to analyse one type. I could use a import wizard, however I will be needing to do this for +100 files and don't want to click myself through the import manager every time! I have tried this so far:
[num text raw] = xlsread('ERGEBNISLISTE_ANONYM_SRL_2014-06-02.CSV');
typeb(raw(:,2)=='frequency_type_b')
The error message says: ??? Undefined function or method 'eq' for input arguments of type 'cell'. So I'm guessing that only works for numbers in Matrices. I have also looked at cat, match and writing a if-function, but none of them seemed doable.Is there a way to solve my problem? Maybe even a different approach, like only loading a specific type in the first place?
Any help is much appreciated!

Accepted Answer

James Tursa
James Tursa on 8 Jul 2015
Edited: James Tursa on 8 Jul 2015
Don't use == (the eq function) to compare strings since this is an element-by-element operator. Use a function meant for this purpose instead. E.g.,
strcmp(raw(:,2),'frequency_type_b')
This result will be a logical vector that you can use to index into and extract your desired data from other matrices.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!