how can I convert comma with decimal points for a lot of textfiles?
31 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
afrya
el 25 de En. de 2015
Comentada: Walter Roberson
el 3 de Feb. de 2019
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
0 comentarios
Respuesta aceptada
Más respuestas (3)
Stephen23
el 25 de En. de 2015
Editada: Stephen23
el 14 de Dic. de 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
- reading the whole string using fileread
- performing a regexprep or strrep operation
- 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
el 25 de En. de 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 comentarios
Geoff Hayes
el 26 de En. de 2015
Editada: Geoff Hayes
el 29 de En. de 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.
Roland Antal
el 3 de Feb. de 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!
1 comentario
Walter Roberson
el 3 de Feb. de 2019
This is the option Stephen suggested, "externally in your favorite text editor,"
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!