From: Zukri on
Hi.

I have a matrix U = (8, 10, 12 , 14)

I would like to shown them in a graph as a legend by using the Matrix 'U' i.e. (8 is for the first line, 10 is the second line and so on). As oppose to simply typing --> legend('8','10','12','14').

This is because Matrix 'U' is input by the user and changes the legend.

How do I do this?
From: Walter Roberson on
Zukri wrote:

> I have a matrix U = (8, 10, 12 , 14)
> I would like to shown them in a graph as a legend by using the Matrix
> 'U' i.e. (8 is for the first line, 10 is the second line and so on). As
> oppose to simply typing --> legend('8','10','12','14').
> This is because Matrix 'U' is input by the user and changes the legend.
>
> How do I do this?

tUs = str2num(U.');
legend(tUs{:});
From: Zukri on
Thanks a lot, Walter. :) However, I've still got a problem. Here's what I've got from MatLab:

U =
10 12 14 16

Name Size Bytes Class Attributes
U 1x4 32 double

So I've applied as mentioned;
> tUs = str2num(U.');
> legend(tUs{:});
and the error - Requires string or character array input.


So im guessing that my U is already in string characters since I did put 'str2num' earlier. So i went straight to
> legend(U{:});
and the error - ??? Cell contents reference from a non-cell array object.

I dont understand what the error means. Also, what does U{:} mean? Thanks alot!
From: Paul on
"Zukri " <buster_2020(a)hotmail.com> wrote in message <i1esc8$a7q$1(a)fred.mathworks.com>...
> Thanks a lot, Walter. :) However, I've still got a problem. Here's what I've got from MatLab:
>
> U =
> 10 12 14 16
>
> Name Size Bytes Class Attributes
> U 1x4 32 double
>
> So I've applied as mentioned;
> > tUs = str2num(U.');
> > legend(tUs{:});
> and the error - Requires string or character array input.
>
>
> So im guessing that my U is already in string characters since I did put 'str2num' earlier. So i went straight to
> > legend(U{:});
> and the error - ??? Cell contents reference from a non-cell array object.
>
> I dont understand what the error means. Also, what does U{:} mean? Thanks alot!

I think he ment:
tUs = num2cell(U.');
and not:
tUs = str2num(U.');

It's just a guess. Try it this way.
From: Walter Roberson on
Zukri wrote:
> Thanks a lot, Walter. :) However, I've still got a problem. Here's what
> I've got from MatLab:
>
> U =
> 10 12 14 16
>
> Name Size Bytes Class Attributes
> U 1x4 32 double
> So I've applied as mentioned;
>> tUs = str2num(U.');
>> legend(tUs{:});
> and the error - Requires string or character array input.

tUs = cellstr(str2num(U.'));
legend(tUs{:});

should do it.

> Also, what does U{:} mean?

F(V{:}) is the same as if you had written

F(V{1}, V{2}, V{3}, ... V{end})

provided that V is a cell array. See the Getting Started section of the
documentation for more information about cell arrays.