deleting all rows from a table that contain a string

11 visualizaciones (últimos 30 días)
Eth
Eth el 16 de Mayo de 2019
Comentada: Eth el 16 de Mayo de 2019
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')), :);

Respuesta aceptada

madhan ravi
madhan ravi el 16 de Mayo de 2019
T(~all(strcmp(T{:,:},'empty'),2),:)
  4 comentarios
madhan ravi
madhan ravi el 16 de Mayo de 2019
Use any() instead of all()
Eth
Eth el 16 de Mayo de 2019
Thanks! worked perfectly.

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 16 de Mayo de 2019
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 comentario
Eth
Eth el 16 de Mayo de 2019
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by