How to plot a patch?

9 次查看(过去 30 天)
Mr M.
Mr M. 2016-11-28
回答: Star Strider 2016-12-17
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 个评论
bio lim
bio lim 2016-11-28
Can you give the code for your rectangle plot so that I can take a look at it?
Mr M.
Mr M. 2016-12-17
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.

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2016-12-17
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 2016-12-13
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

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by