我有很多(数十万)相当大(> 0.5MB)的文件,其中数据是数字的,但用逗号作为小数分隔符。对我来说使用像sed "s/,/./g"
. 当分隔符是点时,我只使用textscan(fid, '%f%f%f')
,但我看不到更改小数分隔符的选项。如何以有效的方式读取这样的文件?
文件中的示例行:
5,040000 18,040000 -0,030000
注意: R 有一个类似的问题,但我使用 Matlab。
我有很多(数十万)相当大(> 0.5MB)的文件,其中数据是数字的,但用逗号作为小数分隔符。对我来说使用像sed "s/,/./g"
. 当分隔符是点时,我只使用textscan(fid, '%f%f%f')
,但我看不到更改小数分隔符的选项。如何以有效的方式读取这样的文件?
文件中的示例行:
5,040000 18,040000 -0,030000
注意: R 有一个类似的问题,但我使用 Matlab。
使用测试脚本,我发现系数小于 1.5。我的代码看起来像:
tmco = {'NumHeaderLines', 1 , ...
'NumColumns' , 5 , ...
'ConvString' , '%f' , ...
'InfoLevel' , 0 , ...
'ReadMode' , 'block', ...
'ReplaceChar' , {',.'} } ;
A = txt2mat(filename, tmco{:});
注意不同的 'ReplaceChar' 值和 'ReadMode' 'block'。
在我的(不是太新的)机器上,我得到了一个 ~5MB 文件的以下结果:
我的测试脚本的完整代码:
%% generate sample files
fdot = 'C:\temp\cDot.txt';
fcom = 'C:\temp\cCom.txt';
c = 5; % # columns
r = 100000; % # rows
test = round(1e8*rand(r,c))/1e6;
tdot = sprintf([repmat('%f ', 1,c), '\r\n'], test.'); % '
tdot = ['a header line', char([13,10]), tdot];
tcom = strrep(tdot,'.',',');
% write dot file
fid = fopen(fdot,'w');
fprintf(fid, '%s', tdot);
fclose(fid);
% write comma file
fid = fopen(fcom,'w');
fprintf(fid, '%s', tcom);
fclose(fid);
disp('-----')
%% read back sample files with txt2mat and textscan
% txt2mat-options with comma decimal sep.
tmco = {'NumHeaderLines', 1 , ...
'NumColumns' , 5 , ...
'ConvString' , '%f' , ...
'InfoLevel' , 0 , ...
'ReadMode' , 'block', ...
'ReplaceChar' , {',.'} } ;
% txt2mat-options with dot decimal sep.
tmdo = {'NumHeaderLines', 1 , ...
'NumColumns' , 5 , ...
'ConvString' , '%f' , ...
'InfoLevel' , 0 , ...
'ReadMode' , 'block'} ;
% textscan-options
tsco = {'HeaderLines' , 1 , ...
'CollectOutput' , true } ;
A = txt2mat(fcom, tmco{:});
B = txt2mat(fdot, tmdo{:});
fid = fopen(fdot);
C = textscan(fid, repmat('%f',1,c) , tsco{:} );
fclose(fid);
C = C{1};
disp(['txt2mat test comma (1=Ok): ' num2str(isequal(A,test)) ])
disp(['txt2mat test dot (1=Ok): ' num2str(isequal(B,test)) ])
disp(['textscan test dot (1=Ok): ' num2str(isequal(C,test)) ])
disp('-----')
%% speed test
numTest = 20;
% A) txt2mat with comma
tic
for k = 1:numTest
A = txt2mat(fcom, tmco{:});
clear A
end
ttmc = toc;
disp(['txt2mat test comma avg. time: ' num2str(ttmc/numTest) ])
% B) txt2mat with dot
tic
for k = 1:numTest
B = txt2mat(fdot, tmdo{:});
clear B
end
ttmd = toc;
disp(['txt2mat test dot avg. time: ' num2str(ttmd/numTest) ])
% C) textscan with dot
tic
for k = 1:numTest
fid = fopen(fdot);
C = textscan(fid, repmat('%f',1,c) , tsco{:} );
fclose(fid);
C = C{1};
clear C
end
ttsc = toc;
disp(['textscan test dot avg. time: ' num2str(ttsc/numTest) ])
disp('-----')
您可以使用txt2mat
.
A = txt2mat('data.txt');
它将自动处理数据。但是你可以明确地说:
A = txt2mat('data.txt','ReplaceChar',',.');
PS 这可能效率不高,但如果您只需要特定数据格式的部分,则可以从源文件中复制该部分。
您可以尝试通过添加标题行数来加速 txt2mat,如果可能的话,还可以添加列数作为绕过其文件分析的输入。与使用点分隔小数的 textscan 导入相比,不应该有 25 倍。(您也可以使用 mathworks 网站上的作者页面与我联系。)如果您找到在 matlab 中处理逗号分隔小数的更有效方法,请告诉我们。
我的解决方案(假设逗号仅用作小数位,并且空格表示列):
fid = fopen("FILENAME");
indat = fread(fid, '*char');
fclose(fid);
indat = strrep(indat, ',', '.');
[colA, colB] = strread(indat, '%f %f');
如果您碰巧需要像我一样删除单个标题行,那么这应该可以:
fid = fopen("FILENAME"); %Open file
indat = fread(fid, '*char'); %Read in the entire file as characters
fclose(fid); %Close file
indat = strrep(indat, ',', '.'); %Replace commas with periods
endheader=strfind(indat,13); %Find first newline
indat=indat(endheader+1:size(indat,2)); %Extract all characters after first new line
[colA, colB] = strread(indat, '%f %f'); %Convert string to numerical data