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

2 次查看(过去 30 天)
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

采纳的回答

Alberto Mora
Alberto Mora 2019-6-7
编辑:Alberto Mora 2021-2-10
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 个评论
Stephen23
Stephen23 2021-2-12
str = fileread('myfile.txt');
str = regexprep(str,{'(\d),(\d)','"?,?"'},{'$1.$2',' '});
mat = sscanf(str,'%f',[5,Inf]).' % more efficient than STR2NUM in a loop
mat = 4×5
0 0 0.9025 0 0.0342 0.0500 0 0.9000 0 0.0341 0.1000 0.0003 0.9000 0.0019 0.0341 0.1500 0.0013 0.9025 0.0074 0.0342

请先登录,再进行评论。

更多回答(1 个)

Debasish Samal
Debasish Samal 2019-6-7
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

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by