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:
Using variables to create a legend

Subject: Using variables to create a legend

From: Joseph

Date: 29 Oct, 2010 15:13:03

Message: 1 of 7

I am looking at various aspects of concentration profiles in pipe flow at various radii. When I wrote the program I set the radius intervals at which I view the characteristics as a variable. Now I need to put a legend on the plot (to make any sense of the results) but can't seem to do it with a variable number of intervals (lines that need legend entries). I tried:

R = (0.0000001:R/RS:R)';

hold all

for k = 2:RS+1
plot(F(:,1),F(:,k));
z = legend('R(k)',k);
end

This is for k intervals where R is holding the values I need on the legend. It either gives me one 'R(k)' as a string entry (above) or an error when I remove the single quotes. how do I get around this? Thanks,

Joe

Subject: Using variables to create a legend

From: Sean

Date: 29 Oct, 2010 15:29:04

Message: 2 of 7

"Joseph " <jcremaldi@gmail.com> wrote in message <iaeo9v$of2$1@fred.mathworks.com>...
> I am looking at various aspects of concentration profiles in pipe flow at various radii. When I wrote the program I set the radius intervals at which I view the characteristics as a variable. Now I need to put a legend on the plot (to make any sense of the results) but can't seem to do it with a variable number of intervals (lines that need legend entries). I tried:
>
> R = (0.0000001:R/RS:R)';
>
> hold all
>
> for k = 2:RS+1
> plot(F(:,1),F(:,k));
> z = legend('R(k)',k);
> end
>
> This is for k intervals where R is holding the values I need on the legend. It either gives me one 'R(k)' as a string entry (above) or an error when I remove the single quotes. how do I get around this? Thanks,
>
> Joe

legend(sprintf('R(%d)',k))

Subject: Using variables to create a legend

From: someone

Date: 29 Oct, 2010 15:33:04

Message: 3 of 7

"Joseph " <jcremaldi@gmail.com> wrote in message <iaeo9v$of2$1@fred.mathworks.com>...
> I am looking at various aspects of concentration profiles in pipe flow at various radii. When I wrote the program I set the radius intervals at which I view the characteristics as a variable. Now I need to put a legend on the plot (to make any sense of the results) but can't seem to do it with a variable number of intervals (lines that need legend entries). I tried:
>
> R = (0.0000001:R/RS:R)';
>
> hold all
>
> for k = 2:RS+1
> plot(F(:,1),F(:,k));
> z = legend('R(k)',k);
> end
>
> This is for k intervals where R is holding the values I need on the legend. It either gives me one 'R(k)' as a string entry (above) or an error when I remove the single quotes. how do I get around this? Thanks,
>
> Joe

% Perhaps something like:

z = legend(num2str(R(k)),num2str(k));

Subject: Using variables to create a legend

From: Steven_Lord

Date: 29 Oct, 2010 15:58:00

Message: 4 of 7



"Joseph " <jcremaldi@gmail.com> wrote in message
news:iaeo9v$of2$1@fred.mathworks.com...
> I am looking at various aspects of concentration profiles in pipe flow at
> various radii. When I wrote the program I set the radius intervals at
> which I view the characteristics as a variable. Now I need to put a
> legend on the plot (to make any sense of the results) but can't seem to
> do it with a variable number of intervals (lines that need legend
> entries). I tried:
>
> R = (0.0000001:R/RS:R)';
>
> hold all
>
> for k = 2:RS+1
> plot(F(:,1),F(:,k));
> z = legend('R(k)',k);
> end
>
> This is for k intervals where R is holding the values I need on the
> legend. It either gives me one 'R(k)' as a string entry (above) or an
> error when I remove the single quotes. how do I get around this? Thanks,

I would set the DisplayName property for each of your lines (and store each
line's handle in an element of a vector) then pass the vector of handles
into LEGEND on its own.

x = 0:0.1:2*pi;
hold on
axis([0 2*pi -1 1]);
h = zeros(1, 5);
colors = [1 0 0;0 1 0; 0 0 1; 1 1 0; 0 1 1];
for k = 1:5
    h(k) = plot(x, sin(k*x), 'Color', colors(k, :), ...
        'DisplayName', sprintf('x-vs-sin(%d*x)', k));
end
legend(h)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Using variables to create a legend

From: Simon

Date: 29 Apr, 2011 15:30:39

Message: 5 of 7

I agree with Steve Lord, but you could change one line to "automatizate everything:
x = 0:0.1:2*pi;
hold on
axis([0 2*pi -1 1]);
h = zeros(1, 5);
colors =colormap(hsv(5)); ;
for k = 1:5
h(k) = plot(x, sin(k*x), 'Color', colors(k, :), ...
'DisplayName', sprintf('x-vs-sin(%d*x)', k));
end
legend(h)

Simon

Subject: Using variables to create a legend

From: Laura

Date: 2 Nov, 2012 01:39:08

Message: 6 of 7

Thanks for the info, I would like to do something really similar to this post but instead of having a single plot I have 2 subplots.
I have tried to adapt this code for it to generate a legend in one of them or the other or both (I don't really care) that can change with a parameter but everything failed. Any ideas?
thanks a lot!
Laura

Simon <user@compgroups.net/> wrote in message <yI6dnU6AxKECRCfQnZ2dnUVZ_gydnZ2d@giganews.com>...
> I agree with Steve Lord, but you could change one line to "automatizate everything:
> x = 0:0.1:2*pi;
> hold on
> axis([0 2*pi -1 1]);
> h = zeros(1, 5);
> colors =colormap(hsv(5)); ;
> for k = 1:5
> h(k) = plot(x, sin(k*x), 'Color', colors(k, :), ...
> 'DisplayName', sprintf('x-vs-sin(%d*x)', k));
> end
> legend(h)
>
> Simon
>

Subject: Using variables to create a legend

From: dpb

Date: 2 Nov, 2012 13:17:29

Message: 7 of 7

On 11/1/2012 8:39 PM, Laura wrote:
> Thanks for the info, I would like to do something really similar to this
> post but instead of having a single plot I have 2 subplots.
> I have tried to adapt this code for it to generate a legend in one of
> them or the other or both (I don't really care) that can change with a
> parameter but everything failed. Any ideas?
...

Well, I'm sure not "everything" failed but what, specifically, is a
little hard to tell w/o any code to see what you actually tried amongst
the everythings...

You'll have to select the desired subplot, of course, but other than
that the process would be essentially identical to the posted code--

--

Tags for 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