How can I find what parameters are in an inline function.

1 次查看(过去 30 天)
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.

采纳的回答

Kenneth Eaton
Kenneth Eaton 2011-1-24
You can use the ARGNAMES function (which is a method for inline objects) to get the arguments of the inline function as a cell array of strings. Then you can see if one of them matches the string z using the function ISMEMBER:
F = inline('x+y-z');
isParameter = ismember('z',argnames(F));
Note that there is another function by the name of ARGNAMES in the Curve Fitting toolbox, which is the only one that seems to show up when searching the online documentation. The ARGNAMES method used for inline objects can be found in the folder \toolbox\matlab\funfun\@inline for MATLAB R2010b.
  3 个评论
Paulo Silva
Paulo Silva 2011-1-24
Hi Kenneth, I saw that argnames function in the inline documentation, I didn't did that way because it required a toolbox that the user might not have (don't know if it comes with all matlab versions).
Kenneth Eaton
Kenneth Eaton 2011-1-24
Paulo, actually I was mistaken. There is an overloaded ARGNAMES method for inline objects that gets called in the above code, not the one from the Curve Fitting toolbox (which is the only one that shows up when searching online).

请先登录,再进行评论。

更多回答(3 个)

Paulo Silva
Paulo Silva 2011-1-24
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 个评论

请先登录,再进行评论。


Paulo Silva
Paulo Silva 2011-1-24
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 个评论
Todd Flanagan
Todd Flanagan 2011-1-25
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
Thiru jeyan 2011-7-31
>> g = inline('sin(alpha*x)')
g =
Inline function:
g(alpha,x) = sin(alpha*x)
>> argnames(g)
ans =
'alpha'
'x'

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by