read a file .txt with 'm' rows and 5 columns and ....

1 view (last 30 days)
Hello, I must open a .txt file and to manipulate the rows to delete the rows that not corresponding to some arguments. The file is like this:
18,2554 0 0 138 9,365
18,0262 104 0 0 10,562
19,3685 0 0 108 11,011
19,4065 108 206 0 11,451
So, after read the program must to compare the 2nd, 3rd and 4th columns to know if they have values =< 108 (integer) and at the same time zeroes in the other two columns. According to the example above, the rows 2 and 3 must be deleted. Any help???? Thanks in advance.

Accepted Answer

Jarrod Rivituso
Jarrod Rivituso on 30 Apr 2011
Ok i was curious...
data = dlmread('myFile.txt','\t') %assuming tab delimited
colsOfInterest = data(:,2:4)
numOfZeros = sum((colsOfInterest == 0),2)
totalRowSum = sum(colsOfInterest,2)
rows2keep = (totalRowSum <= 108) & (numOfZeros == 2)
newData = data(rows2keep,:)
  2 Comments
Altibano O.
Altibano O. on 11 May 2011
Found it! I changed the conditionals in the line rows2keep ... from <=108 to >108 ...
Now I'll use the values to plot other variables using a similar code as you wrote ...

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!