Using a variable to help fill in the legend line on a plot

1 view (last 30 days)
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)

DGM
DGM on 7 Nov 2021
Edited: DGM on 7 Nov 2021
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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!