how to automatically controls the location of the datacursor in a graph with seprated code?

2 次查看(过去 30 天)
hello.
I want to write code that automatically controls the location of the datacursor in a graph by getting data from each crossing point. Can anyone guide me?

采纳的回答

Hassaan
Hassaan 2023-12-29
编辑:Hassaan 2023-12-29
Script to find the point of maximum magnitude in a vector field and highlight it with a marker:
% Assuming U and V contain the components of the vectors in the vector field
[U, V] = meshgrid(linspace(-0.5, 4.5, 100), linspace(0.5, 4.5, 100));
% Calculate the magnitude of the vectors
M = sqrt(U.^2 + V.^2);
% Find the index of the maximum magnitude
[maxM, maxIdx] = max(M(:));
[maxRow, maxCol] = ind2sub(size(M), maxIdx);
% Get the corresponding X and Y coordinates
maxX = U(maxRow, maxCol);
maxY = V(maxRow, maxCol);
% Plot the vector field
figure;
quiver(U, V, U, V, 'b');
hold on;
% Highlight the maximum magnitude point
plot(maxX, maxY, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
% Optionally, add a text label
text(maxX, maxY, sprintf('Max Magnitude: %.2f', maxM), ...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
This script calculates the magnitude of the vectors at each point in the grid, finds the maximum magnitude, and then plots the vector field with a red circle highlighting the point of maximum magnitude. The coordinates and magnitude are also labeled with a text annotation.
If you have the actual data for the vector field and wish to run this script with real data or need further customization, please provide more details or the data itself.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

更多回答(3 个)

Hassaan
Hassaan 2023-12-28
编辑:Hassaan 2023-12-28
To determine if a point is a crossing point on a graph, you would typically compare the coordinates of the point with the coordinates of the intersection points of your plot. For simplicity, let's assume you have two datasets and you want to find their intersection points:
% Example datasets
x1 = 0:0.1:10;
y1 = sin(x1);
x2 = 0:0.1:10;
y2 = cos(x2);
% Plot both datasets
figure;
plot(x1, y1, x2, y2);
% Enable data cursor mode
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myupdatefcn);
% Find the intersection points
[intersectX, intersectY] = intersections(x1, y1, x2, y2, true);
% Define a custom update function
function txt = myupdatefcn(~, event_obj)
% Obtain the cursor position
pos = get(event_obj,'Position');
% Access the data point's x and y coordinates
x = pos(1);
y = pos(2);
% Initialize the text to display
txt = {['X: ', num2str(x)],...
['Y: ', num2str(y)]};
% Determine if the point is a crossing point
tolerance = 1e-5; % Set a tolerance for floating point comparison
for i = 1:length(intersectX)
if abs(x - intersectX(i)) < tolerance && abs(y - intersectY(i)) < tolerance
% Append information if it's a crossing point
txt{end+1} = 'Crossing point!';
break;
end
end
end
% This function computes the intersections of two curves
function [x0,y0] = intersections(x1,y1,x2,y2,robust)
if nargin < 5
robust = false;
end
% Find approximate intersection points
[x0,y0] = polyxpoly(x1,y1,x2,y2);
if robust
% Refine the intersection points to increase accuracy
% This is a placeholder for a robust method of finding intersections
% such as using fminsearch to minimize the distance between the curves
end
end
In the intersections function, you could implement a more robust method of finding intersections if necessary, for example by using optimization to minimize the distance between the two curves.
Please note that polyxpoly is a function from the Mapping Toolbox, and the intersections function is provided as a simple example. Depending on your specific use case and MATLAB version, you may need to implement a custom intersection-finding algorithm or use a different function that's suitable for your data.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

S.Mostafa BaniHashemian
Thank you for your guidance.
But I want to move(automatic control) datacursor (that located on a shape) to the point of the maximum size of the vectors (in real time) by taking the location data (x,y) in the moment. How should I change the your code?
Like the attached picture:

Hassaan
Hassaan 2023-12-29
编辑:Hassaan 2023-12-29
% Example datasets
x1 = 0:0.1:10;
y1 = sin(x1);
x2 = 0:0.1:10;
y2 = cos(x2);
% Find the maximum value and its index for y1
[maxY1, indY1] = max(y1);
% Find the maximum value and its index for y2
[maxY2, indY2] = max(y2);
% Choose which maximum you want to use, let's use y1 for this example
maxX = x1(indY1);
maxY = maxY1;
% Plot both datasets
figure;
plotHandle1 = plot(x1, y1, '-b');
hold on;
plotHandle2 = plot(x2, y2, '-r');
hold on;
% Plot the point of the maximum value in y1
maxPointHandle = plot(maxX, maxY, 'ko', 'MarkerSize', 10, 'MarkerFaceColor', 'y');
% Annotate the maximum point with text
text(maxX, maxY, sprintf('Max: (%.2f, %.2f)', maxX, maxY), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
hold off;
% Adjust plot settings
xlabel('X');
ylabel('Y');
title('Maximum Value Annotation');
legend([plotHandle1, plotHandle2], {'sin(x)', 'cos(x)'});
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by