name of a variable in legend, matlab

1.9k views Asked by At

I want the name of my variables in a plot legend in matlab. Is this possible?

my example is:

function example(x1)
    figure;
    plot(x1.time, x1.value);
    legend(x1);
end

now I want to call my function like:

>> example(myvariable)

my legend should look like:

---------------
| -- myvariable |
---------------

thanks for your help guys.

1

There are 1 answers

0
Suever On BEST ANSWER

You can use inputname to get the name of the input in the calling workspace and then pass this to legend or set the DisplayName property of the plot to this value.

function example(x1)
    figure
    plot(x1.time, x1.value)
    legend(inputname(1))
end