# Extracting data from confusion matrix fig

15 views (last 30 days)
M Umair on 22 May 2022
Hi, How can i extract the data from a confusion matrix fig file?
M Umair on 26 May 2022
Hello Bala,
The figure contains confusion matrix (file attached). I want to extract all numerical values presented in the figure excluding the percentages.
Thank you

Chunru on 26 May 2022
Edited: Chunru on 26 May 2022
f = openfig('SqueezeNet-CM', 'visible'); h = findobj(f.Children(2).Children, 'Type', 'Text');
data = zeros(length(h), 1);
for i=1:length(h)
data(i) = sscanf(h(i).String, '%f');
end
m = 5;
data = reshape(data, [2*m m]);
data_number = data(4:2:end, 2:end)
data_number = 4×4
115 0 0 2 769 447 174 90 107 753 1020 681 209 0 6 427
data_number = data_number(end:-1:1, end:-1:1)
data_number = 4×4
427 6 0 209 681 1020 753 107 90 174 447 769 2 0 0 115

Bala Tripura Bodapati on 26 May 2022
Hi Umair,
It is my understanding that you would like to extract the numeric values from confusion matrix plotted on a figure.
From the confusion matrix plotted using 'plotconfusion' function, there is no method to extract the numeric values.
Alternatively, you can plot the confusion matrix using the 'confusionchart' function on a figure and then extract the numeric values.
The following code illustrates the same:
X = meas;
Y = species;
Mdl = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1);
predictedY = resubPredict(Mdl);
fig=figure;
confusionchart(Y,predictedY);
'cm' is a confusion chart object. The 'NormalizedValues' property of this object outputs the numeric matrix:
matrix=cm.NormalizedValues
Refer the Matrices documentation for more information on accessing elements from a matrix.
Also, refer the confusionchart documentation for more information on confusion chart and graphics hierarchy documentation for more information on accessing figure object and it's children.