我有多个大小约为2GB的文本文件(大约7000万行)。我还有一台四核机器并可以访问Parallel 计算 </跨度> 工具箱。
通常,您可以打开文件 并读取行,如下所示:
f = fopen(‘file.txt’);l = fgets(f);而〜isempty(l) %用l做某事 l = fgets(f);结束
我想 分发 </跨度> 在我的4个核心上“做点什么”
一些matlab的内置函数支持多线程 - 列表是 这里 。不需要Parallel Computing工具箱。
如果“使用l做某事”可以从工具箱中受益,只需在读取另一行之前实现该功能。
您也可以使用读取整个文件
fid = fopen('textfile.txt'); C = textscan(fid,'%s','delimiter','\n'); fclose(fid);
然后并行计算C中的单元格。
如果阅读时间是一个关键问题,您可能还想访问a中的部分数据文件 parfor 环。这是一个例子 Edric M Ellis 。
parfor
%Some data x = rand(1000, 10); fh = fopen( 'tmp.bin', 'wb' ); fwrite( fh, x, 'double' ); fclose( fh ); % Read the data y = zeros(1000, 10); parfor ii = 1:10 fh = fopen( 'tmp.bin', 'rb' ); % Get to the correct spot in the file: offset_bytes = (ii-1) * 1000 * 8; % 8 bytes/double fseek( fh, offset_bytes, 'bof' ); % read a column y(:,ii) = fread( fh, 1000, 'double' ); fclose( fh ); end % Check assert( isequal( x, y ) );