How do you print an anonymous function properly in a figure title?
10 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203607/image.png)
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)))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203608/image.jpeg)
Accepted Answer
aara
on 11 Feb 2019
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 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!