想要的函数在matlab函数库里没有

本人目前用的是matlab R2016a版本,但是没有confusionchart这个函数,不太清楚是什么原因,有没有伙伴给我个m文件,可以直接复制粘贴在matlab目录里,让matlab可以使用的?还是必须下载更高的版本才能用这个函数呢?

confusionchart函数是在MATLAB R2017b版本中引入的,因此在你的MATLAB R2016a版本中是没有这个函数的。如果你想使用这个函数,可以考虑升级到最新版本的MATLAB或者使用其他方法来绘制混淆矩阵。

混淆矩阵是用于评估分类器性能的常用工具。在MATLAB R2016a中,你可以使用下面的代码来绘制混淆矩阵:

% 假设你有预测值和真实值的向量predictions和labels

% 计算混淆矩阵
C = confusionmat(labels, predictions);

% 绘制混淆矩阵
figure;
imagesc(C);
colorbar;

% 设置坐标轴标签
set(gca, 'XTick', 1:size(C,2));
set(gca, 'YTick', 1:size(C,1));
xlabel('Predicted Class');
ylabel('True Class');

% 设置标签
textStrings = num2str(C(:), '%0.2f');
textStrings = strtrim(cellstr(textStrings));
[x,y] = meshgrid(1:size(C,2), 1:size(C,1));
hStrings = text(x(:), y(:), textStrings(:), ...
                'HorizontalAlignment', 'center');
midValue = mean(get(gca, 'CLim'));
textColors = repmat(C(:) > midValue, 1, 3);
set(hStrings, {'Color'}, num2cell(textColors, 2));

% 设置坐标轴范围
axis([0 size(C,2)+1 0 size(C,1)+1]);


这段代码会绘制一个带有数值的混淆矩阵图,其中数值表示真实类别和预测类别之间的匹配数。你也可以使用类似的方法来自定义混淆

img

2022a中文件 confusionchart.m


function h = confusionchart(varargin)
%CONFUSIONCHART Plot a confusion matrix
%
%   CONFUSIONCHART(m) plots a confusion matrix. The matrix m must be a 
%   valid confusion matrix, that is, m must be a square matrix and its 
%   elements must be positive integers. The element m(i,j) is the number of
%   times an observation in the ith true class was predicted to be in the 
%   jth class. Each colored cell of the chart corresponds to a single
%   element of the confusion matrix.
%
%   CONFUSIONCHART(m, labels) plots a confusion matrix using the
%   matrix m and the array of class names labels. Each element of labels
%   corresponds to the name of a single class. The labels array must be the
%   same size as the confusion matrix m, and can be a categorical, numeric,
%   logical, character or string vector, or a cell array of character
%   vectors.
%
%   CONFUSIONCHART(trueLabels, predictedLabels) calculates and plots a
%   confusion matrix from the vectors of class labels trueLabels and
%   predictedLabels. Each element of the vectors corresponds to a single
%   observation. The vectors of class labels can be categorical, numeric,
%   logical, character or string arrays, or cell arrays of character
%   vectors.
%
%   CONFUSIONCHART(___, Name, Value) specifies additional options for
%   the plot using one or more name-value pair arguments. Specify the
%   options after all other input arguments.
%
%   CONFUSIONCHART(parent, ___) creates the plot in the figure, panel, 
%   or tab specified by parent.
%
%   cm = CONFUSIONCHART(___) returns the ConfusionMatrixChart object. 
%   Use cm to modify properties of the chart after creating it.

% Copyright 2017-2019 The MathWorks, Inc.

narginchk(1, Inf);

try
    % Check input args are valid, throw error here if they're not.
    [parent, model, cellOfKeyValuePairs] = iParseInput(varargin{:});
    
    % If the user hasn't specified an 'OuterPosition' value, we try and
    % replace the existing axes (if one exists). If they have specified a
    % 'Position', 'InnerPosition' or 'OuterPosition' value, we make a new
    % chart and leave any existing axes alone.
    if ~iHasPositionArg(cellOfKeyValuePairs)
        constructorFcn = @(varargin)(mlearnlib.graphics.chart.ConfusionMatrixChart(...
            varargin{:}, 'Model', model, cellOfKeyValuePairs{:}));
        
        % If the user hasn't defined a parent, parent will be empty. This
        % will be handled correctly by prepareCoordinateSystem.
        cm = matlab.graphics.internal.prepareCoordinateSystem('mlearnlib.graphics.chart.ConfusionMatrixChart', parent, constructorFcn);
    else
        % If the user hasn't defined a parent, we need to get one now.
        if isempty(parent)
           parent = gcf(); 
        end

        cm = mlearnlib.graphics.chart.ConfusionMatrixChart('Parent', parent, 'Model', model, cellOfKeyValuePairs{:});
    end
    
    fig = ancestor(cm, 'Figure');
    if isscalar(fig)
        fig.CurrentAxes = cm;
    end
catch e
    throw(e);
end

% Prevent outputs when not assigning to variable.
if nargout > 0
    h = cm; 
end

end

function [parent, model, cellOfKeyValuePairs] = iParseInput(varargin)
% Parse input to standard form. We return the parent separately (even
% though it's included in the args) so we can use it in
% matlab.graphics.internal.prepareCoordinateSystem.

parentValidator = mlearnlib.internal.confusionmatrixchart.input.ParentValidator();
syntaxInterpreter = mlearnlib.internal.confusionmatrixchart.factories.SyntaxInterpreter();

parser = mlearnlib.internal.confusionmatrixchart.input.InputParser(parentValidator, syntaxInterpreter);

[parent, model, cellOfKeyValuePairs] = parser.parse(varargin{:});
end

function tf = iHasPositionArg(cellOfKeyValuePairs)
% Returns true if the name-value pairs contain a 'Position' name of some
% sort.

propNames = cellOfKeyValuePairs(1:2:end);
tf = any(strcmpi(propNames, 'Position')) || ...
    any(strcmpi(propNames, 'InnerPosition')) || ...
    any(strcmpi(propNames, 'OuterPosition'));
end

望采纳。
在你的版本中,有confusionmat函数,也可以用于构建混淆矩阵。

% Define some true and predicted labels
trueLabels = [1 2 1 2 2 1];
predictedLabels = [1 2 1 1 2 1];

% Create a confusion matrix
C = confusionmat(trueLabels, predictedLabels);

% Plot the confusion matrix as a chart
plotconfusion(trueLabels, predictedLabels)

confusionchart是在 R2018b 中引入的。由于你使用的是 R2016a,因此它不可用。更新到 R2018b 以使用它

MATLAB图像处理基础
https://blog.csdn.net/TxyITxs/article/details/112779460