How can I identify which line in a figure I am clicking on?

I am plotting 1000 lines on a plot and what to identify which line is the outlier is.

Answers (1)

Jan
Jan on 11 Apr 2013
Edited: Jan on 11 Apr 2013
You can use the ButtonDownFcn of the lines:
y = rand(10, 10);
LineList = plot(1:10, y);
set(LineList, 'ButtonDownFcn', {@myLineCallback, LineList});
function myLineCallback(LineH, EventData, LineList)
disp(LineH); % The handle
disp(get(LineH, 'YData')); % The Y-data
disp(find(LineList == LineH)); % Index of the active line in the list
set(LineList, 'LineWidth', 0.5);
set(LineH, 'LineWidth', 2.5);
uistack(LineH, 'top'); % Set active line before all others
I've inserted some ideas about what "identify" could mean: The line's handle, the YData or the index related to all lines.

2 Comments

I also tried it and found that it works!
I might also just add an additional useful tip for the question posed. If you're plotting in a for-loop, you can give each line a name using the DisplayName argument inside your plot function. Example:
for i=1:length(numPlots)
plotObject(i) = plot(myPlotXaxis(i),myPlotYaxis(i),DisplayName='IterationNum-'+string(i))
set(p1(i), 'ButtonDownFcn', {@myLineCallback, p1(i)});
end
In this way, each line will have a specific name. This will come up in the command line, when you click on one of the lines on your graph.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Asked:

on 11 Apr 2013

Commented:

on 13 Sep 2023

Community Treasure Hunt

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

Start Hunting!