How to Add Title to Legend ?
86 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to add a title to legend, as shown in the picture inside the red circle. How do I do that, knowing that I am using Matlab 2013 ?
I tried several times to implement it, but I did not achieve any results...
This is the code I want to execute....
% Clear workspace.
clear all
% Clear command window.
clc
% Close all windows.
close all
% Compute Min Separation (dpt) two sources when pt1=2 and dx=0.5 and z=30
N = 3:1:20;
dpt1 = [7.75, 5.4, 4.13, 3.35, 2.82, 2.43, 2.14, 1.91, 1.72, 1.57, 1.44, 1.333, 1.24, 1.16, 1.088, 1.025, 0.97, 0.918];
% Compute Min Separation (dpt) two sources when pt1=2 and dx=0.5 and z=50
dpt2 = [12.88, 8.97, 6.884, 5.581, 4.695, 4.05, 3.56, 3.18, 2.87, 2.615, 2.405, 2.222, 2.068, 1.932, 1.815, 1.709, 1.615, 1.532];
% Compute Min Separation (dpt) two sources when pt1=2 and dx=0.5 and z=70
dpt3 = [18.05, 12.55, 9.63, 7.812, 6.57, 5.67, 4.99, 4.453, 4.02, 3.662, 3.363, 3.113, 2.895, 2.705, 2.54, 2.394, 2.263, 2.145];
figure(1);
grid on;
title('Minimum Separation (dpt)');
xlabel('NO. of Antenna');
ylabel('Minimum Separation (dpt)');
hold on;
plot(N, dpt1, 'r');
plot(N, dpt2, 'g');
plot(N, dpt3, 'b');
hold off;
lgd = legend(sprintf('pt1 = %g cm, \\lambda = %g cm, \\Deltax = %g cm, Z = %g cm ', 2, 0.4, 0.5, 30),...
sprintf('pt1 = %g cm, \\lambda = %g cm, \\Deltax = %g cm, Z = %g cm ', 2, 0.4, 0.5, 50),...
sprintf('pt1 = %g cm, \\lambda = %g cm, \\Deltax = %g cm, Z = %g cm ', 2, 0.4, 0.5, 70));
title(lgd,' Min Separation (dpt) ');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1561239/image.png)
8 commentaires
madhan ravi
le 6 Déc 2023
Déplacé(e) : madhan ravi
le 6 Déc 2023
Thanks sir Walter, didn’t know about the NodeChildren.
Réponses (1)
Steven Lord
le 5 Déc 2023
You're using a release of MATLAB that's more than ten years old. MATLAB has changed quite a bit during that decade, including a major series of changes to the Handle Graphics functionality in release R2014b. The legend function had its behavior change significantly with that series of changes if I recall correctly.
The following code worked for me in release R2013a. It does not look the same in that release as it does in newer releases (as I said above, major series of changes) but it did create a title. I don't know if there is a way to achieve the exact same appearance in the release you're using as in newer releases.
plot(1:10)
legh = legend('line y = x');
t = get(legh, 'Title');
set(t, 'String', 'hello')
But I strongly encourage you to consider upgrading to a newer release of MATLAB if it is possible.
3 commentaires
Steven Lord
le 6 Déc 2023
As I said, I don't know if there is a way to achieve the exact same appearance in the release you're using as in newer releases. The easiest way to get the legend to appear the way it does in more recent releases likely would be to upgrade to a more recent release.
Voir également
Catégories
En savoir plus sur Graphics Objects dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!