Show constant in a Legend

6 views (last 30 days)
nikola oljaca
nikola oljaca on 23 Mar 2021
Commented: DGM on 24 Mar 2021
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
DGM
DGM on 24 Mar 2021
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)

Community Treasure Hunt

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

Start Hunting!