deleting all rows from a table that contain a string

I have the following table T:
LastName Age Smoker Height Weight BloodPressure
_________ ___ ______ ______ ______ _____________
empty empty empty empty empty empty
'Sanchez' 38 true 71 176 124 93
empty empty empty empty empty empty
'Johnson' 43 false 69 163 109 77
empty empty empty empty empty empty
'Li' 38 true 64 131 125 83
empty empty empty empty empty empty
'Diaz' 40 false 67 133 117 75
empty empty empty empty empty empty
'Brown' 49 true 64 119 122 80
empty empty empty empty empty empty
I want to remove all the rows that contain the string 'empty'. I've searched but the examples I found was to search on a particular column but I want to search on all columns.
T(cellfun(@isempty, strfind(T.columnName, 'empty')), :);

 Accepted Answer

T(~all(strcmp(T{:,:},'empty'),2),:)

4 Comments

Is there a way to delete the entire row if one instance of the string 'empty' is found? For example:
LastName Age Smoker Height Weight BloodPressure
_________ ___ ______ ______ ______ _____________
empty empty empty empty empty empty
'Sanchez' 38 true 71 176 124 93
one two three four empty rose
'Johnson' 43 false 69 163 109 77
empty empty empty empty empty empty
'Diaz' 40 false 67 133 117 75
Output (the top row deleted but also the 3rd row where the Weight column has the string 'empty':
LastName Age Smoker Height Weight BloodPressure
_________ ___ ______ ______ ______ _____________
'Sanchez' 38 true 71 176 124 93
'Johnson' 43 false 69 163 109 77
'Diaz' 40 false 67 133 117 75
Use any() instead of all()
Thanks! worked perfectly.

Sign in to comment.

More Answers (1)

Eth, I think the more important question is how you got yourself into this corner to begin with. You say you have a table, But it looks more like you have a cell array. Probably you have a table all of whose variables are cell columns. Given that you have both text, logical, and numeric data, that's not a good position to be in. However you got there has made things much harder than they need to be.
This is the table you want:
>> t
t =
11×6 table
LastName Age Smoker Height Weight BloodPressure
_________ ___ ___________ ______ ______ _____________
<missing> NaN <undefined> NaN NaN NaN NaN
"Sanchez" 38 true 71 176 124 93
<missing> NaN <undefined> NaN NaN NaN NaN
"Johnson" 43 false 69 163 109 77
<missing> NaN <undefined> NaN NaN NaN NaN
"Li" 38 true 64 131 125 83
<missing> NaN <undefined> NaN NaN NaN NaN
"Diaz" 40 false 67 133 117 75
<missing> NaN <undefined> NaN NaN NaN NaN
"Brown" 49 true 64 119 122 80
<missing> NaN <undefined> NaN NaN NaN NaN
At that point, rmmissing or fillmissing are simple one-liners to clean up your data.

1 Comment

Hi Peter,
I'm pretty new to MATLAB programming as you can tell. I've created an app in appdesigner with a table (8 columns by n rows) and I'm reading a formatted text file into the table but I need the ability to modify the rows that were read from the text file and also the ability to add new rows (and these new rows need to be editable) before and after the rows that are already there and also delete any unwanted row and once this is all done I need to overewrite the modified text file. I have it working now but it's probably not the best approach.

Sign in to comment.

Categories

Asked:

Eth
on 16 May 2019

Commented:

Eth
on 16 May 2019

Community Treasure Hunt

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

Start Hunting!