Extracting Time Data from Text File
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Simon Montrose
el 6 de Feb. de 2022
Comentada: Simon Montrose
el 7 de Feb. de 2022
In short, I've been trying to put together a script that scans a .txt file and pulls number values from it. The context is Rubik's Cube times; I have a program that produces .txt files from sessions, and the contents include somee basic statistics at the top, a list of times, and the moves used to scramble the cube for each time. I've included an example of the beginning of one of these files below:
Average: 10.05
Best: 9.34
Worst: 14.26
Mean: 10.75
Standard Deviation: 1.79
1: 9.91 D2 L2 B2 L2 F2 D L2 B2 D' U L' U' L D' L F' R' B' U B2
2: 9.86 R2 U2 B2 F2 D L2 D' U' R2 U' B2 R U F' U' B' D B2 U R' D2 F2
3: (14.26) D2 L2 B' L2 B2 D2 L2 F2 D2 F R' D B R2 B' D' R2 B2 F L
4: (9.34) B F U2 B R2 B' U2 F' D2 R' D' B2 D' B' U2 R' B' D' B'
5: 10.39 B F' U2 B2 L2 U2 R2 B' R2 B U B2 R2 D2 R2 B' L B R D2
My goal is to pull the times from these files to run my own stats on them, but there are a lot of nuances that have made this difficult!
The biggest issues I've had:
1) As you can see, some of the times contain 3 sig figs, and some contain 4. I haven't found a clean way to pull both from the file at the same time.
2) I haven't found a way to ignore the statistics at the top (if this is too much for a script, it would be easy to just delete that section of the text file before I run the script.
What I have so far isn't pretty, but so far this has successfully created an array of the 4-digit times for me:
fileID = fopen("txt Files/text-4AB30AA43EF2-1.txt")
raw = fscanf(fileID,"%c")
fclose(fileID)
num = '[0123456789][0123456789].[0123456789][0123456789]';
out = regexp(raw,num,'match');
gen1 = str2double(out);
list = gen1'
Any help would be greatly appreciated, thank you!
0 comentarios
Respuesta aceptada
Stephen23
el 7 de Feb. de 2022
Editada: Stephen23
el 7 de Feb. de 2022
Simpler and more efficient:
str = fileread('test_1.txt');
tkn = regexp(str,'^\s*(\d+):\D+(\d+\.?\d*)\W+([^\n\r]+)', 'tokens', 'lineanchors');
tkn = vertcat(tkn{:})
mat = str2double(tkn(:,1:2))
Optional extras:
% tkn(:,1:2) = num2cell(mat); % optional, but it is much better to store numeric
% data in a numeric array, so probably best avoided.
spl = regexp(tkn(:,3),'\w+','match')
spl{:}
5 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Standard File Formats 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!