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

64 views (last 30 days)
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

Accepted Answer

per isakson
per isakson on 25 Jan 2015
Edited: per isakson on 25 Jan 2015

More Answers (3)

Stephen23
Stephen23 on 25 Jan 2015
Edited: Stephen23 on 14 Dec 2015
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 on 25 Jan 2015
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 Comments
Geoff Hayes
Geoff Hayes on 26 Jan 2015
Edited: Geoff Hayes on 29 Jan 2015
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.

Sign in to comment.


Roland Antal
Roland Antal on 3 Feb 2019
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!

Community Treasure Hunt

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

Start Hunting!