Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MATLAB中绘制填充矩形作为所需目标的背景?_Matlab_Matlab Figure_Fill_Area_Rectangles - Fatal编程技术网

如何在MATLAB中绘制填充矩形作为所需目标的背景?

如何在MATLAB中绘制填充矩形作为所需目标的背景?,matlab,matlab-figure,fill,area,rectangles,Matlab,Matlab Figure,Fill,Area,Rectangles,我有两个数据集,一个是目标位置,另一个是实际位置。我想用+/-可接受范围绘制目标,然后用实际值覆盖。然而,这个问题只与目标位置有关 我尝试了内置的区域、填充和矩形函数,但没有成功。使用stackoverflow上的代码,它只在某些区域正确 比如说 y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum y1 = y+1; %variation in target size y2 = y-1; t = 1:15; X=[t

我有两个数据集,一个是目标位置,另一个是实际位置。我想用+/-可接受范围绘制目标,然后用实际值覆盖。然而,这个问题只与目标位置有关

我尝试了内置的区域、填充和矩形函数,但没有成功。使用stackoverflow上的代码,它只在某些区域正确

比如说

y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1;               %variation in target size
y2 = y-1;
t = 1:15;
X=[t,fliplr(t)];        %create continuous x value array for plotting
Y=[y1,fliplr(y2)];      %create y values for out and then back
fill(X,Y,'b');
生成的图形如下所示:

我希望在此处绘制的红色方框内填写:


谢谢大家!

如果只绘制函数y和x,那么可以使用阶梯图。幸运的是,您可以使用楼梯功能,如:

创建向量xs、ys,使用plot函数时生成楼梯图。我们现在可以使用这些向量为fill函数生成正确的X和Y向量。请注意,楼梯生成列向量,因此我们必须首先转置它们:

y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1;               %variation in target size
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
X=[ts.',fliplr(ts.')];        %create continuous x value array for plotting
Y=[ys1.',fliplr(ys2.')];      %create y values for out and then back
fill(X,Y,'b');

再次感谢HBaders。您完美地回答了我的问题,但是当我将其应用于我需要的大型数据集时,我获得了这张图像

我认为这是因为fill函数将顶点连接到fill

在任何情况下,对于另一个人的潜在解决方案,请将建议的代码与楼梯功能结合使用,并使用面积功能

通过将它们一个接一个地绘制,并将下部区域的颜色设置为白色,它显示为我所追求的矩形图形

%sample code. produces image similar to o.p.
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; 
y1 = y+1;               
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
area(ts,ys1,'FaceColor','b','EdgeColor','none')
hold on
area(ts,ys2,'FaceColor','w','EdgeColor','none')
再次感谢你的帮助,为我指明了正确的方向

%sample code. produces image similar to o.p.
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; 
y1 = y+1;               
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
area(ts,ys1,'FaceColor','b','EdgeColor','none')
hold on
area(ts,ys2,'FaceColor','w','EdgeColor','none')