how can I convert comma with decimal points for a lot of textfiles?

조회 수: 64 (최근 30일)
afrya
afrya 2015년 1월 25일
댓글: Walter Roberson 2019년 2월 3일
Hello,
I would like to import 10 textfiles which have comma as decimal delimiter.
All text file look like:
Time Fn Ft
0,1 1,2 5,9
0,2 1,8 9,4
0,3 1,9 10,8
Does anyone know how to do import those textfiles and convert comma with decimal points?
Thank you for your help

채택된 답변

per isakson
per isakson 2015년 1월 25일
편집: per isakson 2015년 1월 25일

추가 답변 (3개)

Stephen23
Stephen23 2015년 1월 25일
편집: Stephen23 2015년 12월 14일
MATLAB high-level data reading functions (eg textscan, csvread , etc) only accept the period character (.) as the decimal radix point. This also applies to sscanf and the other string->numeric conversion functions.
Basically you need to convert the comma to a period before converting the strings to numerics. You can do this:
  • externally in your favorite text editor, or
  • within MATLAB by
  1. reading the whole string using fileread
  2. performing a regexprep or strrep operation
  3. doing the numeric conversion using textscan or your function of choice.
Because the every file format is different and the presence of periods can make this complicated, you need to know exactly how the data is formatted before you can try this.

Geoff Hayes
Geoff Hayes 2015년 1월 25일
Afrya - if I copy and paste your three line example into a file called data.txt as
0,1 1,2 5,9
0,2 1,8 9,4
0,3 1,9 10,8
I can then use the importdata function to read all the data into a cell array (cell array because your input data comes in as strings due to the comma) with the code
data = importdata('data.txt','\t')
In the above, I'm assuming that each column is separated by a tab. Now, I just loop through each row in the matrix and replace the commas with a period as
numericData = [];
for k=1:size(data,1)
numericData(k,:) = str2num(char(strrep(data(k,:),',','.')'));
end
with numericData being populated as
numericData =
0.1000 1.2000 5.9000
0.2000 1.8000 9.4000
0.3000 1.9000 10.8000
Try the above and see what happens! (Note that if you have a header row in your data file, you should be able to ignore it. See importdata .
  댓글 수: 5
Geoff Hayes
Geoff Hayes 2015년 1월 26일
편집: Geoff Hayes 2015년 1월 29일
Afrya - there appears to be nine header rows that you will want to ignore when importing the data. See the documentation for importdata that will describe how you can ignore these lines.

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


Roland Antal
Roland Antal 2019년 2월 3일
I know it's too late to answer that question because it's actually four years later but I found the solution right now, so maybe will still help others who will search for solution in future. Une notepad++ Ctrl+F find comma and than is there a replace all function which helps us. Good luck everybody!
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 2월 3일
This is the option Stephen suggested, "externally in your favorite text editor,"

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by