Billie
Billie

Reputation: 9146

How to store a function in a variable in MATLAB?

Let's say I want to store the function y(x) = x + 2 in a variable.

Is there any way to save y = x + 2, and access as y(x)? For example, y(2)?

Upvotes: 2

Views: 615

Answers (1)

gnovice
gnovice

Reputation: 125854

An anonymous function is what you're looking for:

>> y = @(x) x+2;
>> y(2)

ans =

     4

Upvotes: 4

Related Questions