Legend isn't including all entries

13 次查看(过去 30 天)
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')

采纳的回答

Voss
Voss 2022-3-11
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 ))

更多回答(0 个)

类别

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

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by