Clear Filters
Clear Filters

how to hold legend

45 views (last 30 days)
Elysi Cochin
Elysi Cochin on 26 Oct 2013
Commented: Shyam Jani on 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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 26 Oct 2013
Edited: Azzi Abdelmalek on 26 Oct 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 Comments
Rajani Metri
Rajani Metri on 5 Dec 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 on 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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!