Pergunta

I have a figure with several lines and a corresponding legend. For example:

figure; hold all; 
plot(sin(1:0.1:10)); 
plot(cos(1:0.1:10)); 
legend('sin', 'cos');

I wonder if it is possible to change a text of a specific legend entry. If I do

hline = findobj(gcf, 'type', 'line');
legend(hline(5), 'new text')

the old legend disappears and a new one appears with just a single entry. How can I keep the whole legend and only change one entry?

Foi útil?

Solução

First, take a handle when you create the figure, or alternatively, extract the legend handle from the current figure:

h1 = legend('sin', 'cos');
h1 = findobj(gcf, 'tag', 'legend');

You can then retrieve the current text:

ltext = get(h1,'string');

ltext is a cell array of strings. Simply replace the one you want (presuming you know which number in the legend it is):

ltext{n} = 'new text';
set(h1,'string',ltext);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top