MATLAB: Parsing a text file

parsingtext file

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

Best Answer

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