【问题标题】:Matlab: How to read in numbers with a comma as decimal separator?Matlab:如何用逗号作为小数分隔符读取数字?
【发布时间】:2012-01-02 11:53:36
【问题描述】:

我有很多(数十万个)相当大 (>0.5MB) 的文件,其中数据是数字的,但用逗号作为小数分隔符。 使用像sed "s/,/./g" 这样的外部工具对我来说是不切实际的。 当分隔符是点时,我只使用textscan(fid, '%f%f%f'),但我看不到更改小数分隔符的选项。 我怎样才能有效地读取这样的文件?

文件中的示例行:

5,040000    18,040000   -0,030000

注意:有一个similar question for R,但我用的是Matlab。

【问题讨论】:

标签: matlab file-io decimal-point


【解决方案1】:

通过测试脚本,我发现系数小于 1.5。我的代码如下所示:

tmco = {'NumHeaderLines', 1      , ...
        'NumColumns'    , 5      , ...
        'ConvString'    , '%f'   , ...
        'InfoLevel'     , 0      , ...
        'ReadMode'      , 'block', ...
        'ReplaceChar'   , {',.'} } ;

A = txt2mat(filename, tmco{:});

注意不同的 'ReplaceChar' 值和 'ReadMode' 'block'。

我的(不是太新的)机器上大约 5MB 的文件得到以下结果:

  • txt2mat 测试逗号平均值。时间:0.63231
  • txt2mat 测试点平均值。时间:0.45715
  • textscan 测试点平均值。时间:0.4787

我的测试脚本的完整代码:

%% 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 的开销较大,较小文件的系数会增加。但即使是 0.5MB 的文件,我得到的文件也少于 2 个。
【解决方案2】:

您可以使用txt2mat。

A = txt2mat('data.txt');

它将自动处理数据。但你可以明确地说:

A = txt2mat('data.txt','ReplaceChar',',.');

附:它可能效率不高,但如果您只需要特定数据格式的部分,则可以从源文件中复制该部分。

【讨论】:

【解决方案3】:

您可以尝试通过添加标题行数来加速 txt2mat,如果可能的话,添加列数作为绕过其文件分析的输入。与使用点分隔小数的文本扫描导入相比,不应该有 25 倍。 (您也可以使用 mathworks 网站上的作者页面与我联系。) 如果您发现在 matlab 中处理逗号分隔小数的更有效方法,请告诉我们。

【讨论】:

  • 嗯,它仍然慢得多。与使用点分隔数字的 textscan 导入相比,我实现了 20 倍。我使用了公式:txt2mat(filename, 'InfoLevel', 0, 'ReplaceChar', {',', '.'}, 'NumHeaderLines', 1, 'ConvString', repmat('%f', 1,5), 'NumColumns', 5);
【解决方案4】:

我的解决方案(假设逗号仅用作小数位,并且空格表示列):

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

【讨论】:

    猜你喜欢
    • 2011-09-01
    • 1970-01-01
    • 2011-04-11
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多