0

I'm trying to textscan a file and read a single line until the end of it, undependently of the number of elements in that line.

My file is a .txt file formatted like this :

602,598,302,456,1023,523,....
293,291,566,331,987,56,....
589,202,429,2911,294,567,...

And so on. I have the number of the line, and all lines have the same number of elements, but it can vary from one file to another.

I wrote something like:

fid = fopen('somefile.txt');
C = textscan(fid, formatSpec,'HeaderLines',Row-1);
TheLine = C{1};
fclose(fid);

X = numel(TheLine);
plot(1:X,TheLine);

I really don't know what to type in the formatSpec field. I've tried a few things in the way of %[^\n] but I didn't get much sucess.

raggot
  • 992
  • 15
  • 38
Vissenbot
  • 227
  • 2
  • 5
  • 15

1 Answers1

1

Try this -

C = textscan(fid, '%d,','HeaderLines',Row-1);

Row will specify the row of data that you want to extract from the text file.

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • Sweet! It works fine! Can you explain fast how this works? I haven't seen anything of the likes online... – Vissenbot May 15 '14 at 20:18
  • 1
    @Marc-olivierLessard Well let's suppose that you know there are 6 elements in that particular row, you would usually do - `C = textscan(fid, '%d,%d,%d,%d,%d,%d,','HeaderLines',Row-1)`. Now if you don't know the number of elements, but you know that the elements are of consistent format then you would mention only one `%d,` and MATLAB would internally replicate it. This is a big of a guestimate at this point though backed up by some doc search and code runs. – Divakar May 15 '14 at 20:50