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

1 回表示 (過去 30 日間)
BP
BP 2021 年 11 月 7 日
編集済み: DGM 2021 年 11 月 7 日
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

回答 (1 件)

DGM
DGM 2021 年 11 月 7 日
編集済み: DGM 2021 年 11 月 7 日
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

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by