Automatically changing title based on a function input variables names

10 ビュー (過去 30 日間)
PANAGIOTIS GEORGIOS ILIOPOUOS
PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 4 月 30 日
コメント済み: Voss 2022 年 4 月 30 日
I made a function that creates a histogram.I want every time i call the function for an input variable 'X' ,the title and the xlabel to change automatically.
For exaple:
  • when i i call the function [HISTOGRAM]=hist(apples),then my title should be 'HISTOGRAM apples'
  • when i i call the function [HISTOGRAM]=hist(lemons),then my title should be 'HISTOGRAM lemons
The same for xlabel.
Is this possible?
below is the code i have already written.
function [HISTOGRAM]=hist(X)
HISTOGRAM=histogram(X)
title('HISTOGRAM (VARIABLE NAME)')
xlabel('(VARIABLE NAME)')
ylabel('Number of sequence reads')
end
  1 件のコメント
dpb
dpb 2022 年 4 月 30 日
Look at
doc inputname
and see if that gives you any ideas...

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 4 月 30 日
You could add a second input argument to your function (and don't call your function hist because that's already taken by a built-in MATLAB function):
my_hist(randn(100),'jackfruit');
function [HISTOGRAM]=my_hist(X,var_name)
HISTOGRAM=histogram(X);
title(sprintf('HISTOGRAM (%s)',var_name))
xlabel(sprintf('(%s)',var_name))
ylabel('Number of sequence reads')
end
  2 件のコメント
PANAGIOTIS GEORGIOS ILIOPOUOS
PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 4 月 30 日
編集済み: PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 4 月 30 日
Very insightful... thank you!!!
Voss
Voss 2022 年 4 月 30 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by