1

I have a huge data, with 7 colums and 20000 rows. I let Matlab to read mydata:

[tdata zdata kdata ldata mdata ndata pdata]=textread('mydata.txt')

But what I need is something else. I want to let Matlab to collect every 1000. Row from the data. Help to textread give an explanation for the first nth data:

[tdata zdata kdata ldata mdata ndata pdata]=textread('mydata.txt',n).

Is there any way to do this with a small change of textread format? Or should I write a for loop?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
user1018331
  • 143
  • 4
  • 14
  • 1
    For a related question, please see: http://stackoverflow.com/questions/5531082/matlab-how-to-read-every-nth-line-of-a-text-file – H.Muster Sep 18 '12 at 13:17

1 Answers1

2

It's probably easiest if you read all data, and crop away the unwanted data afterwards:

[tdata zdata kdata ldata mdata ndata pdata] = textread('mydata.txt')

tdata = tdata(1:1000:end);
zdata = zdata(1:1000:end);
kdata = kdata(1:1000:end);
ldata = ldata(1:1000:end);
mdata = mdata(1:1000:end);
ndata = ndata(1:1000:end);
pdata = pdata(1:1000:end);

If the memory overhead is too large, or you find this unacceptable, you'll have to write a loop with fgetl, something along these lines:

fid = fopen('mydata.txt', 'r');
i = 0;
while (~feof(fid))
    i = i + 1;
    line = fgetl(fid);

    if mod(i, 1000) == 0
        parsed = textscan(line, '%f%f%f%f%f%f');
        %# etc.

    end
end
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • I don't want to use a for or while loop. I couldn't think the first one:( Thank you very much. It was very helpfull. – user1018331 Sep 18 '12 at 13:24
  • may I ask you something about your second advise? I'll get each line for mod(1000). But is it posible to get it separately, as tdata,ydata,... etc? – user1018331 Sep 18 '12 at 14:19
  • 1
    @user1018331 That's what the line `parsed = textscan(line, '%f%f%f%f%f%f');` is for; this splits the line up in separate values, which are stored in cell-array `parsed`. You can store these manually somewhere else (like in `tdata(j), ldata(j), ...` (where `j` is incremented inside the `if`.) – Rody Oldenhuis Sep 18 '12 at 14:42