我有一个像这个专栏一样的文本文件:
0,472412
0,455627
0,439148
0,421753
...
0,116577
0,086670
0,057373
0,027161
matlab中如何将逗号转换为点?
我有一个像这个专栏一样的文本文件:
0,472412
0,455627
0,439148
0,421753
...
0,116577
0,086670
0,057373
0,027161
matlab中如何将逗号转换为点?
Matlabs 网站上的这篇文章建议:
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
...或者你可以这样做:
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)
另一种选择,(因为您想将文件打开到 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
将产生您想要的向量。现在您可以将其保存回文件,或执行任何操作...
a='0,00445'
a = 0,00445
b=strrep(a,',','.')
b = 0.00445