Draw a draggble rectangle (fill with color) on a subplot to change the values in another subplot.

Hi I am trying to draw a rectangle that can be dragable (between the XLim of the subplot). When dragged some update is made in another subplot based on the x limits imposed by the position of the rectangle.
What I have done:
h = findobj(ax, 'Type', 'line' ); % get the handle of get the x data from subplot
xp=get(h,'xData'); % get subplot xData
yp = get(ax,'ylim'); % get subplot y limit
vert = [xp(1), yp(1);xp(1), yp(2);xp(100), yp(2);xp(100), yp(1)]; % create vertex coordinates
transparency = 0.5; facecolor =[ 0.3765 0.3765 0.3765 ]; % light grey
edgecolor = [0 0 0]; tcolor(1,1,1:3) = facecolor; % color
fac = [1 2 3 4]; % connect points to make square
dat.hPatch = patch('parent',ax,'Faces',fac,'Vertices',vert,'FaceColor',tcolor,'FaceAlpha',transparency,'edgecolor',edgecolor,'Visible','on', 'tag', 'hPatch');
1) This is the better approach?
2) If I maximise or minimize the image the subplots don't update properly (for instance one subplot shrink and another not) how force them to update?
3) How to make the rectangle move smoothly when dragged by the mouse?
Any help would be apreciated.

Risposte (1)

If you have the Image Processing toolbox
doc imrect

6 Commenti

Hi adam Thank you for the help. I also try imrect but can I fill the rectangle with a collor?
h = imrect(ax, [0, YLM(1), xp(Npoints), heigh1]);
addNewPositionCallback(h,@(p) title(mat2str(p,3)));
fcn = makeConstrainToRectFcn('imrect',get(ax,'XLim'),get(ax,'YLim'));
setPositionConstraintFcn(h,fcn);
setResizable(h,0)
setColor(h,[1,0,0])
Not as far as I am aware, though you could create your own rectangle that mirrors its position, e.g.
doc rectangle
or you could just use that rectangle and program the moving behaviour again as you do for your current solution.
If you use imrect then setting the vertices of the rectangle object to update when imrect's position changes should be relatively simple.
I think you will need to ensure the imrect is created after the rectangle though as it needs to sit on top and imrect dos not seem to be a standard graphics object that you can define the uistack order of.
ok thank you.
Can you help me a little more please. When the rectangle position change I acess the callbak OnRectPosChange but how can I pass the handle of the rectangle and other variables? Is not possible to use also a tag so how can I know what rectangle is being draged and in what subplot?
addNewPositionCallback(h, @OnRectPosChange);
My problem is that I have several subplots. Let say that I have 4 subplots.
The behavior should be: Subplot1 with rect -> when dragged change subplot 2 Subplot3 with rect -> when dragged change subplot 4
What happens is that when I drag rect of subplot 1 both values of subplots 2 and 4 are changing, since I can't determine what rect is being dragged.
You can pass arguments like you would to a normal function if you have them at the time you setup the callback - e.g.
addNewPositionCallback(h, @OnRectPosChange( 'rect1', a ) );
assuming 'a' were defined in the workspace in which you call that line - i.e. when you setup the callback, not when it triggers.
If I was doing it I would have it embedded in a class which stores the rectangle objects in an array or a map, depending how I wish to access each one, but alternatively you can pass in a string like the example when you setup the callback.
Hi Adam,
I already try that but I get an error saying invalid syntax. I am using matlab R2015B vs 8.6.
Oops, sorry, that is the incorrect syntax when you add arguments. It should be:
@() OnRectPosChange( 'rect1', a )
to create an anonymous function.

Accedi per commentare.

Richiesto:

il 9 Mar 2016

Modificato:

il 10 Mar 2016

Community Treasure Hunt

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

Start Hunting!

Translated by