- Callbacks — Programmed Response to User Action
- How to get the point under mouse click on the plot or histogram?
- Obtain data points from plot using 'buttondownfcn' nested functions
Interactive plot - how to open plot by clicking a point in another plot?
6 views (last 30 days)
I have two variables: Th and SWH which are two 40x1 arrays. In a loop, I use a value from Th to find the peaks in a timeseries in order to calculate SWH. Basically, my code is as such:
I am plotting Th and SWH to see the relationship between the two variables (we'll call this plot Plot A). However, I want to plot the pk_x and pk_y for each Th and SWH. Except, I don't want to make 40 or so plots. Is there any way I can open a plot that shows the pk_x and pk_y of a specific value of Th and SWH if i click on that point in Plot A?
Hope this makes sense. Thank you!
Benjamin Kraus on 1 Jun 2022
Edited: Benjamin Kraus on 1 Jun 2022
If I understand your question correctly, you can use the ButtonDownFcn to do this.
For more details:
I think this example will do something similar to what you are requesting:
someOtherData = rand(100,10);
ax = axes;
p = plot(ax, 1:10, '.-');
% Capture clicks on the plot line by setting the ButtonDownFcn. You can
% also detect clicks on the axes itself by setting the ButtonDownFcn on the
p.ButtonDownFcn = @(~,~) buttonCallback(ax, someOtherData);
function buttonCallback(ax, someOtherData)
% Determine where you clicked:
cp = ax.CurrentPoint(1,1:2);
% Determine the nearest data point. The specifics of this algorithm will
% depend on your data. In this toy example all my data are integers, so I
% can just round, but most examples will be more complicated than this.
ind = round(cp(1));
% Create a new plot using someOtherData based on the index that was