Find a substring in table

102 views (last 30 days)
katharina voigt
katharina voigt on 26 Aug 2019
Answered: Lei Hou on 30 Aug 2019
I'm struggling with finding the rows of columns that contains a certain substring (e.g. '.jpg') in a table column?
Any suggestions?

Answers (3)

Lei Hou
Lei Hou on 27 Aug 2019
Hi Katharina,
You can make use of table indexing to get that data you want. I’ll use a simple example below to illustrate the output.
Suppose the table is like the following:
Var1 Var2 Var3
___________ ____ ____________
"file1.png" 1 "Image1.jpg"
"file2.jpg" 2 "Image2.jpg"
"file3.gif" 3 "Image3.gif"
"file4.jpg" 4 "Image4.png"
If you want to check which rows of column ‘Var1’ end with ‘.jpg’ and return those rows with all columns, here is the solution.
>> t1 = t(endsWith(t.Var1,'.jpg'),:);
The output 't1' is like the following
Var1 Var2 Var3
___________ ____ ____________
"file2.jpg" 2 "Image2.jpg"
"file4.jpg" 4 "Image4.png"
I’m not sure whether I understand your question correctly. If not, please provide more information about your use case or illustrate your expected output with my simple example.

katharina voigt
katharina voigt on 28 Aug 2019
Edited: katharina voigt on 28 Aug 2019
Thanks Lei for your response.
My question has changed a bit, but in essence it is the same.
I would like to have the row number, when t.Code == 'Response:' (the digits behind response can change and that is why i would like to have it more flexible.
So at the moment I tried this
However, I get the following error:
Error using contains
Search term must be a string array, character vector, or cell array of character vectors.
Following this i tried
But it said that the conersion to cell from categorical is not possible.
Please find table (t) attached.
Thank you very much in advance.

Lei Hou
Lei Hou on 30 Aug 2019
Hi Katharina,
You should cellstr to convert the categorical data to a cell array of character vectors as:
You can also call string to convert the categorical data to a string array as:
Since R2018b, it is highly recommended to use string instead of cellstr when you work with text because string is more efficient.


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!