How do I delete rows in a table if they contain a specific string?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2014-4-11
I fixed your formatting, but as thanks I want you to read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
回答(2 个)
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
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
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 个评论
另请参阅
类别
在 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!