Help with Dataread and textread error
Show older comments
I'm a beginner with matlab and i'm trying to do power spectrum analysis with a txt file and this is the error that i'm receiving, please help me understand it.
Error using dataread
Trouble reading integer from file (row 21620, field 1) ==>
Error in textread (line 171)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
Error in ReadSpectraVSplots (line 46)
=
textread(fnameFFT,'%d%s%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f',numrow,'headerlines',numskip);
>>
6 Comments
dpb
on 15 Jul 2020
textread has been deprecated; that it needs a variable for every variable in the input format string is a real weakness when have many columns in a file.
The specific error is there is not a recognizable integer at the target line as the first element in that line in the input file. What's there we don't know, but it's not an integer.
It's essentially impossible to debug anything written as the format string above is -- however, the doc is mum on how to avoid so it's the typical beginner's mistake and nothing to be ashamed about...but
>> fmt='%d%s%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
>> sum(fmt=='f')
ans =
81
>>
But, as starters, write something like
fmt=['%d' repmat('%s',1,3) repmat('%f',1,81)];
and pass the variable for the format string.
To solve your problem, use one of the higher-level routines like readtable or even textscan.
If you want specific further help, attach the file so folks can see what you are dealing with, exactly.
Monika Chaulagain
on 16 Jul 2020
Walter Roberson
on 16 Jul 2020
Your file does not have your textscan() attempt, and you did not post the error message you got for it.
Anyhow, look at line 21620 of your input file: it does not start with an integer.
Monika Chaulagain
on 16 Jul 2020
Walter Roberson
on 16 Jul 2020
I would suspect you have an off-by-one error in calculating numrow, or alternately that your numskip is one too large.
dpb
on 16 Jul 2020
Again, if want somebody to fix the format string and/or show specifics for textscan or another non-deprecated function to replace textread, attach (a section of) the data file so can see what's actually there.
It's probably too large to do whole thing as text, just select first/last pages in editor and paste them into another file would be more than enough...
Answers (0)
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!