Use Legend to show array and matrix

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

 採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 17 日

0 投票

legend( arrayfun(@(K) sprintf('%s=%d',A{K}, B(K)), 1:length(A), 'UniformOutput', false) )

4 件のコメント

Walter Roberson
Walter Roberson 2013 年 12 月 17 日
Alternately,
legend( strcat(A, '=', num2str(B(:)) ) )
Wei
Wei 2013 年 12 月 17 日
Thanks guys! I can have one legend now... But actually I just have x=1 without other two legends. Do you know how to modify?
Thanks
Wei
Wei 2013 年 12 月 17 日
It said: Warning: Ignoring extra legend entries.
Walter Roberson
Walter Roberson 2013 年 12 月 17 日
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 ?

サインインしてコメントする。

その他の回答 (1 件)

Honglei Chen
Honglei Chen 2013 年 12 月 16 日

0 投票

You can do this:
plot(rand(10,3))
legend(cellfun(@(x,y)sprintf('%s=%s',x,y),A,B,'UniformOutput',false))

8 件のコメント

Wei
Wei 2013 年 12 月 16 日
Thanks for your reply. I am sorry I just modified my question and could you please take look at it again?
Thanks
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 16 日
Why have you changed your question?
Wei
Wei 2013 年 12 月 16 日
I made some change of my code...
Honglei Chen
Honglei Chen 2013 年 12 月 16 日
Your B now contains numbers, replace the second %s in my code to %d if they are integers
Wei
Wei 2013 年 12 月 16 日
It always give me this: Error using cellfun Input #2 expected to be a cell array, was double instead.
Honglei Chen
Honglei Chen 2013 年 12 月 17 日
You change the second one to a regular array, you can use Walter's code below
Wei
Wei 2013 年 12 月 17 日
Thanks a lot! I got the legend but there is only x=1 without other two.
Wei
Wei 2013 年 12 月 17 日
It said: Warning: Ignoring extra legend entries.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

Wei
2013 年 12 月 16 日

コメント済み:

2013 年 12 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by