deleting all rows from a table that contain a string

20 ビュー (過去 30 日間)
Eth
Eth 2019 年 5 月 16 日
コメント済み: Eth 2019 年 5 月 16 日
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')), :);

採用された回答

madhan ravi
madhan ravi 2019 年 5 月 16 日
T(~all(strcmp(T{:,:},'empty'),2),:)
  4 件のコメント
madhan ravi
madhan ravi 2019 年 5 月 16 日
Use any() instead of all()
Eth
Eth 2019 年 5 月 16 日
Thanks! worked perfectly.

サインインしてコメントする。

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2019 年 5 月 16 日
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 件のコメント
Eth
Eth 2019 年 5 月 16 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by