How to label the maximum and minimum value on a figure

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

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

Why are you being so pretentious about his question? Maybe he did know that you need to use min() and max() function but, it just won't work for some other reason. Just because it was easy for you doesn't mean it's easy for everyone. Don't try to discourage someone because their question seems obvious.
Not everyone knows about those functions - many beginners may not. Because it's tagged as homework I can't show him how to do it so all I can do is to inform him of the functions and encourage him to look up them up in the documentation. How would you answer his question, keeping in mind that you can't do it for him (like I'm doing below) because it's his homework?
Now that it's 8 years past when his homework was due I can give the final solution:
[minSignal, indexOfMin] = min(signal);
tMin = t(indexOfMin)
[maxSignal, indexOfMax] = max(signal);
tMax = t(indexOfMax)
textLabel = sprintf('Min at t=%f, max at t=%f', tMin, tMax);
text(0, 0, textLabel); % Adjust 0,0 as needed - depends on signal range.
what does 't' in your code here represents ?
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)

Categories

Find more on Networks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!