How to automatically calculate and display the difference between 2 datatip points in MATLAB R2023a?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 25 Sep 2023
Réponse apportée : MathWorks Support Team
le 2 Oct 2023
How can I automatically calculate the difference between two selected datatips and display them as "dx" and "dy" on the chart? What steps should I follow to achieve this?
Réponse acceptée
MathWorks Support Team
le 25 Sep 2023
There are at least two ways to achieve the intended workflow:
1 . Using the "ginput" function:
One suggested approach is to manually select the points using the "ginput" function and perform the calculation with the datatip "UpdateFcn" callback function.
The "ginput" function allows you to select the coordinates of multiple points within Cartesian, polar, or geographic axes. To choose a point, you move the cursor to the desired location and press a mouse button or a key on the keyboard. Pressing the Return key stops the selection before all points are chosen. The selected points' coordinates are returned by MATLAB.
The documentation for "ginput" can be found at:
Here is the workaround code:
% Create a new figure window
f = figure;
% Define x and y values for plotting
x = 1:10;
y = x.^2;
% Plot y = x^2
plot(x, y);
% Prompt the user to select the first point of interest using the mouse
firstPoint = ginput();
% Prompt the user to select the second point of interest using the mouse
secondPoint = ginput();
% Calculate the difference in x-coordinates between the two selected points
xDiff = secondPoint(1) - firstPoint(1);
% Calculate the difference in y-coordinates between the two selected points
yDiff = secondPoint(2) - firstPoint(2);
2. Using "datatips" function:
If you want to incorporate only datatips in your workflow, you can use the following code.
This workaround using "uifigure" creates a uifigure where you can select datatips.
uif = uifigure;
ax = axes(uif);
x = 1:10;
y = x.^2;
plot(ax,x,y);
xlim([1, 10]); % Adjust xlim and ylim
ylim([2, 500]);
First, execute the above section, and after selecting the 2 datatips execute the following code to compute the difference between the datatips:
datatips = findobj(uif,'Type','datatip'); % Get the selected datatips from the plot
dy = datatips(2).Y - datatips(1).Y; %Calculation of dy
dx = datatips(2).X - datatips(1).X; %Calculation of dx
Then to show the difference of both the datatips on the plot execute the following code snippet:
% Show specific datatip (dx,dy) on the graph
hold(ax,'on')
plot(ax, datatips(1).X, datatips(1).Y, 'ro', 'MarkerSize', 10);
plot(ax, datatips(2).X, datatips(2).Y, 'ro', 'MarkerSize', 10);
plot(ax, dx, dy, 'ro', 'MarkerSize', 10);
plot(ax,x,y);
text(ax, datatips(1).X, datatips(1).Y, sprintf('dx = %d\ndy = %d', dx, dy))
hold(ax,'off')
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Exploration dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!