Using a variable to help fill in the legend line on a plot
Show older comments
I have a variable that is named in the first lines of my program. It changes each day. I need the variable to fill in a part of legend text.
The variable is below.
Volts="25";
The code for the legend is below.
legend('25V Open circuit 10 to 90% Rise Time');
I need to know how to make it replace only the 25 in the legend with the variable number in enter Volts="XX";
Thank you
Answers (1)
You can use sprintf() to construct a bit of text from numeric variables or other string/char variables. Then you can use that in your legend or title as needed.
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
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!