将逗号转换为点

时间:2012-10-30 15:29:43

标签: matlab

我有一个明智的文本文件:

0,472412
0,455627
0,439148
0,421753
...
0,116577
0,086670
0,057373
0,027161

如何在matlab中将逗号转换为点?

4 个答案:

答案 0 :(得分:4)

Matlabs网站上的

This post建议:

function comma2point_overwrite( filespec )
% replaces all occurences of comma (",") with point (".") in a text-file.
% Note that the file is overwritten, which is the price for high speed.
    file = memmapfile( filespec, 'writable', true );
    comma = uint8(',');
    point = uint8('.');
    file.Data( transpose( file.Data==comma) ) = point;
    delete(file)
end 

答案 1 :(得分:2)

......或者你可以这样做:

wholefile = fileread('yourfile.txt')       % read in entire file
newfiledata = strrep(wholefile,',','.')    % replace commas with full stops
fileid = fopen('yourfile.txt','w')         % open file to write
fprintf(fileid,'%s',newfiledata)           % print to file
fclose(fid2)

答案 2 :(得分:1)

另一个选项(因为你想把文件打开到matlab的工作区)是:

  • 使用

    加载文件
     a=load('filename.txt');
    

因为逗号是默认分隔符,所以你会得到一个2xN矩阵,例如:

a =

       0      472412
       0      455627
       0      439148
       0      421753
  • 找到重要的数字位数以获得正确的小数点位置:

     d = 10.^(1+floor(log10(abs(a(:,2)))));
    
  • 然后:

     v=a(:,2)./d
    

将产生您想要的矢量。现在你可以把它保存回文件,或做任何事......

答案 3 :(得分:1)

  
    

α= '0,00445'

  

a = 0,00445

  
    

B = strrep(A, '', '')

  

b = 0.00445