How to search a table for a string
古いコメントを表示
Hi, I have a table of data. One of these columns includes a column which I have labelled 'Participant Data'
I want to search 'Participant Data' for a particular string and return all the index of this table when it does pop up.
Additionally if I have 2 arrays of indices, how to return just the numbers that are in both indices?
Thanks!
1 件のコメント
dpb
2020 年 3 月 12 日
- See strfind, contains and friends,
- intersect
回答 (1 件)
Abhisek Pradhan
2020 年 3 月 16 日
4 件のコメント
Lauren Dana
2022 年 9 月 19 日
Oddly enough I'm returning no results from that, even when I know that the table contains the string I'm searching for.
dpb
2022 年 9 月 19 日
Well, we can't debug what we can't see...attach a sample data file with the subject data and the code that reproduces the problem.
At least one possible cause can be that strfind is case-sensitive so has to match case as well.
Alternatively, we have no idea what "table" means in the context in which you're using it -- a Matlab table is a whole different animal and strcmp() doesn't work on the table itself; it'll needs be the content of a variable/column within the table -- and depending on that content.
strcmp does not work with table, it works with cell. First we have to convert table into cell.
Run the following code
name=["sam"; "jack"];
age=[20'; 27]; height=[4; 5];
tab=table(name, age, height)
extract the name sam
idx=strcmp("sam",tab)
" First we have to convert table into cell...."
No, not really. You do have to reference the variable of interest inside the table, though, yes...
Try the following instead...
name=["sam"; "jack"];
age=[20'; 27]; height=[4; 5];
tab=table(name, age, height);
tab(strcmp(tab.name,'sam'),:)
Or, with new string variable functions,
tab(matches(tab.name,'sam'),:)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!