フィルターのクリア

a function that draws a plot: use a variable name for a legend

3 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 5 月 31 日
編集済み: alpedhuez 2020 年 5 月 31 日
I have a data for
days
temperature
I want to write a function that plot(x,y) that, when one inputs plot(days,temperature), that outputs
plot(days, temperature)
legend('days','temperature')
So I start with
function plottest(x,y)
plot(x,y)
end
Now how should I write
legend
?

採用された回答

And_Or
And_Or 2020 年 5 月 31 日
If you want to get the variable name inside a function, you can use the "inputname" function:
function plottest(x,y)
plot(x,y)
legend(inputname(1),inputname(2))
end
However, in your plot you only have one "y" variable, so it does not make sense adding several legends. Maybe you want to change the label of the axes? In this case:
function plottest(x,y)
plot(x,y)
xlabel(inputname(1))
ylabel(inputname(2))
end

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by