Skip to content

MATLAB - End Statement

In MATLAB, the end keyword is fundamental to the language’s syntax. Unlike languages that use curly braces {} or indentation to define code blocks, MATLAB uses end to explicitly terminate structures like functions, loops, and conditional statements. This keyword has a second, powerful meaning when used for array indexing.

Every control flow statement or program unit that encapsulates a block of code must be paired with an end. This creates a clear, unambiguous scope for the code within the block.

Consider a function that processes a matrix. The end keywords clearly delineate the scope of the function, the for loop, and the if statement.

function positiveCount = countPositiveElements(matrix)
% This function counts the number of positive elements in each column.
[~, numCols] = size(matrix);
positiveCount = zeros(1, numCols); % Pre-allocate result array
for c = 1:numCols % Loop through each column
col = matrix(:, c);
for r = 1:length(col) % Loop through each element in the column
if col(r) > 0 % Check if the element is positive
positiveCount(c) = positiveCount(c) + 1;
end % This 'end' terminates the 'if' statement
end % This 'end' terminates the inner 'for' loop
end % This 'end' terminates the outer 'for' loop
end % This 'end' terminates the 'function'

The MATLAB Editor provides a crucial feature for managing end statements. Pressing Ctrl+I (or Cmd+I on macOS) will automatically indent your code. If your indentation looks wrong, it’s a strong visual cue that you have a mismatched or missing end statement. This is the single most effective way to debug scope issues.

This is a powerful and idiomatic feature of MATLAB. When used inside parentheses () or curly braces {} for indexing, end automatically resolves to the last index of that dimension.

myVector(end) % Access the last element of a vector
myMatrix(end, 1) % Access the element in the last row, first column
myMatrix(1, end) % Access the element in the first row, last column
myVector(2:end) % Access all elements from the second to the last
myVector(end-2:end) % Access the last three elements

Let’s see how end simplifies common data manipulation tasks.

% Create a sample matrix
A = [ 1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12];
% Get the entire last column
lastCol = A(:, end);
% Result: [4; 8; 12]
% Get the entire last row
lastRow = A(end, :);
% Result: [9, 10, 11, 12]
% Get a sub-matrix without the first row and last column
subMatrix = A(2:end, 1:end-1);
% Result: [5, 6, 7; 9, 10, 11]
% Add a new row to the end of the matrix
A(end+1, :) = [13, 14, 15, 16];
% A is now a 4x4 matrix
  • Parse error at ‘end’: This usually means you have an extra end statement that doesn’t correspond to any opening function, for, if, etc. Use the auto-indenter to find it.
  • An ‘end’ might be missing: This error occurs when a code block is left open. The MATLAB editor will often show a red underline near where the end is expected.
  • Using end outside an indexing context: Typing end on its own in the command window will result in an error, as it only has meaning for terminating blocks or for indexing.