
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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