Opening a textfile with only reading a upto a certain number of lines using textscan
19 views (last 30 days)
Show older comments
subharthi chowdhuri
on 14 Jul 2016
Commented: Walter Roberson
on 14 Jul 2016
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?
0 Comments
Accepted Answer
Walter Roberson
on 14 Jul 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 Comments
Walter Roberson
on 14 Jul 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.
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!