Scanning for floats in different formats

조회 수: 2 (최근 30일)
Marcel Staniszewski
Marcel Staniszewski 2022년 9월 28일
편집: Adam Danz 2022년 9월 29일
I am looking for a robust solution to extract all numbers from raw data files coming in varying formats (see below). My code is currently scanning through each line of the raw data file and extracting all floats by using textscan(line, '%f').
This method works fine for a data that looks like this:
Cyclic Acquisition Time: 28.107748 Sec 10/3/2017 5:34:26 PM
A B C
mm N segments
0.43347636 275.24789 198
0.054932408 27.816936 199
0.43500644 276.26346 200
0.052249052 26.64908 201
...
But doesnt work for data like that:
"Elapse Time ","Disp ","Disp ","Load 1 ","Load 1 ","T2_Chamber","T2_Chamber","Axial cmd","Axial cmd",
"Sec ","mm ","mm ","N ","N ","C ","C ","V ","V ",
0.00000 , -0.486, -2.425, 18.22, -150.81, 91.0, 90.0, 0.300, -5.303,
0.00000 , -0.289, -0.336, 9.30, -17.66, 90.6, 89.9, 0.641, 0.534,
33.36000 , -0.630, -2.395, -15.02, -149.87, 91.0, 90.0, 0.081, -5.250,
33.36000 , -0.630, -2.395, -14.98, -150.05, 90.6, 89.9, 0.082,
...
Thanks in advance!
Marcel

채택된 답변

Adam Danz
Adam Danz 2022년 9월 28일
편집: Adam Danz 2022년 9월 29일
The second data set uses a comma delimiter and you can ignore the top two lines by specifying the HeaderLine property. I've tested this with a copy-pasted version of your second data set.
fid = fopen('mydata.txt');
t = textscan(fid, '%f', 'Delimiter', ',', 'HeaderLines', 2)
  댓글 수: 2
Marcel Staniszewski
Marcel Staniszewski 2022년 9월 29일
편집: Marcel Staniszewski 2022년 9월 29일
Thank you very much! I overlooked the delimiter property for textscan...
On another note, ist there a specific function to determine the delimiter symbol without user input?
Edit: I found a solution that works for me - thank you!
Walter Roberson
Walter Roberson 2022년 9월 29일
readmatrix() should autodetect delimiter

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

추가 답변 (0개)

카테고리

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