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 "."
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{:});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments