Find a substring in table
56 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm struggling with finding the rows of columns that contains a certain substring (e.g. '.jpg') in a table column?
Any suggestions?
Kati
0 个评论
回答(3 个)
Lei Hou
2019-8-27
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.
0 个评论
Lei Hou
2019-8-30
Hi Katharina,
You should cellstr to convert the categorical data to a cell array of character vectors as:
startsWith(cellstr(t.Code),'Response')
You can also call string to convert the categorical data to a string array as:
startsWith(string(t.Code),'Response')
Since R2018b, it is highly recommended to use string instead of cellstr when you work with text because string is more efficient.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!