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

15 visualizaciones (últimos 30 días)
Nick
Nick el 23 de En. de 2013
Comentada: Luke Coniker el 14 de Jul. de 2021
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

Respuestas (2)

José-Luis
José-Luis el 23 de En. de 2013
Editada: José-Luis el 24 de En. de 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 comentarios
Nick
Nick el 23 de En. de 2013
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 el 24 de En. de 2013
Editada: José-Luis el 24 de En. de 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.

Iniciar sesión para comentar.


Justin Cantrell
Justin Cantrell el 15 de Ag. de 2016
@Nick Did you figure this out ? I have the same problem. Thanks

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by