Removing rows from table based on content

6 次查看(过去 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.

采纳的回答

KALYAN ACHARJYA
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
KALYAN ACHARJYA 2021-2-7
Later (more free time), I will update the 2nd one without loop, if any.
Jakub Steiner
Jakub Steiner 2021-2-7
Thanks a lot sir, you made my day much better.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by