How do you print an anonymous function properly in a figure title?

23 ビュー (過去 30 日間)
KieranSQ
KieranSQ 2019 年 2 月 11 日
コメント済み: KieranSQ 2019 年 2 月 11 日
I am trying to print an anonymous function as part of a title for a plot. I would like it to look like a latex entry where but we get f(x)=@(x)x.^2 instead.
I have provided a MWE below;
f=@(x) x.^2
x_coordinates=[1:10]
fx=f(x_coordinates)
plot(x_coordinates,fx)
title(sprintf('The Evolution of the Heat Equation with Source f(x)=%s', func2str(f)))
  2 件のコメント
KieranSQ
KieranSQ 2019 年 2 月 11 日
Many thanks for your comment, but this does not wokr for a function handle - is there anything that would do this job?

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

採用された回答

aara
aara 2019 年 2 月 11 日
You could do it by manipulating the equation of the function alone and then adding it to the remaining text:
f=@(x) x.^2
x_coordinates=[1:10]
fx=f(x_coordinates)
plot(x_coordinates,fx)
eqn = func2str(f);%changes to a char array
eqn2 = eqn(5:end);%removes the '@(x) handle
eqn3 = replace(eqn2,'.','') %removes the . used for multiplication
placeholder1 = "The Evolution of the Heat Equation with Source $f(x)=" + string(eqn3+ "$")
title(placeholder1,'Interpreter','latex')
Not sure if this works for all equations.
  2 件のコメント
KieranSQ
KieranSQ 2019 年 2 月 11 日
Thank you for this - yes it works for this particular case. The issue now would be if there is a decimal present as a scaling.
KieranSQ
KieranSQ 2019 年 2 月 11 日
I have now found a way to remove certain parts of the string without losing others - we would use something like this,
eqn = func2str(fu);%changes to a char array
eqn2 = eqn(5:end);%removes the '@(x) handle
eqn2=strrep(eqn2,'.*','')
eqn3=strrep(eqn2,'.^','^')
eqn3=strrep(eqn2,'./','/')
Many thanks for your help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by