Hello .I have the following question :I am reading a text file using the usual
textread(filename,'%s%s%s%f%f%d','delimiter',',').Sometimes though the file is corrupted and instead of a number as expected ,in one of the fields ,there is an INF .
How do I change the program so I can simply avoid these lines completely ?
Many thanks,
Constantin

3 Kommentare

madhan ravi
madhan ravi am 23 Jan. 2019
Bearbeitet: madhan ravi am 23 Jan. 2019
upload your file and which version of matlab are you using?
Constantin milea
Constantin milea am 23 Jan. 2019
Cannot ,is a proprietary one but a line looks like this basically :
EQ235464,Mining,Active,SPY,23.4,34,5,INF,230
madhan ravi
madhan ravi am 23 Jan. 2019
try reading the file using readtable()

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephan
Stephan am 23 Jan. 2019
Bearbeitet: Stephan am 23 Jan. 2019

1 Stimme

Hi,
use readtable like suggested by Madhan. If it is always in column 8 of your data like your example given use logical indexing:
filename = 'filename.txt';
result = readtable(filename)
result(result.Var8==Inf,:) = []
using this example as file, where line 5 and 9 are corrupt:
EQ235464,Mining,Active,SPY,23.4,34,5,1,230
EQ235464,Mining,Active,SPY,23.4,34,5,2,230
EQ235464,Mining,Active,SPY,23.4,34,5,3,230
EQ235464,Mining,Active,SPY,23.4,34,5,4,230
EQ235464,Mining,Active,SPY,23.4,34,5,INF,230
EQ235464,Mining,Active,SPY,23.4,34,5,6,230
EQ235464,Mining,Active,SPY,23.4,34,5,7,230
EQ235464,Mining,Active,SPY,23.4,34,5,8,230
EQ235464,Mining,Active,SPY,23.4,34,5,INF,230
EQ235464,Mining,Active,SPY,23.4,34,5,10,230
results in:
result =
10×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
__________ ________ ________ _____ ____ ____ ____ ____ ____
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 1 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 2 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 3 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 4 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 Inf 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 6 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 7 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 8 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 Inf 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 10 230
result =
8×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
__________ ________ ________ _____ ____ ____ ____ ____ ____
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 1 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 2 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 3 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 4 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 6 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 7 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 8 230
'EQ235464' 'Mining' 'Active' 'SPY' 23.4 34 5 10 230
Best regards
Stephan

1 Kommentar

Constantin milea
Constantin milea am 23 Jan. 2019
Many thanks for both answers ,yes is working great

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by