Removing rows from table based on content
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I got some data with "false" values that I need to remove
The data looks as following...
I need to remove all rows that include the letter "x". The fourth column sometimes consists only of the letter "x", sometimes it consists of "x" and other values as seen on the picture.
I am loading the data usinf following code
filename = 'Sample.txt';
fileID = fopen(filename);
opts = detectImportOptions(filename);
opts.VariableTypes{1} = 'string';
opts.VariableTypes{4} = 'string';
M = readtable(filename,opts);
fclose(fileID);
All help is welcomed and appreciated.
0 件のコメント
採用された回答
KALYAN ACHARJYA
2021 年 2 月 7 日
編集済み: KALYAN ACHARJYA
2021 年 2 月 7 日
If you are wish to check the single string with x only, as in the rows 6 and 7, then you can accomplish it easily. In that case it avoid the removal of row 5, where x is also there within the string ( with Gap consideation).
id=find(strcmp('x',M.Var4));
M(id,:) = []
Result:
M =
10×4 table
Var1 Var2 Var3 Var4
_______________ ____ __________ ______________________________
"1598607723000" 4 6.5162e+14 "9049F00430000764B1BD4046DCEA"
"1598607723000" 12 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 14 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 16 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 10 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 12 6.5162e+14 "x 5D4BCCB982A069"
"1598607723000" 14 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
"1598607723000" 12 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
"1598607723000" 4 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
"1598607723000" 3 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
But If you wish to remove the 5 rows also, within the strings x is there, not single 'x' string, in that case I have used loop, may be it can be avoided.
for i=1:length(M.Var4)
id(i)=max(strcmp('x',split(M.Var4(i))))
end
M(id,:) = []
Result:
M =
9×4 table
Var1 Var2 Var3 Var4
_______________ ____ __________ ______________________________
"1598607723000" 4 6.5162e+14 "9049F00430000764B1BD4046DCEA"
"1598607723000" 12 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 14 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 16 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 10 6.5162e+14 "8D06A10E99158B12D00427C73A6C"
"1598607723000" 14 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
"1598607723000" 12 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
"1598607723000" 4 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
"1598607723000" 3 6.5162e+14 "8D48C12B5811F16DD1085E938F4F"
2 件のコメント
KALYAN ACHARJYA
2021 年 2 月 7 日
Later (more free time), I will update the 2nd one without loop, if any.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!