MATLAB: How to convert ‘comma’ to ‘dot’

commadot

Hi;
I have a txt file. But it includes comma. How can I convert comma to dot? Because with comma I can not obtain the exact graphic..
Thanks a lot.
I attach the txt file.

Best Answer

You can use my function fstrrep to replace each . with a , and then use dlmread to read the data:
fstrrep('test22.txt', ',', '.')
data = dlmread('test22.txt')
The m-file:
function fstrrep(filename, oldsubstr, newsubstr, newfilename)
%FSTRREP Replace string in file.
%


%FSTRREP(FILENAME, OLDSUBSTR, NEWSUBSTR, [NEWFILENAME])read each line in
%FILENAME and replaces the string OLDSUBSTR with NEWSUBSTR and writes the
%new line to NEWFILENAME. If the optional argument NEWFILENAME is not
%given, the contents of the original file is changed.
%
%Example: In file 'test.txt', replace each ',' with a '.'
% fstrrep('test.txt', ',' '.')
%
%Thorsten.Hansen@psychol.uni-giessen.de
%%open file to read from and new file to write to
fid1 = fopen(filename, 'r');
if fid1 == -1
error(['Cannot open file ' filename ' for reading.']);
end
if nargin < 4
newfilename = '_temp.txt';
end
fid2 = fopen(newfilename, 'w');
if fid2 == -1
error(['Cannot open file ' newfilename ' for writing.']);
end
%%read line and write changed line to new file
line = fgets(fid1);
while line ~= -1
newline = strrep(line, oldsubstr, newsubstr);
fprintf(fid2, '%s', newline);
line = fgets(fid1);
end
%%close both files
st = fclose(fid1);
if st == -1
error(['Cannot close ' filename '.'])
end
st = fclose(fid2);
if st == -1
error(['Cannot close ' tempfilename '.'])
end
%%replace old file with new file
if nargin < 4
movefile(newfilename, filename)
end