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

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Jarrod Rivituso
Jarrod Rivituso il 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 Commenti
Altibano O.
Altibano O. il 5 Mag 2011
Thank you so much Jarrod!!
The file works fine!! I think that has only a probelm: The matrix saved is just that I would to discharge ... So, must I change the variable to keep, right? But no have other variable with the values greater than 108! The matlab have created only the matrix with all indesiderable values ...
Altibano O.
Altibano O. il 11 Mag 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 ...

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by