How to search a table for a string

912 Ansichten (letzte 30 Tage)
Jonathan
Jonathan am 12 Mär. 2020
Bearbeitet: dpb am 10 Jan. 2023
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 Kommentar
dpb
dpb am 12 Mär. 2020
  1. See strfind, contains and friends,
  2. intersect

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Abhisek Pradhan
Abhisek Pradhan am 16 Mär. 2020
Searching a table for a string can done using find and strcmp. A combination of both function can be used to search a string:
find(strcmp('YourString',Table));
Refer the following link for model details.
  4 Kommentare
Samad
Samad am 10 Jan. 2023
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)
tab = 2×3 table
name age height ______ ___ ______ "sam" 20 4 "jack" 27 5
extract the name sam
idx=strcmp("sam",tab)
idx = logical
0
dpb
dpb am 10 Jan. 2023
Bearbeitet: dpb am 10 Jan. 2023
" 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'),:)
ans = 1×3 table
name age height _____ ___ ______ "sam" 20 4
Or, with new string variable functions,
tab(matches(tab.name,'sam'),:)
ans = 1×3 table
name age height _____ ___ ______ "sam" 20 4

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by