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

5 Ansichten (letzte 30 Tage)
Greg
Greg am 24 Jan. 2011
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.

Akzeptierte Antwort

Kenneth Eaton
Kenneth Eaton am 24 Jan. 2011
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 Kommentare
Paulo Silva
Paulo Silva am 24 Jan. 2011
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 am 24 Jan. 2011
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).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Paulo Silva
Paulo Silva am 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 Kommentare
Greg
Greg am 24 Jan. 2011
Awesome. Thanks again for the continued help.
Paulo Silva
Paulo Silva am 24 Jan. 2011
Kenneth I forgot to test for that condition :(

Melden Sie sich an, um zu kommentieren.


Paulo Silva
Paulo Silva am 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 Kommentar
Todd Flanagan
Todd Flanagan am 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.

Melden Sie sich an, um zu kommentieren.


Thiru jeyan
Thiru jeyan am 31 Jul. 2011
>> g = inline('sin(alpha*x)')
g =
Inline function:
g(alpha,x) = sin(alpha*x)
>> argnames(g)
ans =
'alpha'
'x'

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by