how get information moving mouse on line plotted

14 ビュー (過去 30 日間)
Luca Re
Luca Re 2024 年 5 月 17 日
コメント済み: Luca Re 2024 年 6 月 7 日
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');

回答 (1 件)

Saurabh
Saurabh 2024 年 5 月 21 日
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 件のコメント
Saurabh
Saurabh 2024 年 6 月 7 日
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.
Luca Re
Luca Re 2024 年 6 月 7 日
ok thank

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by