-1

What does @(x) myfun(x, F_index) means in MATLAB? What does it call and return?

For example in this application:

fmincon(@(x) myfun(x, F_index), ...)

Please provide more examples and explain them if possible.

Yellows
  • 693
  • 4
  • 14
Masan
  • 347
  • 1
  • 4
  • 17
  • 1
    This is an [anonymous function handle](http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html). – Yellows Feb 11 '15 at 17:45
  • That construct (`@(x) ...`) is called an [anonymous function](http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html). It creates a variable that is essentially a function. The usage here indicates that the anonymous function is being used to [parameterize](http://www.mathworks.com/help/matlab/math/parameterizing-functions.html) `myfun`. Please see the documentation for more details. – TroyHaskin Feb 11 '15 at 17:46
  • 1
    Why don't you comment [on the answer you were given](http://stackoverflow.com/questions/28367531/error-using-fmincon-matlab) for clarification? – knedlsepp Feb 11 '15 at 17:49
  • possible duplicate of [@(t) mean in Matlab?](http://stackoverflow.com/questions/23726320/t-mean-in-matlab) – also [what is @ operator in Matlab](http://stackoverflow.com/questions/2100595/what-is-operator-in-matlab) and [Call functions with @() in MATLAB](http://stackoverflow.com/questions/24966517/call-functions-with-in-matlab/24973974#24973974) – horchler Feb 11 '15 at 18:28
  • This is another question @knedlsepp – Masan Feb 11 '15 at 19:31
  • @TroyHaskin Please see the answer bellow and answer his comment – Masan Feb 11 '15 at 19:32
  • @Jamaisavenir: Well, on itself this question is a duplicate of any 'anonymous function' question here on SO. So instead of asking this, you could either read one of those for clarification, or comment on the answer you were given for clarification... – knedlsepp Feb 11 '15 at 19:43

1 Answers1

1

It is an anonymous function, which is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle. Anonymous functions can accept inputs and return outputs, just as standard functions do. However, they can contain only a single executable statement. For example, create a handle to an anonymous function that finds the square of a number:

    function out=powerplus1(x,dat)
    out=x^2+dat;
     end

In another file you write

dat=1;
sqr = @(x) powerplus1(x,dat);
a = sqrplusone(5)

Then a will be 26.

Variable sqrplusone is a function handle. The @ operator creates the handle, and the parentheses () immediately after the @ operator include the function input arguments. This anonymous function accepts a single input x, and implicitly returns a single output, an array the same size as x that contains the squared plus one values. Find the square plus one of a particular value (5) by passing the value to the function handle, just as you would pass an input argument to a standard function.

a = sqrplusone(5)
a =
   26
SAH
  • 190
  • 2
  • 14
  • 1
    I have tried to give you an example with 2 argument, but I got some errors. If anyone else can , help him. I think he want something like this : `function out=mypower2(x,dat)` `out=x^2+dat;` `end` `sqr = @(x) mypower2(x,dat)` sqr(5)=? – SAH Feb 11 '15 at 19:27
  • Yes, I exactly need to know that @Electricman – Masan Feb 11 '15 at 19:30
  • @Electricman When you create an anonymous function with variables not in the `@(...)` list (like `dat` in this case), they need to be defined already because the anonymous function will store their values for later execution. See [Parameterizing Using Anonymous Functions](http://www.mathworks.com/help/matlab/math/parameterizing-functions.html#bsgprpq-8) for an example. – TroyHaskin Feb 11 '15 at 19:39
  • I have edited the my answer. @Jamaisavenir – SAH Feb 11 '15 at 19:51
  • Thanks @TroyHaskin The answer is edited, Hope its correct. – SAH Feb 11 '15 at 19:52