Legend isn't including all entries

조회 수: 10 (최근 30일)
David Harra
David Harra 2022년 3월 11일
댓글: Voss 2022년 3월 11일
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 ))
  댓글 수: 2
David Harra
David Harra 2022년 3월 11일
Thank you so much. Perfect :)
Voss
Voss 2022년 3월 11일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by