How to use regexp to read numbers preceded by text out of a data file

5 views (last 30 days)
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.
I read this already but it is not really helping me.
Thanks for any help :)

Accepted Answer

Walter Roberson
Walter Roberson on 5 Sep 2021
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 Comments
Jean Volkmar
Jean Volkmar on 6 Sep 2021
Edited: Jean Volkmar on 6 Sep 2021
Okay so i also had a go at experimenting, in the end i did not get it to work as nicely but better than nothing.
Here is what i did:
path(1) = "";
residuals = cell(length(path),5); %Preallocate
for j = 1:1:length(path)
S = fileread(path(j)); %read file
residuals{j,1} = regexp(S,"(?<=Ux,\sInitial\sresidual\s=\s)[^,]+","match"); %Find a number of consecutive charakters after a matching string "Ux, Initial residual = " 1 or more times consecutively, and exclude the comma.
residuals{j,2} = regexp(S,"(?<=Uy,\sInitial\sresidual\s=\s)[^,]+","match"); %Find a number of consecutive charakters after a matching string "Uy, Initial residual = " 1 or more times consecutively, and exclude the comma.
residuals{j,3} = regexp(S,"(?<=Uz,\sInitial\sresidual\s=\s)[^,]+","match"); %Find a number of consecutive charakters after a matching string "Uz, Initial residual = " 1 or more times consecutively, and exclude the comma.
residuals{j,4} = regexp(S,"(?<=p,\sInitial\sresidual\s=\s)[^,]+","match"); %Find a number of consecutive charakters after a matching string "p, Initial residual = " 1 or more times consecutively, and exclude the comma.
residuals{j,5} = regexp(S,"(?<=ClockTime\s=\s)\d+","match"); %Find a number of consecutive digits after a matching string "ClockTime = " 1 or more times consecutively, and exclude the comma.
end
After that i saved the numbers into an array like this:
ux = zeros(length(path),length(residuals{1})); %Preallocate
for k = 1:1:length(path)
for j = 1:1:length(residuals{1})
ux(k,j) = str2double(residuals{k,1}(1,j)); %Convert string inside the cell to double
end
end

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!