Counting the amount of times a specific error appears

1 view (last 30 days)
I am currently writing a script that opens a file directory, and then reads through each of the reports. The reports have the exact same structureand give True/False results.
I want to be able to identify when there is a 'False' and give the line number where it is found [this I have accomplished with my current code]
Depending on which line the 'false' is found I want to link it to a specific error code
Then after all of the files have been read I want to see the total amount of times that a specific error code appeared
myFolder = 'C:\file\location';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.m'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
fid = fopen(fullFileName);
tline = fgetl(fid);
linecounter = 0;
%tests that you want to track the number of fails of
noisefailcounter=0;
spectrumfailcounter=0;
while ischar(tline)
%disp(tline) %if you want to display the whole file that is being read
%identifies when there is a fail in any given line
if contains(tline,'FALSE')
disp(tline)
i=1;
else
i=0;
end
%depending in which line the fail occurs it is correlated to a specififc test that failed
if linecounter==3 || i==1
noisefailcounter=noisefailcounter+1;
end
if linecounter==8 || i==1
spectrumfailcounter=spectrumfailcounter+1;
end
linecounter = linecounter+1;
tline=fgetl(fid);
end
TotalNoiseFail=noisefailcounter
TotalSpectrumFail=spectrumfailcounter
fclose(fid);
end
  1 Comment
Nicolas B
Nicolas B on 21 Feb 2025
Edited: Nicolas B on 21 Feb 2025
Have you considered using patterns to scrap your files?
If your file contains only true/false lines, then there is the option to load a whole file (if they are not too long, otherwise, read it line per line), then split per line using splitlines() and then detect which lines contains true/false using contains() and aggregate your results.
If your file is structures (with for example test names), uses simply extract() to get your results.

Sign in to comment.

Answers (1)

dpb
dpb on 21 Feb 2025
It would be easier to be sure solve the problem if you would attach a sample file, but...
...
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.m'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
noisefailcounter=0;
spectrumfailcounter=0;
for k = 1:numel(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading %s\n', fullFileName);
data=readlines(fullFileName); % return whole file as string array
isFalse=contains(data,'FALSE'); % logical array of FALSE-containing records
ixFalse=find(isFalse); % and the locations in the file
noisefailcounter=noisefailcounter+(ixFalse==3); % increment noise found counter
spectrumfailcounter=spectrumfailcounter+(ixFalse==8); % ditto for spectrum
end
TotalNoiseFail=noisefailcounter;
TotalSpectrumFail=spectrumfailcounter;

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!