- 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?
14 views (last 30 days)
Show older comments
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:
for i=1:length(Th)
(pk_x,pk_y)=find_pks(x,y,Th,11)
SWH=[SWH; mean(pk_y)];
end
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!
0 Comments
Answers (1)
Benjamin Kraus
on 1 Jun 2022
Edited: Benjamin Kraus
on 1 Jun 2022
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
% axes.
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
% clicked.
figure
plot(someOtherData(:,ind));
title(ind)
end
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!