1

What is the equivalent of fgel and fgets in MATLAB for reading one column at a time (not a line) from a text file?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
user1611107
  • 287
  • 1
  • 4
  • 13
  • 1
    To read one column from a text file, you'll need to scan through the entire file. Since the file is stored sequentially, you're bound to read one line at a time, extracting the desired value from each line. – Eitan T Sep 22 '13 at 11:25
  • @EitanT: Thanks, but I hope there's another way still. My data is too heavy to be read at once.. I leave the question opened. – user1611107 Sep 22 '13 at 11:31
  • Is there any smart way to rotate the data in my txt file b4 trying to read it to a whole matrix or line by line? The problem is that the statistics that I want to perform has to be performed on what's currently in the columns (the data are simulated paths of 10k random walkers, where the rows are positions of different particles and columns are time) – user1611107 Sep 22 '13 at 11:40
  • You'll still have to read it line by line in order to manipulate it. – Eitan T Sep 22 '13 at 12:22
  • related questions: [this](http://stackoverflow.com/questions/5214422/load-only-a-column-of-file-matlab), [this](http://stackoverflow.com/questions/17440689/reading-specific-column-from-csv-file-in-matlab), [this](http://stackoverflow.com/questions/18240893/csv-importing-but-not-reading-in-anything-but-first-column) and many more... – Eitan T Sep 22 '13 at 14:04

1 Answers1

2

You cannot avoid reading the file. However, if your dataset is large, you can tell MATLAB to ignore the irrelevant parts while reading the file.

For instance, if your columns are space delimited, and you want to read the floating-point numbers in the first column, you can try the following:

fid = fopen('input.txt');
C = textscan(fid, '%f %*[^\n]');
C = C{:};
fclose(fid);

This still reads the entire file, but stores only the first column in memory.

Eitan T
  • 32,660
  • 14
  • 72
  • 109