如何强制MATLAB功能区保持图

时间:2015-07-25 16:33:24

标签: matlab matlab-figure

我正在研究这个获取轴处理程序和数据的函数,并且应该在轴上正确绘制它。该函数在for循环中调用。它应该在一个图中绘制多个数据。我的结果如下所示。

power spectrum graphs

只有两个正确绘制的图形(具有四种颜色的图形)。其他人错过了在最终区域之前绘制的区域(红色区域是每个图形中的最后一个绘制区域)。但是每个轴的脚本都是相同的。那么哪里可能是错误?整个功能写在下面。

function [] = powerSpectrumSmooth(axis,signal,fs)

N= length(signal);
samplesPer1Hz = N/fs;
delta = int16(3.5*samplesPer1Hz); %last sample of delta frequncies
theta = int16(7.5*samplesPer1Hz); %last sample of theta frequncies
alpha = int16(13*samplesPer1Hz); %last sample of alpha frequncies
beta = int16(30*samplesPer1Hz); %last sample of beta frequncies

x=fft(double(signal));
powerSpectrum = 20*log10(abs(real(x)));
smoothPS=smooth(powerSpectrum,51);

PSmin=min(powerSpectrum(1:beta));

y1=[(smoothPS(1:delta)); zeros(beta-delta,1)+PSmin];
y2=[zeros(delta-1,1)+PSmin; (smoothPS(delta:theta)); zeros(beta-theta,1)+PSmin];
y3=[zeros(theta-1,1)+PSmin; (smoothPS(theta:alpha)); zeros(beta-alpha,1)+PSmin];
y4=[zeros(alpha-1,1)+PSmin; (smoothPS(alpha:beta))];


 a1=area(axis,1:beta,y1);
 set(a1,'FaceColor','yellow')
 hold on

 a2=area(axis,1:beta,y2);
 set(a2,'FaceColor','blue')

 a3=area(axis,1:beta,y3);
 set(a3,'FaceColor','green')

 a4=area(axis,1:beta,y4);
 set(a4,'FaceColor','red')

ADDED

这是调用上述函数的函数。

function [] = drawPowerSpectrum(axesContainer,dataContainer,fs)

size = length(axesContainer);

for l=1:size

    powerSpectrumSmooth(axesContainer{l},dataContainer{l},fs)

    set(axesContainer{l},'XTickLabel','')
    set(axesContainer{l},'YTickLabel','')

    uistack(axesContainer{l}, 'top');

end

7月29日增加

这是一个重现错误的脚本,因此您可以在计算机中运行它。在再次运行之前,您可能需要清除变量。

len = 9;

axesContainer = cell(len,1);
x = [0.1,0.4,0.7,0.1,0.4,0.7,0.1,0.4,0.7];
y = [0.1,0.1,0.1,0.4,0.4,0.4,0.7,0.7,0.7];

figure(1)

for i=1:len
    axesContainer{i} = axes('Position',[x(i),y(i),0.2,0.2]);
end

dataContainer = cell(len,1);
N = 1500;

for i=1:len
        dataContainer{i} = rand(1,N)*100;
end

for l=1:len

    y1=[(dataContainer{l}(1:N/4)) zeros(1,3*N/4)];
    y2=[zeros(1,N/4) (dataContainer{l}(N/4+1:(2*N/4))) zeros(1,2*N/4)];
    y3=[zeros(1,2*N/4) (dataContainer{l}(2*N/4+1:3*N/4)) zeros(1,N/4)];
    y4=[zeros(1,3*N/4) (dataContainer{l}(3*N/4+1:N))];

    axes=axesContainer{l};

    a1=area(axes,1:N,y1);
    set(a1,'FaceColor','yellow')
    hold on
    a2=area(axes,1:N,y2);
    set(a2,'FaceColor','blue')
    hold on
    a3=area(axes,1:N,y3);
    set(a3,'FaceColor','green')
    hold on
    a4=area(axes,1:N,y4);
    set(a4,'FaceColor','red')

    set(axes,'XTickLabel','')
    set(axes,'YTickLabel','')

end

我的脚本结果如下:

enter image description here

同样只有一张图片包含所有区域。

2 个答案:

答案 0 :(得分:1)

看起来每次调用plot(axes,data)都会删除axes中写的内容。

重要说明:请勿使用与函数相同的变量名称。请勿拨打sinplotaxes!我将其更改为axs

要解决这个问题,我只使用了经典的subplot,而不是像你一样创建轴:

len = 9;

axesContainer = cell(len,1);
x = [0.1,0.4,0.7,0.1,0.4,0.7,0.1,0.4,0.7];
y = [0.1,0.1,0.1,0.4,0.4,0.4,0.7,0.7,0.7];

figure(1)


dataContainer = cell(len,1);
N = 1500;

for i=1:len
        dataContainer{i} = rand(1,N)*100;
end

for l=1:len

    y1=[(dataContainer{l}(1:N/4)) zeros(1,3*N/4)];
    y2=[zeros(1,N/4) (dataContainer{l}(N/4+1:(2*N/4))) zeros(1,2*N/4)];
    y3=[zeros(1,2*N/4) (dataContainer{l}(2*N/4+1:3*N/4)) zeros(1,N/4)];
    y4=[zeros(1,3*N/4) (dataContainer{l}(3*N/4+1:N))];

    axs=subplot(3,3,l);

    a1=area(axs,1:N,y1);
    set(a1,'FaceColor','yellow')
    hold on
    a2=area(axs,1:N,y2);
    set(a2,'FaceColor','blue')
    hold on
    a3=area(axs,1:N,y3);
    set(a3,'FaceColor','green')
    hold on
    a4=area(axs,1:N,y4);
    set(a4,'FaceColor','red')

    set(axs,'XTickLabel','')
    set(axs,'YTickLabel','')
    axis tight % this is to beautify it.
end

enter image description here

据我所知,您仍然可以将axs变量保存在axescontainer中,然后修改所需的属性(如位置)。

答案 1 :(得分:1)

我发现了如何做我需要的事情。

len = 8;

axesContainer = cell(len,1);
x = [0.1,0.4,0.7,0.1,0.4,0.7,0.1,0.4];
y = [0.1,0.1,0.1,0.4,0.4,0.4,0.7,0.7];

figure(1)

for i=1:len
    axesContainer{i} = axes('Position',[x(i),y(i),0.2,0.2]);
end

dataContainer = cell(len,1);
N = 1500;

for i=1:len
        dataContainer{i} = rand(1,N)*100;
end

for l=1:len

    y1=[(dataContainer{l}(1:N/4)) zeros(1,3*N/4)];
    y2=[zeros(1,N/4) (dataContainer{l}(N/4+1:(2*N/4))) zeros(1,2*N/4)];
    y3=[zeros(1,2*N/4) (dataContainer{l}(2*N/4+1:3*N/4)) zeros(1,N/4)];
    y4=[zeros(1,3*N/4) (dataContainer{l}(3*N/4+1:N))];

    axes=axesContainer{l};

    Y=[y1',y2',y3',y4'];

    a=area(axes,Y);

    set(axes,'XTickLabel','')
    set(axes,'YTickLabel','')

end

该区域应该与这样的矩阵一起使用。棘手的部分是,每个下一列中的信号不是绝对绘制的,而是相对于前一列中的数据。这意味着,如果在时间1,第一列中的数据值为1,第二列中的数据值为4,则第二列数据的值为5。来源:http://www.mathworks.com/help/matlab/ref/area.html