0

I have a text file which I want to import into matlab. Here are the first 2 rows of the text file (tempfile.txt):

1,"4/26/2016","6:40:00 PM","111","0","13.45","NaN","ACTIVE","NaN",
2,"4/26/2016","6:40:30 PM","73","0","14.99","NaN","ACTIVE","NaN",

When I tried using textscan:

fid = fopen('tempfile.txt');
data = textscan(fid, '%*d %s %s %s %*d %*d %*d %*s %*s', 'Delimiter', ',')

It only imports the first row of the text file. I have tried adding \n to the formatSpec but it still does not work. Please help!

TYL
  • 1,577
  • 20
  • 33

2 Answers2

1

Your problem is that all of your fields are double quoted - i.e. They are strings, and you cannot parse them in as Floats/Doubles, instead parse them in as strings, and cast them to Doubles in Matlab:

data = textscan(fid, '%d %s %s %s %s %s %s %s %s', 'Delimiter', ',')

works fine at parsing your data, then use str2num to convert your data back to numeric. Why do you have double quotes around everything?

=============EDIT============

Since you only want 3 fields, you should do something like:

 fid = fopen('abc1.txt');
 data = textscan(fid, '%*d %s %s %s %*s %*s %*s %*s %*s', 'Delimiter', ',')
GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
  • That works! Thanks a lot, you are my hero, as I have trying to figure importing data into matlab for the whole day. Anyway, I am not sure why there are double quotes, but as you suggested, str2num solves it. Thanks again! – TYL Jun 30 '16 at 08:25
0

It seems that you have a comma separated values CSV file try this function instead:

M = csvread('tempfile.txt')
anquegi
  • 11,125
  • 4
  • 51
  • 67
  • That's not possible as csvread requires all elements to be numeric (I think so), which my text file does not qualify – TYL Jun 30 '16 at 08:20