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

조회 수: 11(최근 30일)
Alberto Mora
Alberto Mora 2019년 6월 7일
댓글: Alberto Mora 2021년 2월 12일
I want to import a .csv data file with comma both for decimal place symbol and as system for divide the column, see below.
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.

채택된 답변

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
  댓글 수: 2
Alberto Mora
Alberto Mora 2021년 2월 12일
Thank you for your suggestion Stephen! Your method is more efficent.

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

추가 답변(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",...
for i = 1:numel(var)
res = [res cellfun(@(x)strrep(x,',','.'), var(i), 'Uni',0)];



Community Treasure Hunt

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

Start Hunting!

Translated by