Hi all,
I am looking for a way to insert number coding of both the legend and the curves of the a graph with plot command. like attached figure ( I added numbers manually) do you know any way to do that. thanks for your time and help.number_coding plot.png

 Akzeptierte Antwort

Star Strider
Star Strider am 2 Feb. 2020

0 Stimmen

It is likely not possible to do exactly that, however you can come close.
Try this:
x = [1 10 14 17];
y = randi([-40 -11], 4, 6);
figure
plot(x, y, '.-')
text(ones(1, size(y,2)), y(1,:), compose('%d', 1:size(y,2)))
lgndc = compose('%d Curve %d', (1:size(y,2)).'*[1 1]);
legend(lgndc, 'Location','SW')
ylim([-45 max(ylim)])
You will need to produce your own version of ‘lgndc’. Just precede each entry by the number of the curve.

6 Kommentare

Sho Gan
Sho Gan am 2 Feb. 2020
thanks Star Strider, when I run your code I get the following error for the text(...) command:
" String argument expected after 2 or 3 numeric arguments"
any idea where does it come from?
Star Strider
Star Strider am 2 Feb. 2020
My pleasure.
I get no errors when I run it.
Please post the complete error message (all the red text), including the line that threw that error.
Sho Gan
Sho Gan am 2 Feb. 2020
error.PNG
I changed compose to sprintfc (undocumented, everyone has it).
Try this:
x = [1 10 14 17];
y = randi([-40 -11], 4, 6);
figure
plot(x, y, '.-')
text(ones(1, size(y,2)), y(1,:), sprintfc('%d', 1:size(y,2)))
lgndc = sprintfc('%d Curve %d', (1:size(y,2)).'*[1 1]);
legend(lgndc, 'Location','SW')
ylim([-45 max(ylim)])
Again, it ran for me without error (R2019b).
Sho Gan
Sho Gan am 2 Feb. 2020
thank you very much it works perfect :)
Star Strider
Star Strider am 2 Feb. 2020
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by