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 Center および File ExchangeGrid 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