Function Closure in MATLAB

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: MATLAB

In MATLAB, a closure is a function that can access and manipulate variables defined outside of its own scope. A closure is created by defining a function within another function (the enclosing function).

Here's an example that uses closures in MATLAB:

function main()
    x = 5;
    y = 10;
    f = createFunction();
    output = f(x);
    fprintf('The result is %f\n', output);
end

function output = createFunction()
y = 20;
f = @innerFunction;

function result = innerFunction(input)
result = x * y + input;
end

output = f;
end

In this example, the createFunction() function creates and returns the innerFunction() closure, which takes an input and returns a result based on the values of x and y.

The x and y variables are defined outside of the innerFunction() scope, but can be accessed because they are defined in the parent scope of createFunction().

When main() calls createFunction(), it returns the innerFunction() closure, which is assigned to f. The resulting function is then called with x as the input, which returns the result of x * y + input.

The output of running this code would be:

The result is 75.000000

This example demonstrates how closures can be useful in encapsulating and manipulating variables within a function.