Legend isn't including all entries

7 views (last 30 days)
David Harra
David Harra on 11 Mar 2022
Commented: Voss on 11 Mar 2022
Hi everyone
I am getting another error when creating my legend as one of my entries aren't showing
figure(03)
plot(Time, Data, Time(locs), pks, 'or')
xlim([3.1e-6 6.1e-6])
RMS= rms(Data);
yline(RMS)
xlabel('Time (s)')
ylabel('Amplitude ')
title('10 MHz 110 Features (Pulse Echo)')
legend('Signal', sprintf('Amplitude1 = %0.04d', 9.2612e-4),sprintf('Amplitude2 = %0.04d',2.4969e-4), sprintf('RMS = %0.004d', 2.9752e-5 ))
My amplitude 2 in the legend isn't showing up. I should have entries. My locs and pks are just 2 x 1 vectors. I have even tried playing around with different variations for example trying , and as you can see the image on the right is the best I can get. I gives me all 4 legend entries but the pointer location is off. Any help would be greatly appreciated :)
plot(Time, Data, Time(locs), pks(1,:), 'or')

Accepted Answer

Voss
Voss on 11 Mar 2022
There are a couple of different things you can do, depending on what your objective is. See Approach 1 and 2 below:
% making up relevant variables:
Time = (3:0.01:6)*1e-6;
Data = randn(size(Time));
[pks,locs] = findpeaks(Data);
[pks,idx] = sort(pks);
pks = pks(end-1:end);
locs = locs(idx(end-1:end));
% Approach 1: plot one line for each of the 2 peaks:
figure(03)
plot(Time, Data);
hold on
plot(Time(locs(1)), pks(1), 'or')
plot(Time(locs(2)), pks(2), 'or')
xlim([3.1e-6 6.1e-6])
RMS= rms(Data);
yline(RMS)
xlabel('Time (s)')
ylabel('Amplitude ')
title('10 MHz 110 Features (Pulse Echo)')
legend('Signal', sprintf('Amplitude1 = %0.04d', 9.2612e-4),sprintf('Amplitude2 = %0.04d',2.4969e-4), sprintf('RMS = %0.004d', 2.9752e-5 ))
% Approach 2: make a multi-line legend entry for the two peaks:
figure(04)
plot(Time, Data);
hold on
plot(Time(locs), pks, 'or')
xlim([3.1e-6 6.1e-6])
RMS= rms(Data);
yline(RMS)
xlabel('Time (s)')
ylabel('Amplitude ')
title('10 MHz 110 Features (Pulse Echo)')
legend('Signal', sprintf('Amplitude1 = %0.04d\nAmplitude2 = %0.04d',9.2612e-4,2.4969e-4), sprintf('RMS = %0.004d', 2.9752e-5 ))

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!