textread function integer error

1 view (last 30 days)
Ryan Begley
Ryan Begley on 3 Apr 2020
Answered: Walter Roberson on 3 Apr 2020
I am trying to read this text file
Danae Ruiz 26 13.7 4.0 2.3 22 G 5-5 Albuquerque, N.M. Maricopa SR
Jazelyn Maletino-Faga 26 12.3 3.4 1.1 21 G 5-5 Tracy, Calif. Venture Academy SR
Haley Villegas 28 11.8 2.2 3.6 00 G 5-3 Anthem, Ariz. Boulder Creek SR
Bethany Wolph 22 11.3 1.0 0.8 5 G 5-5 Chandler, Ariz. Valley Christian JR
Jenna Knudson 26 7.7 4.1 3.7 33 G 5-8 Sterling, Colo. Sterling SR
Susie Reynoso 25 5.6 2.4 1.2 1 G/F 5-9 Escondido, Calif. Mission Hills FR
Kamryn Brown 27 5.4 4.7 0.3 2 F/C 6-2 Gilbert, Ariz. Highland HS SO
Melissa Pfeifer 28 4.8 8.7 1.5 24 G/F 5-10 Hays, Kan. Thomas More Prep-Marian SR
Allese Williams 28 4.4 3.5 0.6 12 F/C 5-11 Canyon Country, Calif. Canyon SR
Amber Rollins 12 3.8 2.8 1.5 10 G 5-8 Scottsdale, Ariz. Saguaro SR
McKenna Klecker 19 3.8 1.3 0.4 13 F 5-10 Chandler, Ariz. Seton Catholic FR
Savannah Madden 17 1.8 1.8 1.1 14 G 5-8 Kalamazoo, Mich. Hackett Catholic Prep SO
Jordenn Reibel 15 1.3 1.2 1.2 3 G 5-7 Litchfield Park, Ariz. Millenium HS SR
using this command
[Player.first,Player.last,Player.gp,Player.ppg,Player.rpg,Player.apg,Player.num,PLayer.pos,Player.hgt,Player.hTown,Player.pSchool,Player.class] = textread('ERAU Womens Basketball stats.txt','%s %s %d %f %f %f %d %s %s %s %s %s');
but i keep getting this error:
Error using dataread
Trouble reading integer from file (row 16, field 3) ==>
Error in textread (line 171)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
How do I fix this?

Answers (1)

Walter Roberson
Walter Roberson on 3 Apr 2020
textread() is not recommended. It is recommended that you switch to textscan() or to readtable().
textread('ERAU Womens Basketball stats.txt','%s %s %d %f %f %f %d %s %s %s %s %s')
That is 12 fields on a single line, with the fields separated by whitespace. textread() does not start a input line each time it reaches the end of the format: it keeps going on the current line.
In the below, the ^ show the beginning of each input field, and the entries such as 3d show the format item number (e.g., 3rd entry in the format) and the expected datatype. #FAIL marks a format mismatch that would terminate reading. ! such as 7d! marks a format mismatch that would not terminate reading, such as floating point number on input with an integer format.
Danae Ruiz 26 13.7 4.0 2.3 22 G 5-5 Albuquerque, N.M. Maricopa SR
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
1s 2s 3d 4f 5f 6f 7d 8s 9s 10s 11s 12s 1s
Jazelyn Maletino-Faga 26 12.3 3.4 1.1 21 G 5-5 Tracy, Calif. Venture Academy SR
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
2s 3d#FAIL 4f 5f 6f 7d! 8s 9s 10s 11s 12s 1s 2s 3d#FAIL
Haley Villegas 28 11.8 2.2 3.6 00 G 5-3 Anthem, Ariz. Boulder Creek SR
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
4f#FAIL 5f#FAIL 6f 7d! 8s 9s 10s 11s 12s 1s 2s 3d#FAIL 4f#FAIL 5f#FAIL
What you have not taken into account is that 'Alburquerque, N.M' is two entries not one, and likewise 'Venture Academy' is two entries, not one.
Possibly your input file is tab delimited instead of space delimited, but you did not tell textread() that.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!