How to plot a patch?

25 views (last 30 days)
Mr M.
Mr M. on 28 Nov 2016
Answered: Star Strider on 17 Dec 2016
I have a plot, but I want to crop a rounded rectangle area from it. For example I have a sinusoid plane wave image plot, and I want to draw only part of it, a rounded rectangle area. Is it posible to do it easily?
  2 Comments
bio lim
bio lim on 28 Nov 2016
Can you give the code for your rectangle plot so that I can take a look at it?
Mr M.
Mr M. on 17 Dec 2016
I would like to find a general solution, it is not important what is the plot, it can be an image or curves on a MATLAB plot.

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 17 Dec 2016
Try this (and a short tutorial on the patch funciton):
The Code
x = linspace(0, 2*pi, 1000);
y = sin(x);
sub_area_idx = [find((y <= 0.5) & (x <= pi)) find((y >= -0.5) & ((x > pi) & (x <= 2*pi)))];
figure(1)
plot(x,y,'r')
hold on
patch(x([sub_area_idx, flip(sub_area_idx)]), [y(sub_area_idx), zeros(size(sub_area_idx))], 'g')
hold off
grid
The ‘x’ (independent variable) and ‘y’ (dependent variable) together define the entire sine curve. I arbitrarily chose ± ½ as the amplitude for the patch. The ‘sub_area_idx’ are indices into both ‘x’ and ‘y’ for those criteria.
In the patch call, the independent variable ‘x’ has to ‘retrace its steps’ (thus the flip call) to complete the curve, while the dependent variable ‘y’ only has to define those ‘retraced steps’ as zero, since (by our definition here) the patch only exists between the ‘x’-axis and ‘y’ at any particular point.
I’m not certain what you want to do, but this should at least get you part way there.
The Plot

Farouk Moukaddem
Farouk Moukaddem on 13 Dec 2016
Hi Mr M,
You can use the "imcrop" function to create an interactive image cropping tool associated with the image displayed in your current figure.
The following command can be used to crop the image "I" according to the cropping rectangle "rect"
>>imcrop(I,rect)
"rect" is a four-element position vector of the form [xmin ymin width height] that specifies the size and the position of the crop rectangle. To obtain the coordinates of the rectangle that you would like to crop, you can use the "getrect" function to obtain these values.
Refer to the following documentation links for more information:
Thanks,
Farouk

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!