% % 读取数据 data = readmatrix('G:\19\19\2.xls'); % % % 生成标签 labels = repmat(1:14, 100, 1); labels = labels(:); % % 计算准确率 accuracy = zeros(14, 1); for i = 1:14 start_idx = (i-1)100 + 1; end_idx = i100; accuracy(i) = sum(data(start_idx:end_idx, 1) == labels(start_idx:end_idx)) / 100; end for i = 1:14 idx = find(data(:, 1) == i); correct = sum(data(idx, 2) == i); accuracy(i) = correct / length(idx); end % 生成混淆矩阵 confusionMat = confusionmat(labels, data(:, 2)); figure % newLabels = {'2FSK', 'BPSK', 'CW', 'LFM', 'LFM-BPSK', 'QPSK', 'NLFM', 'FSK-BPSK', 'LFM-FSK', 'MPSK', 'T1', 'T2', 'T3', 'T4'}; % confusionchart(confusionMat,newLabels ,'Normalization', 'row-normalized'); imagesc(confusionMat); colorbar; colormap(flipud(gray)); % 添加数值 textStrings = num2str(confusionMat(:),'%d'); textStrings = strtrim(cellstr(textStrings)); [x, y] = meshgrid(1:size(confusionMat,1), 1:size(confusionMat,2)); hStrings = text(x(:), y(:), textStrings(:), 'HorizontalAlignment', 'center'); midValue = mean(get(gca,'CLim')); textColors = repmat(confusionMat(:) > midValue, 1, 3); labels = {'2FSK', 'BPSK', 'CW', 'LFM', 'LFM-BPSK', 'QPSK', 'NLFM', 'FSK-BPSK', 'LFM-FSK', 'MPSK', 'T1', 'T2', 'T3', 'T4'}; set(gca, 'XTickLabel', labels); set(gca, 'YTickLabel', labels); set(hStrings, {'Color'}, num2cell(textColors, 2));怎么加标题
时间: 2023-07-16 09:12:34 浏览: 155
可以使用 `title` 函数来为图像添加标题,例如:
```matlab
title('Confusion Matrix');
```
这将在图像的顶部中心位置添加一个标题,标题内容为 'Confusion Matrix'。完整代码可能如下:
```matlab
% 读取数据
data = readmatrix('G:\19\19\2.xls');
% 生成标签
labels = repmat(1:14, 100, 1);
labels = labels(:);
% 计算准确率
accuracy = zeros(14, 1);
for i = 1:14
start_idx = (i-1)*100 + 1;
end_idx = i*100;
accuracy(i) = sum(data(start_idx:end_idx, 1) == labels(start_idx:end_idx)) / 100;
end
for i = 1:14
idx = find(data(:, 1) == i);
correct = sum(data(idx, 2) == i);
accuracy(i) = correct / length(idx);
end
% 生成混淆矩阵
confusionMat = confusionmat(labels, data(:, 2));
figure
imagesc(confusionMat);
colorbar;
colormap(flipud(gray));
% 添加数值
textStrings = num2str(confusionMat(:),'%d');
textStrings = strtrim(cellstr(textStrings));
[x, y] = meshgrid(1:size(confusionMat,1), 1:size(confusionMat,2));
hStrings = text(x(:), y(:), textStrings(:), 'HorizontalAlignment', 'center');
midValue = mean(get(gca,'CLim'));
textColors = repmat(confusionMat(:) > midValue, 1, 3);
% 添加标签
labels = {'2FSK', 'BPSK', 'CW', 'LFM', 'LFM-BPSK', 'QPSK', 'NLFM', 'FSK-BPSK', 'LFM-FSK', 'MPSK', 'T1', 'T2', 'T3', 'T4'};
set(gca, 'XTickLabel', labels);
set(gca, 'YTickLabel', labels);
set(hStrings, {'Color'}, num2cell(textColors, 2));
% 添加标题
title('Confusion Matrix');
```
阅读全文