Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Legend Marker <-> Legend Text Spaces (Closer?,How?)

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Orkun OZENER

Date: 5 Feb, 2012 23:05:14

Message: 1 of 8

Hi,
Would it be possible to arrange the space between the marker and the text in legend. For my plot the space is big (there is a lot of space between marker and text).
I solved with using Adobe Photoshop (cutted the markers and made them closer:) but I need a quick an automatic way.that does not need any post processing.
King regards.

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Sadik

Date: 6 Feb, 2012 03:46:10

Message: 2 of 8

Hi Orkun,

You can try the following.

First, get the handle to your legend:

h = legend(...

Then, get its children:

hc = get(h,'Children');

Now the set of every third element is going to give you the handles of the text objects inside the legend and now you can play with the 'Position' properties of these text objects to get what you want. You had better first see what the position vector looks like [say, you look at your third entry in the legend, then we will look at the 9th child]

pos = get(hc(9),'Position');

Let's say that this returned [0.8 0.2 0]. Now, to make your text closer to the marker, you can play with the x component of the position vector [you should decrease it to make it closer to the marker]:

deltaX = 0.1; % Need to experiment with this to choose the best value for your case
pos2 = pos - [deltaX 0 0];
set(hc(9),'Position',pos2)

Best.

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Orkun OZENER

Date: 7 Feb, 2012 15:16:10

Message: 3 of 8

Dear Sadik,
First of all, Thanks for your reply.
 The code answers,

??? Error using ==> get
There is no 'Position' property in the 'hggroup' class.

Error in ==> plotBSFC at 87
pos = get(hc(9),'Position');

I am really really confused again..:) But I understand your answers analogy.But did not managed to make it worked.

My code script about legend is like that;

h = legend([S1 S2 S3 S4 S5],'D2','B10','B20','B50','B100'); % My variables
set(h,...
    'Position',[0.745273809523805 0.7655928571428572 0.133928571428571 0.126190476190476]); % Legend Main Box Position Setting.
set(h,'FontSize',5),'box','off');

M = findobj(h,'type','patch') % Find objects of type 'patch'
set(M,'MarkerSize',sqrt(16)) %Calculate marker size based on size of scatter points

hc = get(h,'Children');
pos = get(hc(9),'Position');

The code answers "hc" 10 x 1 matrix

807,000244140625
806,000244140625
804,000244140625
803,000244140625
801,000244140625
800,000244140625
798,000244140625
797,000244140625
795,000244140625
794,000244140625

Best Regards.

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: dpb

Date: 7 Feb, 2012 15:32:06

Message: 4 of 8

On 2/7/2012 9:16 AM, Orkun OZENER wrote:
...> h = legend([S1 S2 S3 S4 S5],'D2','B10','B20','B50','B100'); ...

...

 > hc = get(h,'Children');

Use the optional arguments of legend() to save the handles you need/want
directly instead of digging for them

doc legend

>
> pos = get(hc(9),'Position');
...

Who knows what the ninth handle actually is???

The other poster simply chose it at (more or less) random as an example.

Don't hard-code such stuff; either save the handles to the objects
directly (my recommendation) or query the type to ensure you are
operating on the desired object(s).

--

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Sadik

Date: 7 Feb, 2012 15:39:10

Message: 5 of 8

Prudent point from dpb.

Orkun, you can send your script together with the data to sadik.hava@gmail.com and I will take a look.

Best.

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Orkun OZENER

Date: 7 Feb, 2012 16:33:11

Message: 6 of 8

Thanks For All Answers..
I understand there is lot way to go:)(From my side)
I will send you the coder..
Best Regards...

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Orkun OZENER

Date: 7 Feb, 2012 17:09:10

Message: 7 of 8

I did it a little bit manuallay...But it is done...

%% LEGEND
h = legend([S1 S2 S3 S4 S5],'D2','B10','B20','B50','B100');
....
...
N=findobj(h,'type','text');
pos = get(N,'Position'); % I lookked at the position from matrix and changed them..:)

set(N(1),'Position',[0.493333333333333,0.135220125786164,0]);
set(N(2),'Position',[0.493333333333333,0.317610062893082,0]);
set(N(3),'Position',[0.493333333333333,0.500000000000000,0]);
set(N(4),'Position',[0.493333333333333,0.682389937106918,0]);
set(N(5),'Position',[0.493333333333333,0.864779874213837,0]);

Best Regards...
Thanks For Your All Help

"Orkun OZENER" <oozener@yildiz.edu.tr> wrote in message <jgrjo7$jqs$1@newscl01ah.mathworks.com>...
> Thanks For All Answers..
> I understand there is lot way to go:)(From my side)
> I will send you the coder..
> Best Regards...

Subject: Legend Marker <-> Legend Text Spaces (Closer?,How?)

From: Thomas

Date: 12 Nov, 2014 20:37:05

Message: 8 of 8

"Orkun OZENER" <oozener@yildiz.edu.tr> wrote in message <jgrlrm$rab$1@newscl01ah.mathworks.com>...
> I did it a little bit manuallay...But it is done...
>
> %% LEGEND
> h = legend([S1 S2 S3 S4 S5],'D2','B10','B20','B50','B100');
> ....
> ...
> N=findobj(h,'type','text');
> pos = get(N,'Position'); % I lookked at the position from matrix and changed them..:)
>
> set(N(1),'Position',[0.493333333333333,0.135220125786164,0]);
> set(N(2),'Position',[0.493333333333333,0.317610062893082,0]);
> set(N(3),'Position',[0.493333333333333,0.500000000000000,0]);
> set(N(4),'Position',[0.493333333333333,0.682389937106918,0]);
> set(N(5),'Position',[0.493333333333333,0.864779874213837,0]);
>
> Best Regards...
> Thanks For Your All Help
>
> "Orkun OZENER" <oozener@yildiz.edu.tr> wrote in message <jgrjo7$jqs$1@newscl01ah.mathworks.com>...
> > Thanks For All Answers..
> > I understand there is lot way to go:)(From my side)
> > I will send you the coder..
> > Best Regards...

Seems like this solution is not working under MATLAB 2014b anymore :/

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us