Find elements in table without looping (Matlab)

673 views (last 30 days)
As this seems to be a quite common problem, I have spent a while trying to find a suitable answer for my case in other posts. However, most of them are full of maths and I cannot see the solution very clear.
This is my case:
I generated a function that imports data into Matlab in form of a table. The table looks like:
This table has around one million of rows. Basically, I need to extract from this table just the rows where the table1.ID matches 'DHK' and leave the result in another table.
This is the code I have just now:
for p=1:length(table1.ID)
if strcmp(IDstr,'DHK')==1
This code works but it takes extremely long time as I am dealing with a million of rows and many import files. There migth be a better way or command that automatically finds the information that I need instead of looking for it line by line?. I found this MATLAB link on the documentation but I am not sure how to apply it for what I need:
I would be very grateful if somebody could help me.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 5 Jun 2015
Edited: Azzi Abdelmalek on 5 Jun 2015
v={'ID' 'var1' 'var2' ;'DHK' 12 14; 'ABC',15 20;'DHK',15 13}
w=cell2table(v(2:end,:),'variablenames',v(1,:)) % Example
Francesco Giuseppe Fornari
what if I wanted to find 'DHK' without knowing in which columns it can be?
I've got multiple csv files, where I need to find the same string.

Sign in to comment.

More Answers (2)

Gabor on 9 Mar 2019
rows ='DHK';
T(rows, :)

Peter Perkins
Peter Perkins on 5 Jun 2015
First convert those highly repetitive strings to a categorical variable:
table1.Var1 = categorical(table1.Var1);
Type whos before and after doing that and watch your memory use decrease. Then do more or less what Azzi suggests, leveraging the categorical equality operator:
table2 = table1(table1.ID == 'DHK',:);
That's it!


Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!