Code covered by the BSD License  

Highlights from
legend1.m

Be the first to rate this file! 9 Downloads (last 30 days) File Size: 1.24 KB File ID: #39505

legend1.m

by

 

17 Dec 2012 (Updated )

Control line and text entries in a legend object, individually.

| Watch this File

File Information
Description

In the case of a multi-line plot where individual lines are created using a loop, one can easily control each line's properties, but it is less clear how to control the properties of each legend entry. This function gives control over some basic properties of individual legend entries: line color, text, text color, text font size.

Example:

nentries = 4;
colors = [0 0 1; 1 0 0; 0 1 0; 0 0 0]; % number of lines needs to match nentries
leglabels = {'Blue';'Red';'Green';'Black'}; % number of cells needs to match nentries

% create 4 curves
t = [1:100];
for ic=1:nentries
    curve(ic,:) = rand(1,length(t)) + [t(1):t(end)]*0.1*ic;
end

% plot curves using for-end loop, insert legend
figure
for ic=1:nentries
    plot(curve(ic,:),'Color',colors(ic,:))
    hold on
end
legend1 (leglabels,colors,12)

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
22 Dec 2012 Giorgos Papakonstantinou

It seems to be a very useful function Stavros. It would be nice to give us an example. I am trying to use it in combination with str2cell function but I didn't have any success yet.
Your function seems to me very useful, since I was always trying to control both the output of the legend depending on the for loop iterations. I asked the same question in the past in the following thread:

http://www.mathworks.com/matlabcentral/answers/49426-plot-curves-with-different-colors-inside-the-for-loop-and-calculation-of-integral-with-trapz

Updates
26 Dec 2012

Fixed error with output handle.

Contact us