user1089977
user1089977

Reputation: 21

Creating an anonymous function and calling it passing arguments in one line in MATLAB

You can do in matlab something like this:

>> fh = @(x) x^2
fh = 
   @(x)x^2

and then

>> fh(3)
ans =
    9

Now I look for a way to create the anonymous function and call it in one line, like this (it does not work):

@(x) x^2 (3) <-- This code does not work!

Is there a way to do it?

Upvotes: 2

Views: 103

Answers (2)

Sam Roberts
Sam Roberts

Reputation: 24127

feval( @(x) x^2, 3) is what you need.

Upvotes: 7

Oli
Oli

Reputation: 16035

This would work (it works also with matrixes):

arrayfun(@(x) x^2,3)

Upvotes: 6

Related Questions