How do I access the axes handles of a confusion matrix?

5 vues (au cours des 30 derniers jours)
pldr-bny
pldr-bny le 11 Avr 2019
Commenté : Adam Danz le 15 Avr 2019
Hi there, I am trying to plot something similar to the photo below (although clearly with a smaller font size), with the same axes labels (actual/predicted) and axes tick marks (class numbers):
The code I was implementing to generate the plot throws the following error when attempting to access the XLabel property of the Xaxis: 'No appropriate method, property or field'XLabel' for class 'matlab.ui.container.Menu'.
I think I am accessing the axes fields incorrectly using the following code:
figure(1)
plotconfusion(known_labels, predicted_labels)
fh = gcf;
ax = gca;
ax.FontSize = 8;
set(findobj(ax,'type','test'),'fontsize',3);
ah = fh.Children(2);
ah.XLabel.String = 'Actual';
ah.YLabel.String = 'Predicted';
ax.XTickLabel = class_labels;
ax.YTickLabel = class_labels;
title('Confusion Matrix 1', 'Fontsize',14)
set(gcf,'color','w');
hold off
Any help with how to do this correctly would be greatly appreciated. Thanks!

Réponses (1)

Adam Danz
Adam Danz le 11 Avr 2019
Modifié(e) : Adam Danz le 11 Avr 2019
You're accessing the XTickLabel field correctly but you're trying to enter a 'matlab.ui.container.Menu' object rather than a cell array of strings (or a numeric vector).
The XTickLabel controls the x tick mark labes. Look at the value of "class_labels" in your code (we don't have access to this). It doesn't specify tick labels.
Also, you could improve how you're getting the axis handle. The plotconfusion() function outputs the figure handle.
fh = plotconfusion(known_labels, predicted_labels);
fh.Children(2).XLabel.String = 'Actual';
fh.Children(2).YLabel.String = 'Predicted';
% Examples of x tick labels (for a matrix with 11 columns)
fh.Children(2).XTickLabel = {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k'};
fh.Children(2).XTickLabel = 200:210
  6 commentaires
pldr-bny
pldr-bny le 12 Avr 2019
Calling class(class_labels) returns 'cell' and the variable description is 71x1 cell array.
I tried converting class_labels to a vector of doubles but I am still getting the original error. When I pass {"01", "02" .....} directly to the XTickLabel object, again I get the original error.
Could there be anything else going wrong here?
Adam Danz
Adam Danz le 15 Avr 2019
This line below (from your penultimate comment) doesn't appear to be a cell array. Could you copy-paste the variable from the command window? How was this variable created? Maybe you could attach that variable to an mat file so I could look at it. The format of this line below is strange to me.
class_labels = {"01"} {"02"} {"03"} ...

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by