필터 지우기
필터 지우기

How to replace commas with dot

조회 수: 12 (최근 30일)
Algirdas Kluonius
Algirdas Kluonius 2013년 5월 8일
댓글: Franciszek Aniol 2020년 4월 21일
Hello everyone can someone help me, I have german IFM optics sensor and it gives me a file with german formatting. That means comma instead of dot. So if i have a number 0,25 matlab can't understand it correctly, i need to change it from 0,25 to 0.25. I found a link which does not help, because it's faulty http://www.mathworks.com/matlabcentral/answers/51399
it suggests to use data = strrep (a, ',' , '.') but as i use it, yeah i get it 0.25 from 0,25 but if i try to test it and write: >> data>1 ans = 1 1 1 1
if i write data+1 ans = 49 47 51 54
can someone help me on this one, because later csv file has 50 rows like this "0,95084310;0,95006830;0,99675316;0,99362558;1, ...." which is actually a first column of a picture. Thanks in advance
  댓글 수: 2
Mingyao
Mingyao 2013년 5월 8일
Suppose that you have a .txt file that contains the following data:
0,950843;0,95006830;0,99675316;0,99362558;
1,950843;1,95006830;1,99675316;1,99362558;
The following code converts it into a double array:
fid=fopen('sample.txt');
data=textscan(fid,'%s %s %s %s','Delimiter',';');
fclose(fid);
for i=1:4
for j=1:2
data{i}{j}=str2double(strrep(data{i}{j},',','.'));
end
data{i}=cell2mat(data{i});
end
data=cell2mat(data);
Good luck!
Algirdas Kluonius
Algirdas Kluonius 2013년 5월 9일
First answer is wrong because I have 64 values in a row and I can't write '%s' 64 times and it also does not work.

댓글을 달려면 로그인하십시오.

답변 (2개)

Andreas Goser
Andreas Goser 2013년 5월 9일
I created this one a million years ago - not sure if it works today:
function comma2dot()
%COMMA2DOT converts comma decimal separator data into dot decimal separator data
% COMMA2DOT() opens an ASCII file finds all comma characters, changes
% them into dot characters and saves the data as a new ASCII file
% V 1.0: Andreas Goser, 7.4.2001
[fname, pname]=uigetfile('*.*', 'ASCII data file with comma decimal separator');
if fname
data=char(textread([pname, fname], '%s', 'delimiter', '\n', 'whitespace', ''));
for k=1:size(data, 1)
f=findstr(data(k, :), ',');
data(k, f)='.';
end
ind=findstr(fname, '.');
fid=fopen([pname, fname(1:ind-1), '_dot', fname(ind:length(fname))], 'w');
for k=1:size(data, 1)-1
fprintf(fid, '%s\r\n', data(k, :));
end
fprintf(fid, '%s', data(size(data, 1), :));
fclose(fid);
disp([pname, fname(1:ind-1), '_dot', fname(ind:length(fname)), ' written']);
end
  댓글 수: 2
Thuy Hoang
Thuy Hoang 2018년 3월 19일
OK! dear Supper Man! I thank you so much. :) It's so good! :) Thank you so much again.
Franciszek Aniol
Franciszek Aniol 2020년 4월 21일
It's so good, you are the best :)

댓글을 달려면 로그인하십시오.


Jason Ross
Jason Ross 2013년 5월 8일
Have you tried using the Import Data Wizard? One of the options allows you to set the delimiter to be a comma. You can use generate a function or a script once the data looks good that will have the relevant import code in it.
  댓글 수: 1
Algirdas Kluonius
Algirdas Kluonius 2013년 5월 9일
The import wizard gives a string and works partially in any case i need to modify all anyway

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by