如何在for循环中保存子图?

问题描述 投票:0回答:1

使用此代码自动创建和保存子图(在 for 循环中)不起作用并抛出一个我不明白的错误:

clear
clc
close all

%----------------------------
x1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
x1 = x1 + r;

y1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
y1 = y1 + r;

z1 = [1,2,3;4,5,6;7,8,9];

LC = 0

for nn = 1:10
    LC = LC+1
figure()
subplot(2,1,1)
scatter(x1,y1)
legend('Data')
subplot(2,1,2)
xvalues_HM = {'True label 1','True label 2','True label 3'};
yvalues_HM = {'Mdl label 3','Mdl label 2','Mdl label 1'};
h = heatmap(xvalues_HM,yvalues_HM,z1);

%GET READY FOR TXTSAVE
txtsave = [num2str(LC)]; %%<--THIS IS TXTSAVE IT IS THE NAME I WANT TO GIVE TO THE FIGURE WHEN I SAVE IT
%TXTSAVE HAD BEEN DEFINED!!!

saveas(gcf,[ txtsave '.png']);
close all

end

我收到此错误消息:

Error using alternatePrintPath
Dimensions of arrays being concatenated are not consistent.

Error in alternatePrintPath

Error in print (line 83)
pj = alternatePrintPath(pj);

Error in saveas (line 181)
        print( h, name, ['-d' dev{i}] )

Error in script (line 396)
        saveas(gcf,[ txtsave '.png']);

这没有任何意义——它连接了什么数组?当我要求它保存图形(即不是子图)时,此代码有效。

使用 MATLAB R2020a 发现的问题。

matlab subplot
1个回答
0
投票

删除第一个图的图例现在会产生所需的子图,尽管带有未标记的数据。

© www.soinside.com 2019 - 2024. All rights reserved.