Help with Dataread and textread error

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

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.
Thanks for your help,
I did try to change to textscan instead of textread but got a different error for it, might not have understood it properly and written something that does make any sense.
I've attached the file. I'm trying to open a txt file on this script and have just been given it with no explaination so just a bit lost.
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.
The last line of the file is 21619 so there isn't a row thats 21620
I would suspect you have an off-by-one error in calculating numrow, or alternately that your numskip is one too large.
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...

Sign in to comment.

Answers (0)

Products

Asked:

on 15 Jul 2020

Commented:

dpb
on 16 Jul 2020

Community Treasure Hunt

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

Start Hunting!