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

65 ビュー (過去 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.
afrya
afrya 2015 年 1 月 29 日
Ok thanks

サインインしてコメントする。


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 Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by