Clear Filters
Clear Filters

how get information moving mouse on line plotted

10 views (last 30 days)
See pics:
if i move cursor on line 1 i want see this information about line 1 ( === 1 - NOZIONALE ===>.***....)
i use it to get information
leg=strcat(string(1:c)',{' - '},string(STR_name),"==>",nm," ",tm);
legend(Ax_Eq,legend_lines,leg,'Location','northwest','Interpreter','none','Box','off');

Answers (1)

Saurabh
Saurabh on 21 May 2024
Hi Luca,
I understand that you want to display the information about the plotted lines on the Axes.
To show the legend of a specific graph when you hover the mouse over it in MATLAB, you can modify the custom callback function for mouse movement to check which line the mouse is closest to and then display a text box or update the figures title with the legend of that line. The example I have included assumes that each line has a ‘DisplayName’ property set, which is used in the legend.
% Example plot with multiple lines and DisplayNames set for each
figure;
hold on;
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
plot1 = plot(x, y1, 'DisplayName', 'Sine Wave');
plot2 = plot(x, y2, 'DisplayName', 'Cosine Wave');
legend;
% Set up the custom mouse movement callback
set(gcf, 'WindowButtonMotionFcn', @(src, event)mouseMoveCallback(src, event, [plot1, plot2]));
function mouseMoveCallback(~, ~, plotHandles)
% Get current point in the axes coordinates
C = get(gca, 'CurrentPoint');
x = C(1,1);
y = C(1,2);
% Initialize minimum distance and index
minDist = inf;
idx = 0;
% Loop through all plots to find the closest one
for i = 1:length(plotHandles)
xData = get(plotHandles(i), 'XData');
yData = get(plotHandles(i), 'YData');
distances = sqrt((xData - x).^2 + (yData - y).^2);
[minDistPlot, ~] = min(distances);
if minDistPlot < minDist
minDist = minDistPlot;
idx = i;
end
end
% If a closest plot is found, display its DisplayName
if idx > 0
displayName = get(plotHandles(idx), 'DisplayName');
title(gca, ['Closest plot: ' displayName]);
else
title(gca, '');
end
end
Here, ‘mouseMoveCallback’ calculates the distance from the mouse pointer to all points on all the lines plotted in the axes. It identifies the line closest to the mouse pointer and updates the axes title with the 'DisplayName' of that line, which acts as its legend.
Few points to note:
  1. This approach requires that each plotted line has a unique 'DisplayName' set, which is used to identify the line in the legend.
  2. The `minDist` threshold for deciding if the mouse is "close enough" to a line to display its legend might need adjustment based on your specific plot and preferences.
  3. This method updates the axes title to show which line is closest to the mouse. You could alternatively use a dedicated text box or other UI elements to display this information, depending on your application's requirements.
To get more information about the function used in the example, refer these links:
  1. https://www.mathworks.com/help/matlab/ref/matlab.system.get.html?s_tid=doc_ta
  2. https://www.mathworks.com/help/matlab/ref/set.html?s_tid=doc_ta
  3. https://www.mathworks.com/help/matlab/ref/gca.html?s_tid=doc_ta
  3 Comments
Saurabh
Saurabh on 7 Jun 2024
Hi Luca,
If you are looking for an answer with respect to the app designer, then many things need to be changed in the code itself. I will look into it or try to find an alternate answer.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!