How to label the maximum and minimum value on a figure

345 views (last 30 days)
Find the time point that corresponds to the maximum and minimum value. Add a text label to the figure indicating the maximum value. Add a text label to the figure indicating the minimum value.

Accepted Answer

Image Analyst
Image Analyst on 14 Mar 2013
Have you used the help? Did you look up max, min, and text? You will use each of those functions to solve your homework exercise. Be sure to accept both output arguments of min() and max(). This should be a really really trivial problem, even for a beginner. It's so simple that we can't really even give hints without solving it. It's just 4 lines of code to call min(), max(), and 2 calls to text().
  4 Comments
Image Analyst
Image Analyst on 7 Nov 2021
He's plotting something versus "time points", so t is a vector of the time points, in other words the x axis, and the x axis represents time.
% Create a sample signal.
t = linspace(0.1, 2*pi, 100); % Time axis
y = sin(t);
plot(t, y, 'b-', 'LineWidth', 2);
grid on;
xlabel('t [Time in seconds]');
ylabel('Voltage [Volts]');
% Draw t axis
yline(0, 'LineWidth', 2);
% Find min y value and it's index.
[minSignal, indexOfMin] = min(y);
% Find time value at that min y value.
tMin = t(indexOfMin)
xline(tMin, 'Color', 'r', 'LineWidth', 2)
textLabel = sprintf(' Min of %.2f at t=%f', minSignal, tMin);
text(tMin, minSignal, textLabel, 'fontSize', 15, 'Color', 'r', 'VerticalAlignment','top')
% Find max y value and it's index.
[maxSignal, indexOfMax] = max(y);
% Find time value at that max y value.
tMax = t(indexOfMax)
xline(tMax, 'Color', 'r', 'LineWidth', 2)
textLabel = sprintf(' Max of %.2f at t=%f', maxSignal, tMax);
text(tMax, maxSignal, textLabel, 'fontSize', 15, 'Color', 'r', 'VerticalAlignment','bottom')
ylim([-1.2, 1.2])

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!