Azzera filtri
Azzera filtri

how to hold legend

45 visualizzazioni (ultimi 30 giorni)
Elysi Cochin
Elysi Cochin il 26 Ott 2013
Commentato: Shyam Jani il 1 Set 2022
how to hold legend... the plot is coming correctly... but the legend is coming only for the last one.... how to hold the legend....
col = hsv(9);
for i = 1:cs
plot(x, y, 'LineWidth', 2, 'color', col(j,:)); hold on
legendInfo{i} = Info{i};
legend(legendInfo, 'Location', 'Best'); hold all
end

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 26 Ott 2013
Modificato: Azzi Abdelmalek il 26 Ott 2013
Look at this Example
t=0:0.1:10;
y1=sin(t);
y2=cos(t);
% ------Plot the first data----------
plot(t,y1);
h=legend('word1')
%-------Plot the second data---------
hold on
plot(t,y2);
str=get(h,'string')% get the previous legend
new_leg='word2' % your new legend
h=legend([str new_leg]) % concatenate the new and the previous legend
  2 Commenti
Rajani Metri
Rajani Metri il 5 Dic 2020
Sir,
But it is not holding the Marker of those legends. What to do to hold the marker in legend?
Thank you.
Shyam Jani
Shyam Jani il 1 Set 2022
Hello,
To update the Legend box try this code. The code@Azzi Abdelmalek has written is 80% correct just you need to update few lines (see below)
t=0:0.1:10;
y1=sin(t);
y2=cos(t);
% ------Plot the first data----------
plot(t,y1);
h=legend('word1')
%-------Plot the second data---------
hold on
plot(t,y2);
str=get(h,'string')% get the previous legend
%------These lines you should update--------%
str(end) = [] % delete last element of the cell arrray.
str{end+1} = "word2"; % Adding string
h = legend([str]); % Updating legend box

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by