Filter löschen
Filter löschen

how to hold legend

45 Ansichten (letzte 30 Tage)
Elysi Cochin
Elysi Cochin am 26 Okt. 2013
Kommentiert: Shyam Jani am 1 Sep. 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

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 26 Okt. 2013
Bearbeitet: Azzi Abdelmalek am 26 Okt. 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 Kommentare
Rajani Metri
Rajani Metri am 5 Dez. 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 am 1 Sep. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by