Interactive Plotting - Get another plot via clicking point in current figure
조회 수: 15 (최근 30일)
이전 댓글 표시
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
답변 (2개)
José-Luis
2013년 1월 23일
편집: José-Luis
2013년 1월 24일
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
José-Luis
2013년 1월 24일
편집: José-Luis
2013년 1월 24일
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
2016년 8월 15일
@Nick Did you figure this out ? I have the same problem. Thanks
댓글 수: 1
Luke Coniker
2021년 7월 14일
Hey Justin did you ever figure this out? I having the same problem. Thanks for any help!
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!