Interactive Plotting - Get another plot via clicking point in current figure
15 views (last 30 days)
Show older comments
Hi there, I'm currently looking into making my graphs more interactive. I'm running a simulation conducted using 2 for loops and a bvp4c code. Basically, the resulting plot creates 4 lines (4 cycles of the outer for loop) which are made up of 10 points (10 cycles of the inner for loop).
My desire is that for any one of these points, I want to be able to click ont it, which in turn opens another plot of purpose for which I can define in the code.
Is this possible and if so, if anyone one could point me in the right direction that would be much appreciated!
Cheers,
Nick
0 Comments
Answers (2)
José-Luis
on 23 Jan 2013
Edited: José-Luis
on 24 Jan 2013
Here is a script that will plot four lines and after that highlight the closest point to your click. To select a point use left click, to finish the selection use the right click. The script will keep on going until a right click is detected:
aH = axes;
lH(1) = plot(aH,rand(10,1),'bs');
hold on
lH(2) = plot(aH,rand(10,1),'ro');
lH(3) = plot(aH,rand(10,1),'k+');
lH(4) = plot(aH,rand(10,1),'g^'); % some lines
set(lH,'hittest','off'); % so you can click on the Markers
hold on;
set(aH,'ButtonDownFcn',@getCoord); % Defining what happens when clicking
uiwait(f) %so multiple clicks can be used
And the function getCoord() should be placed in the path and look as follows:
function getCoord(aH,evnt)
drawnow
f = ancestor(aH,'figure');
click_type = get(f,'SelectionType');
ptH = getappdata(aH,'CurrentPoint');
delete(ptH)
if strcmp(click_type,'normal')
%Finding the closest point and highlighting it
lH = findobj(aH,'Type','line');
minDist = realmax;
finalIdx = NaN;
finalH = NaN;
pt = get(aH,'CurrentPoint'); %Getting click position
for ii = lH'
xp=get(ii,'Xdata'); %Getting coordinates of line object
yp=get(ii,'Ydata');
dx=daspect(aH); %Aspect ratio is needed to compensate for uneven axis when calculating the distance
[newDist idx] = min( ((pt(1,1)-xp).*dx(2)).^2 + ((pt(1,2)-yp).*dx(1)).^2 );
if (newDist < minDist)
finalH = ii;
finalIdx = idx;
minDist = newDist;
end
end
xp=get(finalH,'Xdata'); %Getting coordinates of line object
yp=get(finalH,'Ydata');
ptH = plot(aH,xp(finalIdx),yp(finalIdx),'k*','MarkerSize',20);
setappdata(aH,'CurrentPoint',ptH);
elseif strcmp(click_type,'alt')
%do your stuff once your point is selected
disp('Done clicking!');
% HERE IS WHERE YOU CAN PUT YOUR STUFF
uiresume(f);
end
2 Comments
José-Luis
on 24 Jan 2013
Edited: José-Luis
on 24 Jan 2013
I don't understand. You don't get extra plots with the codes I posted. If what you mean is that you want to create different plots for each point you click, then you need to modify the code inside
if strcmp(click_type,'normal')
%whatever you want to do after each click
end
Also, when do you need three right clicks? The code will stop running uiresume() after the first one.
Justin Cantrell
on 15 Aug 2016
@Nick Did you figure this out ? I have the same problem. Thanks
1 Comment
Luke Coniker
on 14 Jul 2021
Hey Justin did you ever figure this out? I having the same problem. Thanks for any help!
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!