在MATLAB中使用逗号十进制分隔符读取txt文件 [英] Read txt file with comma decimal separator in MATLAB
本文介绍了在MATLAB中使用逗号十进制分隔符读取txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的txt文件:
I have a txt file as such:
1,6 2 6,5 5 ... // ~ 1000 columns
0 1 4 2,5 ...
... // ~1000 rows
即,"作为小数点分隔符,而不是."
that is, "," as a decimal separator instead of "."
如何在MATLAB中正确阅读以输出如下信息:
How to read this properly in MATLAB to output as such:
1.6 2 6 5 ...
0 1 4 2.5 ...
...
推荐答案
没有简单的内置方法(令人惊讶!).您需要读取整个文件,然后替换字符串,然后将结果转换为数字.
There is no easy built-in way of doing this (surprisingly!). You'll want to read in the entire file, then do a string replacement, and then convert the result into numbers.
% Read file in as a series of strings
fid = fopen('data.txt', 'rb');
strings = textscan(fid, '%s', 'Delimiter', '');
fclose(fid);
% Replace all commas with decimal points
decimal_strings = regexprep(strings{1}, ',', '.');
% Convert to doubles and join all rows together
data = cellfun(@str2num, decimal_strings, 'uni', 0);
data = cat(1, data{:});
这篇关于在MATLAB中使用逗号十进制分隔符读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文