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

5 ビュー (過去 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 Center および File ExchangeFunction Creation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by