Interactive plot - how to open plot by clicking a point in another plot?

15 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:
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!

Answers (1)

Benjamin Kraus
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
% 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

Categories

Find more on Graphics Performance 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!