9 views (last 30 days)
ha ha on 16 Jun 2019
Commented: per isakson on 16 Jun 2019
Let's say I have a very very large .txt file with (200millions row & 11 columns= 200m-by-11 matrix). All data are numeric number value (e.g., 10, 100 ,200...) . My file is ~ 20GB
When I load this data in Matlab, the errors occurs: "Out of Memory"
clear;clc;filename = 'test42.txt'; load('test42.txt');P = test42(:,1:3);%get data=coordinate(x,y,z) from set of data "column" at (all row & column 1,2,3)
My PC system: win10-64 bit, RAM 16GB, core-i7, HDD:1TB; SSD 1TB
Actually, I just want to load the data contain only first 3 columns. It mean, the matrix that I want to get is: 200m-by-3 matrix. And with the reduce column, I hope Matlab is able to load data.
Do you know any way to read the whole dataset, or read the reduce data with only first 3 columns? Thanks.
The format of my file is like this. per isakson on 16 Jun 2019
Edited: per isakson on 16 Jun 2019
You didn't say how much physical memory is in your system.
Matlab provides ways to handle large text files, Large Files and Big Data, but forget that for a moment. It's not a free lunch.
" (e.g., 10, 100 ,200...)" Does that mean positive whole numbers only? If so, do you know the maximum value of the three first columns, respectively?
"When I load this data" What exactly did you do?
The three first columns will take 4.8GB to store as double.
>> 200*1e6*3*8/1e9
ans =
4.8
But do you need to use double?
Simplest first, convert the three first columns to double and skip the remaining seven columns. Try
%%
fid = fopen( 'c:\whatever\the_huge_text_file.txt', 'r' );
cac = textscan( fid, '%f%f%f%*f%*f%*f%*f%*f%*f%*f', 'HeaderLines',0 );
fclose( fid );
Or use an alternative formatspec, which is a bit easier to read. It says read the first three columns and skip the rest up til a new-line character.
cac = textscan( num2str([1:10]), '%f%f%f%*[^\n]', 'HeaderLines',0 );
Next step requires input from you on the numbers in the three first columns.
In response to the edited question
To keep the precision of the numbers in the text file you need to use double. I'm positive that
%%
fid = fopen( 'c:\whatever\test42.txt', 'r' );
cac = textscan( fid, '%f%f%f%*f%*f%*f%*f%*f%*f%*f', 'CollectOutput',true );
fclose( fid );

ha ha on 16 Jun 2019
Thanks @per isakson. Have a nice day. %%
tic
fid = fopen( 'C:\Users\USER\Desktop\PLO\Rama9_A_with_car.txt', 'r' );
cac = textscan( fid, '%f%f%f%*[^\n]', 'CollectOutput',true );
fclose( fid );
P=cat(1, cac{:});
toc ha ha on 16 Jun 2019
%%
tic
clear;clc;filename = 'Rama9_A.txt';
inputfile = importfile_upgrade(filename);%this is 1x1 cell contain kx3 matric of all data
P=cat(1, inputfile{:});%get data=coordinate(x,y,z) from set of data "column" at (all row & column 1,2,3)
toc per isakson on 16 Jun 2019
Thank you for showing these results.
The three elapsed times puzzles me. The differences are so large.
However, one thing I know is that it's difficult to reproduce results from reading files. A specific result depends on the state of the system cache. Large parts of the file may already be in the cache and furthermore the cache may be more or less "fragmented".
You use the statement
P=cat(1, inputfile{:});
It's possible to increase readability (imo) and save a second or two. Try
>> cssm
Elapsed time is 1.812582 seconds.
Elapsed time is 2.085810 seconds.
Elapsed time is 0.000041 seconds.
Elapsed time is 0.314024 seconds.
>> cssm
Elapsed time is 1.700285 seconds.
Elapsed time is 2.239885 seconds.
Elapsed time is 0.000040 seconds.
Elapsed time is 0.370981 seconds.
%% Sample data
tic, cac = { reshape( [1:194500412*3], [],3 ) }; toc
%%
tic, P1 = cat(1,cac{:}); toc
%%
tic, P2 = cac{1}; toc
%%
tic, P3 = cell2mat( cac ); toc
and P1 and P2 are equal
>> all( P1==P2, 1 )
ans =
1×3 logical array
1 1 1

Walter Roberson on 16 Jun 2019
textscan() is more likely to succeed than some of the other alternatives.
Most reliable would be to pre-allocate all of the storage, and then to process chunks of the file at a time (for efficiency). For example, if you told textscan() to read 50 lines of the file, that would be just under 4 Kb, which would fit easily into MATLAB's "small blocks" storage strategy where it can extend an array in place if the array is sufficiently small. Copy the 50 rows into the master matrix, proceed to next chunk.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7682659,"math_prob":0.76824135,"size":1412,"snap":"2020-34-2020-40","text_gpt3_token_len":409,"char_repetition_ratio":0.14204545,"word_repetition_ratio":0.026200874,"special_character_ratio":0.30311614,"punctuation_ratio":0.17088607,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.974506,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,8,null,3,null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-05T20:56:12Z\",\"WARC-Record-ID\":\"<urn:uuid:67444ef2-3ee4-446b-bfb7-4fec5587144e>\",\"Content-Length\":\"157163\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a008c45c-f18b-42d6-be42-8fad0527f8ac>\",\"WARC-Concurrent-To\":\"<urn:uuid:09f5c228-a18e-4844-82bb-489d257b997a>\",\"WARC-IP-Address\":\"184.25.198.13\",\"WARC-Target-URI\":\"https://in.mathworks.com/matlabcentral/answers/467290-loading-large-txt-files?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:CDNWLZXZGQXHZIC2POEBOQ3YMT6T7CK3\",\"WARC-Block-Digest\":\"sha1:6Y424FIIRO6L3W5ODZIKXOEIAEW4OXL4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735964.82_warc_CC-MAIN-20200805183003-20200805213003-00551.warc.gz\"}"}