How to extract multiple number data after the same specific string from a .txt file?

1 visualización (últimos 30 días)
Hello,
I am trying to extract data following a specific string, and write them into an a matrix with the iteration number (timestep) in the first column, the extracted values in the 2nd column. The file I am wanting to analyse looks something like this:
Time = 1
smoothSolver: Solving for Ux, Initial residual = 0.515984, Final residual = 0.0471902, No Iterations 2
[...a bunch of irrelevant lines with similar output...]
smoothSolver: Solving for k, Initial residual = 1, Final residual = 0.0843728, No Iterations 2
ExecutionTime = 89.492 s ClockTime = 89 s
forceCoeffs forceCoeffs1 write:
Cm = 0.000726308
Cd = 0.0881087
Cl = 0.000216161
Cl(f) = 0.000834389
Cl(r) = -0.000618227
forces forces1 write:
sum of forces:
pressure : (0.245894 0.00235346 0.00322009)
viscous : (1.10938 0.000136965 0.000104868)
porous : (0 0 0)
sum of moments:
pressure : (-0.000279174 -3.13266e-005 -0.00145534)
viscous : (5.74234e-006 0.0078517 -2.88286e-005)
porous : (0 0 0)
Time = 2
smoothSolver: Solving for Ux, Initial residual = 0.0530995, Final residual = 0.00479832, No Iterations 4
[...a bunch of irrelevant lines with similar output...]
smoothSolver: Solving for k, Initial residual = 0.836478, Final residual = 7.78302e-008, No Iterations 1
ExecutionTime = 118.255 s ClockTime = 118 s
forceCoeffs forceCoeffs1 write:
Cm = 0.000298873
Cd = 0.0552984
Cl = 0.00031785
Cl(f) = 0.000457798
Cl(r) = -0.000139948
forces forces1 write:
sum of forces:
pressure : (0.369834 -0.00104592 0.00489057)
viscous : (0.480758 3.26764e-005 -1.44574e-006)
porous : (0 0 0)
sum of moments:
pressure : (-0.000441976 -6.86051e-005 -0.00251666)
viscous : (1.21833e-005 0.00328666 2.64037e-005)
porous : (0 0 0)
...
There will be hundreds lines of similar format as these 2 timesteps, and what I would like to do is, for example to extract all the 'Cm' data after the string 'Cm =', and assign them in to a matrix as:
[1 0.000726308
2 0.000298873
3 ...
...]
Then ultimately be able to take average of any given time intervals.
I have read some answers to similar questions on the forum mentioning the use of functions like 'regexp' or 'textscan', but have not quite figured out how they could be implemented in this case.
Any help would be very much appreciated, thank you!
Kind regards,
Nian

Respuesta aceptada

Spencer Chen
Spencer Chen el 13 de Feb. de 2020
Something like this should get you started:
txt = fileread('test.txt');
txt = splitlines(txt);
for ii = 1:numel(txt)
if startsWith(txt{ii},' Cm = ')
val = sscanf(txt{ii},' Cm = %f')
end
end
I'll leave you to deal with the extracted values youself.
Blessings,
Spencer
  1 comentario
Nian Liu
Nian Liu el 19 de Feb. de 2020
Hello Spencer, really appreciate your quick reply! As I'm really new to code, it took me a little while to understand what each line was doing, but now I've got it to work, and managed to adapt it to some other data extraction cases. Thank you! :) -Nian

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Biotech and Pharmaceutical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by