Clear Filters
Clear Filters

Removing rows from table based on content

6 views (last 30 days)
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.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!