MATLAB - End Function



The 'end' keyword functions as a terminator for statements within MATLAB, such as 'for,' 'while,' 'switch,' 'try,' 'if,' and 'parfor' constructs. Without an 'end' statement, these constructs remain incomplete, awaiting further input. Each 'end' is paired with the nearest preceding unmatched 'for,' 'while,' 'switch,' 'try,' 'if,' or 'parfor' statement, ensuring proper code closure and structural integrity.

Moreover, the use of the 'end' keyword extends to functions in various capacities. It plays a pivotal role in enhancing code legibility and ensuring consistent coding practices. The necessity of 'end' arises in specific scenarios −

Function Files − When a MATLAB file contains multiple functions, if one of the functions is terminated with end, every function within that file must also be terminated with end. This ensures a clear and consistent structure within the file.

Nested Functions − If a file contains a function with one or more nested functions, each function in the file must be terminated with end. This practice maintains the hierarchy of functions and makes the code more organized.

Scripts with Local Functions − In the case of MATLAB scripts containing one or more local functions, it is important to terminate every function in the file with end. This practice ensures that the script's local functions are correctly encapsulated and structured for better code maintenance.

Here are examples and explanations for the different types of functions where end is commonly used −

End Keyword Inside Basic Matlab Function

In a basic MATLAB function, you use end to mark the end of the function block.

function result = myFunction(input)
   % Function code here
   result = input * 2;
end % End of the function

Here, end indicates the termination of the function block, and it's used to close the function definition.

End Keyword Inside Nested Functions in Matlab

You can define functions within other functions. In this case, you use end to denote the end of the inner function.

function result = outerFunction(input)
    % Function code here
    result = innerFunction(input);
    
    function resultInner = innerFunction(innerInput)
        % Inner function code here
        resultInner = innerInput + 1;
    end % End of the inner function
end % End of the outer function

In this example, end is used to indicate the end of both the inner and outer functions.

End Keyword Inside Local or Sub-functions in Matlab

Local or sub functions are particularly useful for breaking down complex code into smaller, more manageable components. Here's an example of a local function within a parent function −

function result = myParentFunction(input)
   % Main function code here
   intermediateResult = myLocalFunction(input);
   result = intermediateResult * 2;
   
   % Local function definition
   function localResult = myLocalFunction(localInput)
      % Local function code here
      localResult = localInput + 1;
   end % End of the local function
end % End of the parent function

In this example −

  • myParentFunction is the parent function.
  • myLocalFunction is the local function defined within the parent function.

The end keyword is used to indicate the end of both the parent and the local function.

Using End to Terminate if Statement and a for Loop

The end keyword can be used inside if statement and for-loop as shown below −

a = [0 0 4 4 0 0 0 4 0];
for k = 1:length(a)
   if a(k) == 0
      a(k) = 2;
   end
end

The "end" after a(k) = 2; denotes the conclusion of the block of code to execute when the condition a(k) == 0 is true. It signifies the end of the code segment that should be executed within the if statement.

The end keyword in-line with for-loop concludes the end of the code segment for the for-loop.

The output when checked with

Using End to Terminate Switch Block

Following examples shows how end keyword is used to terminate the switch block

age = 15;

switch choice
   case 1
      disp('Age: 15')
   case 20
      disp('Age: 20')
   otherwise
      disp('Age not defined')
end

Access Elements of Vector Using End Keyword

Let us first create a vector as shown below −

A = [1:20]

On execution the output is −

>> A = [1:20]

A =

 Columns 1 through 16:

    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16

 Columns 17 through 20:

   17   18   19   20

Now if you want to access the elements from the 10 to the last you can do so using end keyword as shown below −

B = A(10:end)

The output of above on execution in matlab command window is as follows −

>> B = A(10:end)

B =

   10    11    12    13    14    15    16    17    18    19    20

Access Last Row of Matrix Using End Keyword

Let us first create a matrix as shown below

A = magic(3)

When you execute in matlab command window the matrix is as follows −

>> A = magic(3)

A =	

     8     1     6
     3     5     7
     4     9     2

Now to get the last row we can make use of end keyword as shown below −

B = A(end,:)

The output of same B is as follows −

>> B = A(end,:)

B =

   4     9     2

Access Last Column of Matrix Using End Keyword

We will use the below matrix A

A =

     8     1     6
     3     5     7
     4     9     2

To get the last column we can make use of end keyword as shown below −

B = A(:,end)

When you execute the same in matlab command window the output is −

>> B = A(:, end)

B =

   6
   7
   2
Advertisements