# Import data with column separated with a comma, and comma as decimal place symbol

9 views (last 30 days)
Alberto Mora on 7 Jun 2019
Commented: Alberto Mora on 12 Feb 2021
Hello
I want to import a .csv data file with comma both for decimal place symbol and as system for divide the column, see below.
0,"0","0,9025","0","0,0341856"
0,05,"0","0,9","0","0,03409091"
0,1,"0,0003333325","0,9","0,001851847","0,03409091"
0,15,"0,001333333","0,9025","0,007407404","0,0341856"
How can I import the data? Matlab do not recognize the difference between the comma of the decimal place and the comma of the comulm deviders.
Regards
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Alberto Mora on 7 Jun 2019
Edited: Alberto Mora on 10 Feb 2021
I developed the following code, reading and modifying the comma and other symbol in the text.
fid = fopen([PathAndFileName,'.csv'],'r'); % Open file
C = textscan(fid, '%s'); % Read file
fclose(fid); % Close file
C=strrep(C{1,1},'","',' '); % Modify "," into a space
C=strrep(C,'"',''); % Delete remaining "
C=strrep(C,',','.'); % From comma to point decimal simbol
Data=zeros(numel(C),length(str2num(C{end,1}))); % Initialize matrix
for n = 1:numel(C)
Data(n,:) = str2num(C{n,1}); % Divide all rows into the columns
end
##### 2 CommentsShowHide 1 older comment
Alberto Mora on 12 Feb 2021
Thank you for your suggestion Stephen! Your method is more efficent.

Sign in to comment.

### More Answers (1)

Debasish Samal on 7 Jun 2019
One thing that you can do here is import the data and save it in a variable say 'var'. Then replace the commas by a decimal point.
var =[ '0',"0","0,9025","0","0,0341856",...
'0','05',"0","0,9","0","0,03409091",...
'0','1',"0,0003333325","0,9","0,001851847","0,03409091",...
'0','15',"0,001333333","0,9025","0,007407404","0,0341856"];
for i = 1:numel(var)
res = [res cellfun(@(x)strrep(x,',','.'), var(i), 'Uni',0)];
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Data Type Conversion in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!