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:
editing legends

Subject: editing legends

From: C?sar Ghali

Date: 14 Jan, 2009 16:40:03

Message: 1 of 2

Dear friends,

I use the semilogy funtion to draw a curve on an axes control. I want to run the code more than one time to draw many curves on the same control without deleteing the old curves. My question is how can I modify the legend of the axes control in order to add curve's details (strings) to the previous ones.

Any help please???


Cesar Ghali

Subject: editing legends

From: Sadik

Date: 14 Jan, 2009 17:51:02

Message: 2 of 2

"C?sar Ghali" <ceso222@hotmail.com> wrote in message <gkl4h3$scb$1@fred.mathworks.com>...
> Dear friends,
>
> I use the semilogy funtion to draw a curve on an axes control. I want to run the code more than one time to draw many curves on the same control without deleteing the old curves. My question is how can I modify the legend of the axes control in order to add curve's details (strings) to the previous ones.
>
> Any help please???
>
>
> Cesar Ghali

Dear Cesar,

In fact, you may find it relatively simple.

If you would like to do that, whenever you put a legend, you should use its handle like this:

h = legend('string1');

rather than only

legend('string1')

This way, you will have the power to edit the string of the legend any time.

Let us say, you will now hold on this figure and plot another curve, whose label will appear as 'string2' in the legend. Then you simply say,

previousLegendText = get(h,'String');
legend(h,[previousLegendText'; {'string2'}]);

The transpose on the previuosLegendText variable and the other cell type concatenation are technical details you may already know, but should remain as indicated above. If we generalize, if you would like to make N such plots [I will use 1 for the first curve, 2 for the second and so on]:

plot()... first curve here
hold on
h = legend('1');
previousLegendText = get(h,'String')

for n = 2:N
     plot() ............ you can plot nth curve here
     legend(h,[previousLegendText';{num2str(n)}])
end

Hope this helps. Please let me know if you have any bugs in the above code. My email: sadik.hava@gmail.com

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