How to use regexp to read numbers preceded by text out of a data file
1 次查看(过去 30 天)
显示 更早的评论
Hello, i tried to get numbers out of a data file with regexp but i just cant get it to work.
The file is structured like this:
Time = 10001
smoothSolver: Solving for Ux, Initial residual = 1.660195e-07, Final residual = 3.2635415e-09, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.0010739333, Final residual = 1.1651965e-07, No Iterations 1
smoothSolver: Solving for Uz, Initial residual = 1.1728104e-06, Final residual = 2.3091287e-08, No Iterations 2
GAMG: Solving for p, Initial residual = 1.0923774e-05, Final residual = 7.2745974e-07, No Iterations 3
time step continuity errors : sum local = 0.34757443, global = 0.0029670501, cumulative = 0.0029670501
ExecutionTime = 0.92 s ClockTime = 3 s
i want to get out the values behind "Solving for Ux, Initial residual =", "Solving for Uy, Initial residual =", "Solving for Uz, Initial residual =", "Solving for p, Initial residual" and "ClockTime ="
i tried testing this with the following code:
S = fileread(path);
residuals = regexp(S,"(?<=ClockTime\s=\s)\d*");
This should find any number of consecutive digits following the string "ClockTime =" but instead it gives me the numbers 595 and 1193 which the file does not even contain as consecutive digits.
Thanks for any help :)
0 个评论
采纳的回答
Walter Roberson
2021-9-5
residuals = regexp(S,"(?<=ClockTime\s=\s)\d+", 'match');
The numbers you are getting back are the default outputs, which is the position relative to the start of the string.
Note: for your work you might want to use named tokens.
residuals = regexp(S,"(?<=residual\s =)(?<XR>)\S+).*(?<=residual\s =)(?<YR>)\S+).*(?<=residual\s =)(?<ZR>)\S+).*(?<=residual\s =)(?<PR>)\S+)(?<=ClockTime\s=\s)(?<CT>\d+)", 'names');
if all went well, if I did not make mistakes in the pattern, this would return a struct array with fields XR YR ZR PR CT each of which is a character vector, with XR corresponding to 'Solving for x', YR for the next line, ZR for the third line, PR for the GAMG line, and CT for the ClockTIme.
You would then
Nresiduals = structfun(@double, residuals, 'uniform', 0);
Ux = [Nresiduals.XR]; Uy = [Nresiduals.YR]; Uz = [NResiduals.ZR]; p = [NResiduals.PR];
clocks = [NResiduals.CT];
5 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!