MATLAB - Anonymous Functions



A MATLAB function is a separate, self-contained block of code that performs a specific task and can accept input arguments and return output values. Functions help in organizing your code, making it more readable, and promoting code reuse.

Creating a Simple MATLAB Function

Suppose we want to create a function that calculates the square of a given number. Here's how you can do it −

Step 1 − Create a New MATLAB Script

Open MATLAB and create a new script file by clicking on the "New Script" button or using the edit command. Save it with a .m file extension (e.g., square.m).

Step 2 − Define the Function

Inside the square.m file, define the function as follows −

function result = square(x)
   % This function calculates the square of a number.
   result = x^2;
end

In matlab it will be as follows −

square m file

In this code −

  • function result = square(x) declares the function named square that takes one input argument x and returns a result.
  • result = x^2; calculates the square of the input x and assigns it to the variable result.

Step 3 − Use the Function

Now that you've created the function, you can use it in your MATLAB workspace or other scripts. Here's how you can use it −

>> num = 5;
result = square(num)

result =

   25

>> 

Now that we know how to create normal functions in Matlab, let us get a more detailed understanding of Anonymous functions in matlab.

What are Anonymous Functions?

Anonymous functions are small, unnamed functions in MATLAB that are defined using the @(inputs) expression syntax. They are also known as lambda functions because they allow you to create functions without explicitly naming them. Anonymous functions can have multiple inputs and can return a single output.

Creating an Anonymous Function

To create an anonymous function in MATLAB, you use the @(inputs) expression syntax. Here's a simple example −

square = @(x) x^2;

In this example, we define an anonymous function called square that takes one input x and returns its square.

Using Anonymous Functions

Anonymous functions can be used just like regular functions. You can call them with arguments, assign them to variables, and pass them as arguments to other functions. Here are some examples −

result = square(5); % Calls the anonymous function with x=5, result is 25.

Matlab Anonymous Functions Usage

Anonymous functions with no variables, also known as constant anonymous functions, are used especially in situations where you need to pass a function to another function but don't require any input arguments.

You can create a constant anonymous function with no input arguments by using the @() expression syntax. Here's an example

constant_func = @() 42;

In this example, we define an anonymous function called constant_func that takes no input arguments and always returns the constant value 42.

Anonymous functions of no variable also called constant anonymous function

Constant anonymous functions can be useful as function handles to other functions that expect a function argument. Here's an example −

function applyFunctionToArray(arr, func)
   result = func();  % Call the function with no arguments
   disp(result);
end

% Use the constant anonymous function as an argument
constant_func = @() 42;
applyFunctionToArray([1, 2, 3], constant_func);

In this example −

  • We define a custom function applyFunctionToArray that takes an array arr and a function handle func as arguments.
  • Inside applyFunctionToArray, we call the provided function func with no arguments, as it doesn't require any.
  • We use the anonymous function constant_func as an argument to applyFunctionToArray. It will always return 42.
  • When we call applyFunctionToArray with an array [1, 2, 3] and the constant anonymous function, it prints 42 to the console.

Anonymous Functions with one Variable

You can create an anonymous function with one variable in MATLAB using the @(variable) expression syntax. Here's an example −

square = @(x) x^2;

In this example, we define an anonymous function called square that takes one input variable x and returns the square of x.

You can call the function as follows −

result = square(5);% Calls the anonymous function with x=5, result is 25.

Anonymous Functions with Multiple Variables

Anonymous functions with multiple variables in MATLAB are used when you want to define a simple, unnamed function that takes more than one input argument.

To create an anonymous function with multiple variables, you can use the @(var1, var2, ...) expression syntax. Here's an example −

addition = @(x, y) x + y;

In this example, we define an anonymous function called addition that takes two input variables, x and y, and returns their sum.

You can call the anonymous function as shown below −

result1 = addition(4, 5);  % the result1 = 9

Anonymous Functions as Arguments to other Functions

In the example below I am going to make use of the arrayfun function, which applies a given function to each element of an array.

% Define an array of numbers
numbers = [1, 2, 3, 4, 5];

% Use arrayfun to double each element using an anonymous function
doubled_numbers = arrayfun(@(x) 2 * x, numbers)

In this example −

  • An array of numbers called numbers is defined with values [1,2,3,4,5].
  • We use the arrayfun function and provide an anonymous function @(x) 2 * x as the first argument. This anonymous function takes one input x and returns 2 * x, effectively doubling the value of x.
  • arrayfun applies this anonymous function to each element of the numbers array, resulting in a new array called doubled_numbers.
  • Finally, we display the doubled_numbers array, which contains the doubled values of the original array.

When you execute the same in matlab the output is −

>> 
numbers = [1, 2, 3, 4, 5];

doubled_numbers = arrayfun(@(x) 2 * x, numbers)

doubled_numbers =

   2     4     6     8    10
Advertisements