Show constant in a Legend

Hello guys,
My Question is how i can show my constants like x=4 and z=5 in my Legend.

 Accepted Answer

DGM
DGM on 24 Mar 2021
Edited: DGM on 24 Mar 2021
You can use sprintf() to build the legend string from literal text and an assortment of variables. For example:
% dummy parameters
p=[1 2 3];
x=linspace(0,1,100);
for n=1:numel(p)
y=x.^p(n);
subplot(1,numel(p),n)
plot(x,y)
legend(sprintf('y=x^%d',p(n)),'location','northwest')
end
Yields us this plot:

2 Comments

Thank you for the answer but i just want to show the constants in my legend but not the graph of my constants. So i just work with the sprintf ?
If all you need is the legend, just use that. The rest is just context for sake of the example.

Sign in to comment.

More Answers (0)

Asked:

on 23 Mar 2021

Commented:

DGM
on 24 Mar 2021

Community Treasure Hunt

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

Start Hunting!