How to get the string form of an anonymous function?

2 views (last 30 days)
I was thinking about creating a function, which automatically creates plot, data, etc. This function is supposed to be an aid for solving differential equations with numerical methods.
The function is:
function [x,yf,yg]=graphing(f,g,interval)
x = linspace(interval(1),interval(end),80);
yf = f(x);
yg = g(x);
plot(x,yf,'g',x,yg,'b');
tf = 'f(x)=';
tg = 'g(x)=';
legend(tf,tg);
end
And the command could be:
interval = [0 4];
f = @(x) exp(-0.5*x);
g =@(x) sin(2*x);
[x,yf,yg] = graphing(f,g,interval)
There is a way to get the string form of the functional notation for the anonymous function, so that it is possible having in legend the exact function form? It could help me for orientation, even if it is not necessary. Just as a matter of interest.
Thank you in advance.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Feb 2021
Edited: Fangjun Jiang on 16 Feb 2021
f = @(x) exp(-0.5*x);
>> func2str(f)
ans =
'@(x)exp(-0.5*x)'
Or, provide your string, use str2func()

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!