How can I find what parameters are in an inline function.
Show older comments
If I have an inline function like:
F = inline('x+y-z')
How can I check that z is a parameter of this inline function? More specifically, I want to be able to have a function in matlab be able to take a given inline function such as F above and be able to ask it "does F have a parameter in it called k?" to which it should be false, but to "does F have a parameter in it called z?" it should return true. Is there a way to do this? Thanks.
Accepted Answer
More Answers (3)
Paulo Silva
on 24 Jan 2011
F = inline('x+y-z');
~isempty(strfind(char(F),'y')) %put what you are looking for inside the ''
The codes result is 1 if it finds a match and 0 if it doesn't, you can even see if there's a specific symbol like the +, also does combinations like +y and won't detect something like yy when you search for y.
%Interactive example
F=input('Input your inline function: ','s')
F1 = inline(F);
G=input('What parameter do you to check: ','s')
if (~isempty(strfind(char(F1),G)))
fprintf(':) The parameter %s is present inside %s\n',G,char(F1))
else
fprintf(':( There is no parameter %s inside %s\n',G,char(F1))
end
3 Comments
Kenneth Eaton
on 24 Jan 2011
Nice idea, but this won't always work. For example, if one of the parameters in "F" is "by", the above will return true when you check if "y" is a parameter.
Greg
on 24 Jan 2011
Paulo Silva
on 24 Jan 2011
Kenneth I forgot to test for that condition :(
Paulo Silva
on 24 Jan 2011
Here's a new version that doesn't fail with the 'by' parameter and doesn't require the Curve Fitting toolbox or other besides Matlab, my poor skills couldn't find a simpler way to detect the parameters but an expert might :)
clear;clc
F=input('Input your inline function: ','s');
F1 = inline(F);
G=input('What parameter do you to check: ','s');
if (max(ismember(cell2mat(strfind(symvar(F),G)),1))==1)
fprintf(':) The parameter %s is present inside %s\n',G,char(F1))
else
fprintf(':( There is no parameter %s inside %s\n',G,char(F1))
end
1 Comment
Todd Flanagan
on 25 Jan 2011
Hi Paulo, I made a small edit so that your answer text is different from your code text. Hope you don't mind.
Thiru jeyan
on 31 Jul 2011
>> g = inline('sin(alpha*x)')
g =
Inline function:
g(alpha,x) = sin(alpha*x)
>> argnames(g)
ans =
'alpha'
'x'
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!