Filter löschen
Filter löschen

How to convert 'comma' to 'dot'?

21 Ansichten (letzte 30 Tage)
nancy
nancy am 10 Jun. 2016
Bearbeitet: DGM am 7 Feb. 2022
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.

Akzeptierte Antwort

Thorsten
Thorsten am 16 Jun. 2016
Bearbeitet: Thorsten am 16 Jun. 2016
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
  1 Kommentar
nancy
nancy am 16 Jun. 2016
Bu I couldn't execute the code again.. Could you please update and rewrite the code that I have attached and send it to me?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 10 Jun. 2016
Bearbeitet: Azzi Abdelmalek am 10 Jun. 2016
a=importdata('test2.txt')
b=strrep(a,',','.')
c=cell2mat(cellfun(@str2num,b,'un',0))
  2 Kommentare
Mohamed Asaad
Mohamed Asaad am 7 Feb. 2022
Hi! This works for me. However when i use files including negative value or Nan, it does not work. I get error message " Conversion to double from struct is not possible."
DGM
DGM am 7 Feb. 2022
Bearbeitet: DGM am 7 Feb. 2022
The output type of importdata() varies depending on how it manages to split the text based on the specified delimiters. There are probably other ways to do this, but consider this. I've simply renamed the extensions so that they can be uploaded and run here.
A = importdata('Au_1_E_chem_experiment_SPR_angle_offset_after_PEG.txt',' ');
A = strrep(A,',','.'); % convert commas to dots
A = regexp(A,'([^\t]*)','tokens'); % split each line into its elements
B = cellfun(@str2double,vertcat(A{:})); % convert to numeric
B(1:10,:) % show a sample
ans = 10×2
0.1166 -1.2111 0.2069 -1.2121 0.2980 -1.2116 0.3908 -1.2107 0.4812 -1.2110 0.5730 -1.2108 0.6646 -1.2105 0.7555 -1.2099 0.8466 -1.2090 0.9372 -1.2108
Similarly for the other file
A = importdata('Au_1_E_chem_experiment_TIR_angle.txt',' ');
A = strrep(A,',','.'); % convert commas to dots
A = regexp(A,'([^\t]*)','tokens'); % split each line into its elements
B = cellfun(@str2double,vertcat(A{:})); % convert to numeric
B = B(2:end,:); % strip off the header
B(55:64,:) % show a sample
ans = 10×2
5.1407 61.1610 5.2397 61.1607 5.3305 NaN 5.4235 NaN 5.5161 58.1150 5.6089 NaN 5.7000 58.1150 5.7933 NaN 5.8848 NaN 5.9764 NaN

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by