Hello Luis, I hope you are having a wonderful day!
Yes, I understand that you are using MATLAB’s Unit Test Framework and want to retrieve the indices of the data points that caused the test to fail. This can be done easily by writing a simple script to extract the indices from the diagnostic report from “results.Details.DiagnosticRecord.Report.” You may follow the steps below for more understanding:
1. Writing the test (I am using a dummy test just for demonstration)
classdef testMySignal < matlab.unittest.TestCase
methods (Test)
function testSignalIsLow(testCase)
signal = zeros(1, 10000); % All zeros
signal([1198, 10798, 10799]) = 0.01; % Introduce a few small nonzero values
% Test: Check all values <= 1e-6
testCase.verifyLessThanOrEqual(signal, 1e-6);
end
end
end
2.Execute the test using following command in the command window:
results = runtests('testMySignal');
3. Write a function in a separate function file to extract the indices:
function failingIndices = getFailingIndicesFromReport(reportStr)
% Extract "Failing Indices" line using regular expression
tokens = regexp(reportStr, 'Failing Indices:\s*([^\n]*)', 'tokens');
if ~isempty(tokens)
idxStr = tokens{1}{1};
failingIndices = sscanf(idxStr, '%d').';
else
failingIndices = [];
end
end
4. Get the failing indices by using the diagnostic report from the MATLAB command window:
reportStr = results.Details.DiagnosticRecord.Report;
failingIndices = getFailingIndicesFromReport(reportStr);
disp(failingIndices)
You may refer to the attached images for further reference.

