Clear Filters
Clear Filters

How To Get importdata to Ignore NaN?

9 views (last 30 days)
i have a text file that looks like this: (attached for reference)
"2014-01-01 23:56:00",3620609,37.8,23.83333,760.9666,11.375,815.4666,0,71493.23,1
"2014-01-01 23:56:30",3620610,41.66667,23.8,761,"NAN","NAN",0,"NAN",1
"2014-01-01 23:57:00",3620611,39.86666,23.73333,760.9666,11.375,815.5999,0,71593.27,1
my code is simply
a=importdata('TEST.txt');
And a becomes a struct.
looking at a.data, it only has two rows, and the third row (along with any other rows) just gets clipped off. How do I get the code to import the next lines? My resultant a.data is below by the way
3620609 37.8000000000000 23.8333300000000 760.966600000000 11.3750000000000 815.466600000000 0 71493.2300000000 1
3620610 41.6666700000000 23.8000000000000 761 NaN NaN NaN NaN NaN

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jun 2016
fid = fopen('TEST.txt', 'rt');
datacell = textscan(fid, '%q%f%f%f%f%f%f%f%f%f', 'Delimiter', ',', 'TreatAsEmpty, '"NAN"');
fclose(fid)
You might want to use %D instead of %q if you have R2014b or later; that would convert the date/time into datetime objects
If your version of MATLAB is too old for %q then you can use
'"%[^"]"%f%f%f%f%f%f%f%f%f'
You could also consider using readtable()
  1 Comment
Stephen23
Stephen23 on 8 Jun 2016
Ian Adler's "Answer" moved here:
You're original code did not work for me but using readtable did. Thanks

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!