How do I delete rows in a table if they contain a specific string?

조회 수: 5 (최근 30일)
Abigail
Abigail 2014년 4월 11일
편집: Ken Atwell 2014년 4월 20일
I have a table (RawGaze) containing variable MediaName, and I want to delete rows that contain a string "scr". Here is an example of the contents of RawGaze.MediaName:
'03_f_calm_open_scr_f.jpg'
'03_f_calm_open_scr_f.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'21_m_happy_open.jpg'
'05_f_calm_open_scr_c.jpg'
'05_f_calm_open.jpg'
How do I select only those rows that contain the "scr" string and delete them? Thanks! I'm VERY new to MATLAB, and really struggling with this!

답변 (2개)

Ken Atwell
Ken Atwell 2014년 4월 12일
편집: Ken Atwell 2014년 4월 20일
Starting with Azzi's answer, here is a version that work with a table. I also swapped out regexp in favor of strfind, simply because strfind is sufficient in this case and is easier for newcomers to understand.
%%Create a table 'RawGaze' with a row called 'MediaName'
str={'03_f_calm_open_scr_f.jpg'
'03_f_calm_open_scr_f.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'05_f_calm_open.jpg'};
RawGaze = table(str, 'VariableNames', {'MediaName'});
%%Remove rows containing occurrences of 'scr' in 'MediaName'
RawGaze(cellfun(@isempty, strfind(RawGaze.MediaName, 'scr')), :);
  댓글 수: 1
Image Analyst
Image Analyst 2014년 4월 12일
Nice Ken. I love the new table data type you introduced in R2013b. It's very useful and intuitive. I plan on using it a lot.

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 11일
편집: Azzi Abdelmalek 2014년 4월 11일
If str is your cell array
str={'03_f_calm_open_scr_f.jpg'
'03_f_calm_open_scr_f.jpg'
'03_f_fear_open.jpg'
'03_f_fear_open.jpg'
'05_f_calm_open.jpg'}
str(~cellfun(@isempty,regexp(str,'scr','match')))=[]
  댓글 수: 2
Abigail
Abigail 2014년 4월 11일
Thanks for the response. Sorry if I'm being dense, but when I run this, it deletes the 'scr' strings from the cell array str and not the table. Any way to delete from the table as well?
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 11일
No, this deletes all the rows containing scr

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by