How to Add Title to Legend ?

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) ');

8 Comments

madhan ravi
madhan ravi on 5 Dec 2023
Moved: madhan ravi on 6 Dec 2023
https://de.mathworks.com/matlabcentral/answers/324848-setting-a-title-for-a-legend perhaps one of those answers in the link if not simply use MATLAB Online for latest version
Note that 2013 was before the graphics upgrade of R2014b. At the time, legend() worked differently.
Walter Roberson
Walter Roberson on 5 Dec 2023
Moved: madhan ravi on 6 Dec 2023
Note that the stuff about NodeChildren mentioned in some of those responses, did not exist at all in 2013.
Possibly the legendflex contribution might help.
Muhammad Salem
Muhammad Salem on 5 Dec 2023
Moved: madhan ravi on 6 Dec 2023
I have updated the question above. Please review it again.
The problem has not been solved yet. I have tried many codes, but they do not work and I do not know the cause of the problem ?!!
I didn't understand you well, can you explain more?
You indicated you are using a 2013 release of MATLAB.
In fall of 2014, a major upgrade of the MATLAB graphics system was released.
In the time since then, the internal implementation of legend() has changed a lot. The discussions at the link that @madhan ravi posted are mostly irrelevant for you, as they mostly talk about the internal implementation after 2014. But possibly the legendflex() File Exchange contribution might work for you.
madhan ravi
madhan ravi on 6 Dec 2023
Moved: madhan ravi on 6 Dec 2023
Thanks sir Walter, didn’t know about the NodeChildren.
Thank you very much for your efforts.

Sign in to comment.

Answers (1)

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 Comments

Can I enter the word hello inside the rectangle because it is outside ?
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.
Thank you so much.

Sign in to comment.

Products

Release

R2013a

Tags

Asked:

on 5 Dec 2023

Commented:

on 6 Dec 2023

Community Treasure Hunt

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

Start Hunting!