How to use the mouse to select and identify a line on a plot
Show older comments
Hi,
I have an analysis that I am performing repeatedly. There are two multi-channel data sources. These sources have different and possibly varying identification numbers. It is important that data source A and B are correctly identified.
I would like to be able to include a user interactive check where a channel from source A and B are plotted together. The user is then asked for instance to pick or highlight the line from data set A. This is then used to identify A and B. If necessary a swap of data sets is then performed prior to launching into the full analysis.
I am new to GUI programming and using callbacks etc in Matlab and would appreciate any comments or suggestions on this.
Regards
Andrew Sims
Answers (2)
You can use the ButtonDownFcn of the line objects:
function smallTest
axes('NextPlot', 'add')
H(1) = plot(1:10, rand(1, 10), 'r');
H(2) = plot(1:10, rand(1, 10), 'b');
set(H, 'ButtonDownFcn', {@LineSelected, H})
function LineSelected(ObjectH, EventData, H)
set(ObjectH, 'LineWidth', 2.5);
set(H(H ~= ObjectH), 'LineWidth', 0.5);
12 Comments
Jan
on 8 Jan 2018
@Thomas: It is not clear what "save" means in your question. Where should the data appear?
Michael Judge
on 18 Apr 2018
Jan,
I believe Thomas and I are looking for the same thing: how to return data about the line clicked (e.g. XData, YData, or in my case, line index) as an output to these functions passed to the overarching workspace. Knowing how to do this would be extremely helpful.
Thanks, Michael
Michael Judge
on 18 Apr 2018
Defining or updating a global variable inside of one of the functions might accomplish this.
Great, it is possible to make this work with selecting a line from the legend? maple, for example, allows one to select a graph from the legend which helps when there are a lot of plots.
Edit: I was able to do this by simply extending it to work with legends return. The code functions almost identically except there is some funkiness with out legend returns the handles which are interlieved for the lines with something else That is, it returns
text1,...., textn, line1, ?1, line2, ?2, ...linen, ?n
and so one must separate out the ?k
Jan
on 9 May 2019
@Uiy Uiy: I do not understand your comment. Do you have a question? Which code do you use to get this output? For interactive legends see:
- https://www.mathworks.com/matlabcentral/fileexchange/21799-clickablelegend-interactive-highlighting-of-data-in-figures
- https://blogs.mathworks.com/pick/2016/03/25/interactive-legend-in-r2016a/
Beside setting the visibility, you can export the XData and YData easily. It is a bad idea to store them in a global variable, but better use them as inputs of a function.
Mohammed Ahmed
on 30 Mar 2021
"Beside setting the visibility, you can export the XData and YData easily. It is a bad idea to store them in a global variable, but better use them as inputs of a function."
Hi there, I am wondering how can I export the XData and YData?
Thanks in advance.
@Mohammed Ahmed: It depends: The XData and YData of what? And where do you want to use the exported data?
Maybe this helps you:
function main
LineH = DrawCore;
XData = LineH.XData;
YData = LineH.YData;
end
function LineH = DrawCore
figure;
axes;
LineH = plot(1:10, rand(1,10));
end
The export of the data is trivial. The problem might be, how to obtain the handle of the wanted object.
Mohammed Ahmed
on 31 Mar 2021
Exactly, I want to the user to click on two lines. And then find the point of interest of these two lines.
Jan
on 31 Mar 2021
Okay, then do this. If you have a question to a specific detail, please open a new thread and explain it.
Claus Andersson
on 24 Feb 2022
Jan, thanks for the very helpful solution above - is there a way to 'inline' this LineWidth change in the 'set' call to avoid having the function LineSelected.
I would like to have the figure stay interactive, even after the function has stopped running.
Jan
on 24 Feb 2022
I do not understand, what you are asking for.
You need to write the code of a callback into a function. Where do you want to "inline" it instead?
The figure does stay interactive. Which function stopped running?
Claus Andersson
on 24 Feb 2022
0 votes
Hi Jan. My mistake. It works perfectly and the callback continues to work, I had a typo.
1 Comment
Jan
on 25 Feb 2022
Fine. Typos are the companions of programmers.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!