0

I have two datasets, one of which is a target position, and the other is the actual position. I would like to plot the target with a +/- acceptable range and then overlay with the actual. This question is only concerning the target position however.

I have unsuccessfully attempted the built in area, fill, and rectangle functions. Using code found on stackoverflow here, it is only correct in certain areas.

For example

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');

The figure produced looks like this:

I would prefer it to be filled within the red boxes drawn on here:

Thank you!

Community
  • 1
  • 1

2 Answers2

0

If you would just plot a function y against x, then you could use a stairs plot. Luckily for us, you can use the stairs function like:

[xs,ys] = stairs(x,y);

to create the vectors xs, ys which generate a stairs-plot when using the plot function. We can now use these vectors to generate the correct X and Y vectors for the fill function. Note that stairs generates column vectors, so we have to transpose them first:

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');

result

hbaderts
  • 14,136
  • 4
  • 41
  • 48
0

Again, thank you hbaderts. You answered my question perfectly, however when I applied it to the large data set I needed for, I obtained this image

https://dl.dropboxusercontent.com/u/37982601/stair%20fill.png

I think it is because the fill function connects vertices to fill?

In any case, for the potential solution of another individual, combined your suggested code with the stair function and used the area function.

By plotting them on top of one another and setting the color of the lower area to be white, it appears as the rectangular figures I was after.

%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')

https://dl.dropboxusercontent.com/u/37982601/stair%20area.png

Thanks again for your help and for pointing me in the right direction!