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 Comments

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

Sign in to comment.

 Accepted Answer

Stephan
Stephan on 23 Jan 2019
Edited: Stephan on 23 Jan 2019

1 vote

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 Comment

Many thanks for both answers ,yes is working great

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!