Opening a textfile with only reading a upto a certain number of lines using textscan

20 vues (au cours des 30 derniers jours)
I have a textfile which contains many rows and columns. Suppose if I want to read only upto first 100 rows and first 30 columns i.e. first 100 lines of the textfile, how do I do it using textscan?

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Juil 2016
Immediately after the format specification, you can give a number. That number will be used as the number of times to apply the format. So for example,
fmt = repmat('%f', 1, 30);
datacell = textscan(fid, fmt, 100);
If you have more than 30 columns but only want to read the first 30 then
fmt = [repmat('%f', 1, 30), '%*[^\n]\n'];
The [^\n] format reads every character until the next newline. The * before it tells textscan to throw away that data.
  2 commentaires
subharthi chowdhuri
subharthi chowdhuri le 14 Juil 2016
Thank you very much Walter for your help. Its very useful. I have one another small doubt. Whenever I am trying to open a large textfile using textscan which has many rows and columns, the matlab is getting hanged and in the end the my laptop gets restarted again. I am using Matlab 2015a and my machine is 64 bit 8 Gb ram and core I5 processor. Do you have any idea why this might be happening?
Walter Roberson
Walter Roberson le 14 Juil 2016
I do not have any idea at the moment. fopen() should be fast no matter how big the file is, provided that the file is "local".
If the file is stored on a network drive then it depends upon the network file system; it might decide to copy the file locally (but doing that would be a bad idea for networked file systems as it breaks the semantics about simultaneous access... unless you happen to be using the 30+ year old rfs file system on an obsolete Apollo workstation, perhaps...)
Using textscan() to read a large file could take a fair bit of time, but textscan does not open files.
I do not know at the moment how textscan is implemented, but an obvious difficulty it could run into reading large files is if it has to keep "growing" the arrays, because it does not know ahead of time how many results it is going to need to store. There are techniques for reducing that problem, but I do not know how textscan manages memory internally.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by