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)?

Answers


An anonymous function is what you're looking for:

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

ans =

     4

Need Your Help

Crash for inflating class

android android-layout maps runtime-error mapsforge

I am new in android, I want to implement action bar sliding, i read tutorials and write this. in program, i used a framelayout, the framelayout fill with mapsforge, but progrma in runtime has error...

Timezone for android RRule

android icalendar rrule

What timezone does android use to interpret event rrules like FREQ=WEEKLY;WKST=SU;BYDAY=WE? I know that if I will change default timezone in device's settings or in calendar's settings the event wi...