How to read data from a .dat file row by row

5 views (last 30 days)
A very large data file with .dat is to be read and loaded which contains a first row consisting of some characters as the head and a table containing 10,000 rows times 10,000 columns, whose data is in cell form.
I now want to restore the first row as a vector, say 'head',each entry being a string of characters, and the rest is a matrix A, each of whose elements are characters but in cell format originally.
Thanks!

Answers (1)

Walter Roberson
Walter Roberson on 19 Sep 2016
If the .dat is in a well structured text format, then:
ncol = 10000;
sep = ',' ; %could be '\t' or other separator
hfmt = repmat('%s', 1, ncol);
cfmt = repmat('%f', 1, ncol);
fid = fopen('YourFileNameGoesHere.dat', 'rt');
head = textscan(fid, hfmt, 1, 'Delimiter', sep, 'Whitespace', '');
A = textscan(fid, cfmt, 'Delimiter', sep, 'Whitespace', '');
fclose(fid);
You would need to adjust the formats and separator.

Community Treasure Hunt

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

Start Hunting!