How to write a point's coordinates

4 次查看(过去 30 天)
Jacked Daniel
Jacked Daniel 2019-11-28
回答: Nihal 2024-6-12
How can I write coordinates of my extrema on a figure?
I tried using "text" command but I stiil can't deal with the problem

回答(1 个)

Nihal
Nihal 2024-6-12
To write the coordinates of extrema (maximum and minimum points) on a figure in MATLAB, you can indeed use the text command effectively. Here's a step-by-step guide on how to identify the extrema of a given dataset or function and then annotate these points on a figure.
Let's assume you have a simple dataset or a function for which you want to find and annotate the extrema. I'll provide examples for both scenarios.
For a Dataset
Assuming you have x and y vectors representing your data.
x = 1:10; % Example x data
y = [2 3 1 5 6 4 7 8 6 9]; % Example y data
% Find the indices of the min and max
[ymax, imax] = max(y);
[ymin, imin] = min(y);
% Plot the data
plot(x, y, '-o'); % Plot with markers at each data point
hold on; % Keep the plot for further plotting
% Annotate the max
text(x(imax), ymax, sprintf('Max: (%d, %d)', x(imax), ymax), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
% Annotate the min
text(x(imin), ymin, sprintf('Min: (%d, %d)', x(imin), ymin), 'VerticalAlignment', 'top', 'HorizontalAlignment', 'right');
hold off; % Release the plot
Tips
  • Adjust the 'VerticalAlignment' and 'HorizontalAlignment' properties in the text command to better position your annotations relative to the extrema points.
  • Use sprintf to format the text string with the coordinates.
  • If dealing with complex functions or large datasets, consider using numerical methods or built-in functions like findpeaks for more sophisticated extrema detection.
This should help you annotate the extrema on your figures effectively. If you encounter any specific issues, please provide more details about the problem you're facing. I hope it helps.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by