How to convert csv data with decimal csv

Hello Matlab Experts,
I am converting my csv (english) version to erman version. However getting the converted csv with extra space raw. Can anyone help me?
Thank you
clear all
str = fileread('C:\Users\Cha\Desktop\TOUCH.csv');
str = strrep(str, ',', ';');
str = strrep(str, '.', ',');
f = fopen('TOUCH.csv', 'wt');
fprintf(f, '%s', str);
fclose(f);

1 件のコメント

Ive J
Ive J 2020 年 12 月 18 日
Can you share your original file (TOUCH.csv) or a simulated one with the same structure?

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2020 年 12 月 18 日

0 投票

Change
f = fopen('TOUCH.csv', 'wt');
to
f = fopen('TOUCH.csv', 'w');
When you do fileread(), the character vector that is read in will have inside it any carriage returns and linefeeds that are in the original text. When you fwrite() when 'wt' is active, every newline to be written will be converted to carriage return followed by newline. So what was originally CR LF in your file would get written out as CR CR LF .
If you had reason to want to force CR LF line ending, then use 'w', but
str = strrep(strrep(str, char(13), ''), char(10), char([13 10]))
This would remove all existing carriage returns and then change all newline to carriage return followed by newline.
Or alternately you could write the entire replacement series with no strrep, using
str = regexprep(str, {',', '\.', '(?<!\r)\n'}, {';', ',', '\r\n'})
This does all of the replacements, and only converts \n to \r\n if the \n was not proceeded by a \r already.

8 件のコメント

Mohammed Cha
Mohammed Cha 2020 年 12 月 18 日
Thank you very very much
Mohammed Cha
Mohammed Cha 2020 年 12 月 21 日
Dear Sir,
In the same topic, i have one further question. I am trying to read multiple files from folder and trying to convert all files with same decimal conversion. After that i am trying to save all files with same name+additionally with underscore _ge. However, i am getting errors with cell array.
Could you please help me?
Thank you very much
myfilename=dir('*.csv')
numfiles= length(myfilename)
mydata=cell(1,numfiles)
for k=1:numfiles
mydata{k}=fileread(myfilename(k).name);
str=regexprep(mydata,{',','\.','(?<!\r)\n'},{';',',','\r\n'});
f = fopen('myfilename{1}.csv', 'w');
fprintf('%s\n',cell{:})
fclose(f);
end
Walter Roberson
Walter Roberson 2020 年 12 月 22 日
編集済み: Walter Roberson 2020 年 12 月 22 日
myfilename = dir('*.csv');
allfilenames = {myfilename.name};
%get rid of ones that are already _ge.csv
allfilenames(endsWith(allfilenames, '_ge.csv')) = [];
numfiles= length(allfilenames);
for k = 1:numfiles
oldname = allfilenames{K};
newname = regexprep(oldname, '\.csv', '_ge.csv');
mydata = fileread(oldname);
str = regexprep(mydata, {',','\.','(?<!\r)\n'}, {';',',','\r\n'});
f = fopen(newname, 'w');
fprintf('%s\n', str)
fclose(f);
end
Mohammed Cha
Mohammed Cha 2020 年 12 月 22 日
Dear Sir,
Thank you very much for the code and kind response. I tried it but showing one error as well as new converted file with _ge has no any data. Its empty.
Undefined function 'endsWith' for input arguments of type 'cell'.
Error in csvconversion_3 (line 16)
allfilenames(endsWith(allfilenames, '_ge.csv')) = [];
Walter Roberson
Walter Roberson 2020 年 12 月 22 日
endsWith() needs R2016b or later, and you did not indicate that you were using an old MATLAB release.
myfilename = dir('*.csv');
allfilenames = {myfilename.name};
%get rid of ones that are already _ge.csv
mask = ~cellfun(@isempty, regexp(allfilenames, '_ge\.csv$', 'once'));
allfilenames(mask) = [];
numfiles= length(allfilenames);
for k = 1:numfiles
oldname = allfilenames{K};
newname = regexprep(oldname, '\.csv', '_ge.csv');
mydata = fileread(oldname);
str = regexprep(mydata, {',','\.','(?<!\r)\n'}, {';',',','\r\n'});
f = fopen(newname, 'w');
fprintf('%s\n', str)
fclose(f);
end
Mohammed Cha
Mohammed Cha 2020 年 12 月 22 日
編集済み: Mohammed Cha 2020 年 12 月 23 日
Dear Sir,
Thank you very much for your response. The code is working now properly. However, it is producing empty file instead of converted file. I think there is problem in this line below from main code
str = regexprep(mydata, {',','\.','(?<!\r)\n'}, {';',',','\r\n'});
myfilename = dir('*.csv');
allfilenames = {myfilename.name};
%get rid of ones that are already _ge.csv
mask = ~cellfun(@isempty, regexp(allfilenames, '_ge\.csv$', 'once'));
allfilenames(mask) = [];
numfiles= length(allfilenames);
for k = 1:numfiles
oldname = allfilenames{k};
newname = regexprep(oldname, '\.csv', '_ge.csv');
mydata = fileread(oldname);
str = regexprep(mydata, {',','\.','(?<!\r)\n'}, {';',',','\r\n'});
f = fopen(newname, 'w');
fprintf('%s\n', str)
fclose(f);
end
Thank you very much for your kind response.
Walter Roberson
Walter Roberson 2020 年 12 月 23 日
myfilename = dir('*.csv');
allfilenames = {myfilename.name};
%get rid of ones that are already _ge.csv
mask = ~cellfun(@isempty, regexp(allfilenames, '_ge\.csv$', 'once'));
allfilenames(mask) = [];
numfiles= length(allfilenames);
for k = 1:numfiles
oldname = allfilenames{K};
newname = regexprep(oldname, '\.csv', '_ge.csv');
mydata = fileread(oldname);
str = regexprep(mydata, {',','\.','(?<!\r)\n'}, {';',',','\r\n'});
f = fopen(newname, 'w');
fwrite(f, str);
fclose(f);
end
Mohammed Cha
Mohammed Cha 2020 年 12 月 23 日
Thank you very very much Sir. It works now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by