Interactive Plotting - Get another plot via clicking point in current figure

15 ビュー (過去 30 日間)
Nick
Nick 2013 年 1 月 23 日
コメント済み: Luke Coniker 2021 年 7 月 14 日
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

回答 (2 件)

José-Luis
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 件のコメント
Nick
Nick 2013 年 1 月 23 日
Hi José-Luis, many thanks for your answer, it has been quite helpful.
I am having trouble though with the correct syntax as to how differentiate between the different resulting plots that occur as a result of which point is clicked (A different figure will be produced for each point clicked on).
Also, is there anyway it can be modified such that it only requires one left click to open up the corresponding figure as opposed to one left then three right clicks?
Thanks,
Nick
José-Luis
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
Justin Cantrell 2016 年 8 月 15 日
@Nick Did you figure this out ? I have the same problem. Thanks
  1 件のコメント
Luke Coniker
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 ExchangeGraphics Performance についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by