How can I put in legend all cells from one cell array ?

17 views (last 30 days)
Hello,
I have a 20x1 cell array:
g=
'1:54.593'
'1:58.820'
'1:55.448'
'1:55.785'
'1:56.270'
'1:55.259'
'1:58.936'
'1:55.204'
'1:55.479'
'1:56.305'
'1:56.736'
'1:55.781'
'1:57.219'
'1:55.797'
'1:59.418'
'1:53.254'
'1:55.199'
'1:58.210'
'1:54.657'
'1:52.684'
I would like to have this array in legend of one graph with all times in column like it is now. I tried to put:
str = strcat(g(:,1));
legend (str{:})
But it says there is too many input strings so it put in legend just the first time.
I also tried:
legend (g(:,1))
But it's showing the last time on the last cell.
Do you have a solution for this problem ?
Thank you,
Martin

Answers (3)

Jan
Jan on 21 Feb 2018
Edited: Jan on 21 Feb 2018
Do you mean:
legend(g)
? But for a {20 x 1} cell, this should be the same as g(:,1). Therefore I do not understand, what "it's showing the last time on the last cell" means. Please post some code, which produces the input and the legend command you used. A screenshot of what you get as output might be useful also.

Walter Roberson
Walter Roberson on 21 Feb 2018
You only have one graphic object in your axes. You can only put in one legend entry. You could strjoin(g, char(10)) to create a single string with newlines in it, but you will find that you need to use a custom Position option to make the area big enough as the default would be only one line high because it knows that you are passing in only one object.
If you are thinking that you are plotting multiple objects but MATLAB only knows it as one object then you are probably using a scatter plot or a bar plot with only one group.
You should consider whether it really makes sense to legend multiple times for a single graphics object. If you are using a scatter plot with different colors corresponding to different groups, then one legend per color would make sense from an information perspective, but the way to handle that situation is to use gscatter() which creates one graphic object per group.

Martinppp
Martinppp on 21 Feb 2018
Here is my graph (there are other problems but don't pay attention of those).
I just have in X axis the 'z' and in Y axis the 'g'. But there is just one cell array in column from 'g' and one numeric matrix in line from 'z' so I cannot plot further time and put a legend for each.
And the thing I want is all information in the cell array in legend.
  1 Comment
Walter Roberson
Walter Roberson on 21 Feb 2018
For a bar graph I don't think it makes sense to legend multiple entries unless you set the CData property to control the color of the individual bars.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!