how can i plot a rectangle using user clicks in matlab in matlab

You can use the ginput() function to get user clicks and then plot a rectangle based on the coordinates of the two corner points.

Here's an example code:

main.m
% Get user clicks
[x, y] = ginput(2);

% Create rectangle
width = abs(x(2) - x(1));
height = abs(y(2) - y(1));
x_c = min(x);
y_c = min(y);
rectangle('Position', [x_c, y_c, width, height]);
189 chars
10 lines

ginput() returns the x and y coordinates of n points clicked by the user. In this case, we only require two points to define a rectangle. The abs() function is used to make sure that the width and height values are positive even if the user clicks in reverse order. Finally, rectangle() is used to plot the rectangle using the Position property, which specifies the position and size of the rectangle in the form [x, y, width, height].

Note that this code will plot the rectangle in the current figure, so you may want to create a new figure beforehand using the figure() function.

gistlibby LogSnag