Reading a single numerical value from regexp return

3 Ansichten (letzte 30 Tage)
Luke
Luke am 24 Mär. 2023
Kommentiert: Luke am 25 Mär. 2023
I'm searching though text logs and recoridng the number of errors that occur. I want to take the error number after finding it and save it to a variable from the cell array but can't seem to get numerical value out of the cell aray.
The error log looks like the following
Success
Error 303
Success
Completed
Error 301
File
Here is what my code look like at the moment. I get an error when tring to compare the error code to cell array contents.
A = importdata('history_log_28.txt');
numLines = length(A);
Error_301_Count = 0;
Error_302_Count = 0;
Error_303_Count = 0;
for i= 1:numLines
GrabLine = A(i,:);
Error_Catch = regexp(GrabLine,'Error (\w*)','tokens');
if (Error_Catch{1} == '303')
Error_303_Count = Error_303_Count + 1;
end
end
  1 Kommentar
Stephen23
Stephen23 am 24 Mär. 2023
Bearbeitet: Stephen23 am 24 Mär. 2023
"I want to take the error number after finding it and save it to a variable from the cell array but can't seem to get numerical value out of the cell aray. "
There is no numeric value in that cell array, for the very simple reason that REGEXP does not return numeric values from the input text, it returns text. If you want to convert text to numeric, then you can use the usual approaches, e.g. STR2DOUBLE or SSCANF or the like.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Mär. 2023
Bearbeitet: Stephen23 am 24 Mär. 2023
Get rid of the loop and process the entire file at once. Line-by-line just makes it more complex.
You can get rid of one layer of cell arrays by using a look-behind instead of the token. That simplifies the post-processing.
Don't reinvent the wheel (binning) when it already exists in one efficient function.
txt = fileread('test.txt')
txt =
'Success Error 303 Success Completed Error 301 File Error 303 hello world'
tmp = regexp(txt,'(?<=Error\s*)\d+','match');
vec = str2double(tmp) % errors found in the file
vec = 1×3
303 301 303
cnt = histcounts(vec,[301:303,Inf]) % count 301, 302, 303.
cnt = 1×3
1 0 2
  3 Kommentare
Stephen23
Stephen23 am 24 Mär. 2023
Bearbeitet: Stephen23 am 24 Mär. 2023
"I was implementing the loop so I could also keep track of where the error occured in the timeline via a counter."
txt = fileread('test.txt')
txt =
'Success Error 303 Success Completed Error 301 File Error 303 hello world'
[tmp,spl] = regexp(txt,'(?<=Error\s*)\d+','match','split');
% the same in my answer... followed by:
dst = cellfun(@numel,regexprep(spl,'[^\n]','')) % distances
dst = 1×4
1 3 2 1
DST gives the distance from:
  • the start of the text to the 1st error
  • from the 1st error to the 2nd error
  • from the 2nd error to the 3rd error
  • etc etc
  • from the Nth error to the start of the remaining text.
Note that effectively counts the newlines, so it incudes the error lines as well. You might find CUMSUM useful.
Luke
Luke am 25 Mär. 2023
Thank you for the help! I'll take a look at what I have and figure out how to move forward.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by