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

조회 수: 1 (최근 30일)
Greg
Greg 2011년 1월 24일
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
Greg
Greg 2011년 1월 24일
Awesome. Thanks again for the continued help.
Paulo Silva
Paulo Silva 2011년 1월 24일
Kenneth I forgot to test for that condition :(

댓글을 달려면 로그인하십시오.


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