如何将带有轴子图的 Matlab 2014 代码重写到 2016 年?

How to rewrite this Matlab 2014 code with axis-subplots to 2016?

我为 Matlab 2014 编写的代码,但我想将其重写为 Matlab 2016,以便它变得更紧凑,因为它现在是多余的

hFig3=figure('Units', 'inches');
hax3_b1=axes('Parent', hFig3);
hax3_b2=axes('Parent', hFig3);
hax3_b3=axes('Parent', hFig3);
hax3_b4=axes('Parent', hFig3);

b1=subplot(2,2,1, hax3_b1); 
b2=subplot(2,2,2, hax3_b2); 
b3=subplot(2,2,3, hax3_b3); 
b4=subplot(2,2,4, hax3_b4);

% Example of common expression
set([b1 b2], 'Units', 'inches'); % 

u=0:0.1:1; y=sin(u); C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(b1, u, y, C); 
hold on; 
plot(b2, u'); 
histogram(b3, u');
histogram(b4, u);
hold off; 
drawnow; 

输出正常

OS: Debian 8.5 64 位
MATLAB:2014 但想转换为 2016

如果您只是想要更短的内容,可以去掉开头的大部分内容。你不需要 figure 但我把它作为一种习惯。

fh = figure;
x = 1:4;
b = arrayfun(@(y) subplot(2,2,y), x, 'UniformOutput',0);
b{1}.Units = 'inches';
b{2}.Units = 'inches';
u=0:0.1:1; y=sin(u); C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(b{1}, u, y, C); 
plot(b{2}, u'); 
histogram(b{3}, u');
histogram(b{4}, u);

你可以简单地写:

figure('Units','inches');
b1 = subplot(2,2,1); 
b2 = subplot(2,2,2); 
b3 = subplot(2,2,3); 
b4 = subplot(2,2,4);

或者最好,如果你想要一个轴数组:

figure('Units','inches');
b(1) = subplot(2,2,1);
b(2) = subplot(2,2,2);
b(3) = subplot(2,2,3);
b(4) = subplot(2,2,4);

或者使用一个简单的for循环:

figure('Units','inches');
b(1:4) = axes;
for k = 1:numel(b)
    b(k) = subplot(2,2,k);
end

在您选择的任何选项中都不需要所有 axes 命令。

这是您的所有 'demo' 代码:

b(1:4) = axes;
for k = 1:numel(b)
    b(k) = subplot(2,2,k);
end
set(b(1:2), 'Units', 'inches');
u=0:0.1:1; y=sin(u); C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(b(1), u, y, C); 
plot(b(2), u'); 
histogram(b(3), u');
histogram(b(4), u);

figure命令可能也没有真正的需要,这取决于如何处理它。