How to change the properties of legend?

INTRO: Hi, I use the command pulsewidth(data,time) to display the width of peaks as described in the example below:
clear all; close all;
% DATA GENERATION
Fs = 1000;
time = 0:1/Fs:1;
Frq = 10;
data = sin(2*pi*time*Frq);
% CALCULATE AND PLOT PULSEWIDTH
pulsewidth(data,time);figure(gcf);
legend('Location', 'NorthEast');
% PLOT PROPERTIES
box on;
set(gca,'FontSize',18, 'FontName','Arial', 'FontWeight','bold','Color','w','LineWidth',3);
xlabel('TIME','FontSize',18, 'FontName','Arial','FontWeight','bold');
ylabel('AMPLITUDE','FontSize',18, 'FontName','Arial','FontWeight','bold');
scrsz = get(0,'ScreenSize'); set(gcf,'Position',scrsz,'PaperPositionMode','auto');
QUESTION: How can I change the thickness of the lines and the sizes of the symbols displayed in the legend?
This legend is generated automatic with the command pulsewidth and I don't have access to the data. The only thing I could do until now was to fix the location of the legend.
I hope someone knows how to help me.
Thanks in advance
Emerson

 Accepted Answer

dpb
dpb on 16 Aug 2013
From the doc for legend --
LEGH = legend returns the handle to legend on the current axes or empty if none exists.
So, after your call to pulsewidth, include
lh=legend; % return the legend handle
then you can do whatever you want w/ the properties thereof

6 Comments

Hi dpb, thank you for your help.
I included lh=legend as you suggested but still don't know how to change the line thickness, size of symbols and colors of objects displayed in legend.
If I would have plotted data against time, I would specify all properties in the same command line of the plot and they would appear in legend.
In case of pulsewidth, I don't know how to change the color gray of the patch pulse width, the line blue of the curve signal,the size of the symbol x mid cross, and so on...
Do you know which commands I have to write to make these changes?
Thanks in advance for your attention.
...[LEGH,OBJH,OUTH,OUTM] = legend(...) returns a handle LEGH to the legend axes; a vector OBJH containing handles for the text, lines, and patches in the legend; a vector OUTH of handles to the lines and patches in the plot; and a cell array OUTM containing the text in the legend.
Once you have those handles you can get / set any properties of the legend and/or plot desired.
I've not experimented directly whether if you change the properties in the plot those will automagically be reflected in the lagend or not.
You can also, of course, use
get(gca,'children')
to return the handle of the objects of the current axes object children as well.
You might find the interactive edit tool useful in exploring the entities from which you can then write code to automate the changes desired.
You can, of course, always try and see if the pulsewidth function was written cleverly enough to pass property name/value pairs along.
I've never used it so don't know a thing about it--your question prompted me to actually see if it existed here--does but another one I've not had time to explore.
I typed get(gca,'children') in the command window and it returned a list of 10 numbers. Does it have to do with the ten items displayed in legend? What can I do with these numbers?
You need to read up on handle graphics and enhancing plots with set and get
The 10 numbers are the 'handles' of the objects that are the children of the axes object -- iow, the lines, patches, etc., etc., etc., ... or perhaps the higher level group in some instances.
After you have those handles, then you can ascertain which is what by
get(hchildren,'type')
which will give a list of what the various handles are...
If I do something really trivial here...
>> plot(rand(3,5)) % a simple plot of 5 lines
>> hc=get(gca,'children') % get the children handles
hc =
183.0291
182.0291
181.0291
180.0291
179.0291
>> get(hc,'type') % see what the handles represent
ans =
'line'
'line'
'line'
'line'
'line'
>> get(get(gca,'children'),'type') % same thing except nested call
ans =
'line'
'line'
'line'
'line'
'line'
>> get(hc(1)) % see what properties are for the first child...
DisplayName: ''
Annotation: [1x1 hg.Annotation]
Color: [1 0 0]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
XData: [1 2 3]
YData: [0.4756 0.3625 0.7881]
ZData: [1x0 double]
BeingDeleted: 'off'
ButtonDownFcn: []
Children: [0x1 double]
Clipping: 'on'
CreateFcn: []
DeleteFcn: []
BusyAction: 'queue'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
Selected: 'off'
SelectionHighlight: 'on'
Tag: ''
Type: 'line'
UIContextMenu: []
UserData: []
Visible: 'on'
Parent: 173.0291
XDataMode: 'auto'
XDataSource: ''
YDataSource: ''
ZDataSource: ''
>>
What you do is use those handles to set() the desired properties as wanted. As said, you can use the figure editor to explore as well and to see how changing something affects the plot appearance interactively.
Thank you, I read your suggestions and tried different things until it worked.
That's the way to get somewhere w/ handle graphics--seems daunting 'cuz there's so much folderol to deal with with the large number of objects and properties and there's no way to find in documentation except by a very deep hunt keeping on drilling down so all one can do is dig in and start...
But, once you do it a time or two you get the hang of the general idea and then it's just "handle diving" to find what it is they called whatever the property is that does the effect desired for the most part...

Sign in to comment.

More Answers (0)

Products

Tags

Community Treasure Hunt

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

Start Hunting!