Use Legend to show array and matrix

2 views (last 30 days)
I have one array and one matrix like A={'x' 'y' 'z'}; B=[1 2 3]. I want to make a legend as: x=1 y=2 z=3
in my plot, could anyone help me with this issue.
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 17 Dec 2013
legend( arrayfun(@(K) sprintf('%s=%d',A{K}, B(K)), 1:length(A), 'UniformOutput', false) )
  4 Comments
Wei
Wei on 17 Dec 2013
It said: Warning: Ignoring extra legend entries.
Walter Roberson
Walter Roberson on 17 Dec 2013
You can only have one legend entry per plotted item (e.g., one per line).
How are you doing your plotting, and what are the size() of the matrices you are plotting ?

Sign in to comment.

More Answers (1)

Honglei Chen
Honglei Chen on 16 Dec 2013
You can do this:
plot(rand(10,3))
legend(cellfun(@(x,y)sprintf('%s=%s',x,y),A,B,'UniformOutput',false))
  8 Comments
Wei
Wei on 17 Dec 2013
Thanks a lot! I got the legend but there is only x=1 without other two.
Wei
Wei on 17 Dec 2013
It said: Warning: Ignoring extra legend entries.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!