0

I'm rather new to anonymous functions, and I couldn't find anything via a google search, so I'd thought I'd come here for help. For a symbolic function, I can use symvar to determine the function dependencies. Is there an equivalent for anonymous functions?

EXAMPLE: This function tells you how to turn water into gold

afun = @(temp,pressure,volume,time,mass) ... 

Aside from disp(afun), is there a way to capture the dependent variables with a function like symvar where the input variable listing can be stored in another variable? For example test = symvars(afun) where symvar is replace by something relating to anonymous functions. In other words, I'd like "test" to be an array equal to "temp,pressure,volume,time,mass"?

1
  • ...or like coeffnames(f) from cfit objects.... Commented Jan 31, 2020 at 22:34

1 Answer 1

3

The "anonymous" in anonymous function doesn't refer to the contents being unknown. It refers to the fact that this function does not have a static name like a function within an m-file.

For example, if you create the following function, it can only be used by it's name, myfunction.

function out = myfunction(a,b) 

With a function handle, however, it is assigned to a variable and that variable's name is used to call the function.

myfunc = @(a,b)disp('I am a walrus'); myfunc2 = myfunc; 

Now the same function can be called using either myfunc() or myfunc2().

Now that we've gotten the nomenclature out of the way, if you are given a function handle, you can actually just display the function handle (using disp) and see the function definition (complete with input arguments).

f = @(a,b)disp('I am a sneaky function'); disp(f) f = @(a,b)disp('I am a sneaky function'); 

From this it's easy to infer that f takes two input arguments (a and b).

As far as what these input arguments should be and what that function does with those arguments, you'd have to look at the function component of the function handle or ask whoever created it. If you can't tell, this doesn't make it "anonymous", it just makes it poorly documented.

Most function introspection functions that would work on a regular function will also work on an anonymous function if you want to get information about the input arguments programmatically.

%// Get the number of input arguments nargin(f) 

Update

If you used matlabFunction to generate an anonymous function from a symbolic expression, you can specify the input arguments in the call to matlabFunction using the Vars input.

As an example from the documentation.

syms x y z t r = (x + y/2 + z/3)*exp(-t); %// The first input argument will be t and the second will be an array of [x,y,z] matlabFunction(r, 'Vars', {t,[x y z]}); %// @(t,in2)exp(-t).*(in2(1)+in2(2).*(1.0./2.0)+in2(3).*(1.0./3.0)); 

Or as another example where I use an array rather than a cell array containing the values. They will then all appear as separate inputs to the anonymous function by name.

syms x y z r = x + y/2 + z/3; matlabFunction(r, 'Vars', [x,y,z]); %// @(x,y,z)x+y.*(1.0./2.0)+z.*(1.0./3.0); 

If you do not specify them, MATLAB automatically chooses the order as specified in the documentation:

By default, when you convert symbolic expressions, the order is alphabetical. When you convert symbolic functions, their input arguments appear in front of other variables, and all other variables are sorted alphabetically.

Update 2

If you really want some way to inspect the input variables of an anonymous function, here is a little anonymous function to do that for you.

anoninputs = @(f)strsplit(regexp(func2str(f), '(?<=^@\()[^\)]*', 'match', 'once'), ','); 

Now we can test it like:

f = @(a,b)disp('Just another function. Do not mind me'); anoninputs(f) 'a' 'b' 
Sign up to request clarification or add additional context in comments.

16 Comments

Yeah, that just gives in2 which is an array of arguments. Again, had I not known anything about the function, how would I know what those arguments are. I guess that's just the nature of "anonymous" functions in that they truly are anonymous. To work with them, you have to know something about the function or meaning of the inputs.
@ThatsRightJack "To work with them, you have to know something about the function or meaning of the inputs." That's true of any function.
That function is still anonymous in that it doesn't have a name. :)
@ThatsRightJack I just wrote you a function which will accomplish that for you. It's in the "Update 2" section
@ThatsRightJack Now understanding what you wanted, I'll just add that a simple mechanism doesn't exist for this because that's not really a robust or efficient way to typically program something. The argument order is best when it is fixed (or dynamically known) and symbol independent with the function itself adapting to vary arities. That's not a judgement; just an explanation.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.