MATLAB - End Statement
MATLAB - Code Blocks and the end Keyword
Section titled “MATLAB - Code Blocks and the end Keyword”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.
1. Terminating Code Blocks
Section titled “1. Terminating Code Blocks”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.
Example: Nested Structures
Section titled “Example: Nested Structures”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'Best Practice: Auto-Indent for Clarity
Section titled “Best Practice: Auto-Indent for Clarity”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.
2. The end Keyword for Array Indexing
Section titled “2. The end Keyword for Array Indexing”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.
Syntax
Section titled “Syntax”myVector(end) % Access the last element of a vectormyMatrix(end, 1) % Access the element in the last row, first columnmyMatrix(1, end) % Access the element in the first row, last columnmyVector(2:end) % Access all elements from the second to the lastmyVector(end-2:end) % Access the last three elementsExample: Data Manipulation with end
Section titled “Example: Data Manipulation with end”Let’s see how end simplifies common data manipulation tasks.
% Create a sample matrixA = [ 1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12];
% Get the entire last columnlastCol = A(:, end);% Result: [4; 8; 12]
% Get the entire last rowlastRow = A(end, :);% Result: [9, 10, 11, 12]
% Get a sub-matrix without the first row and last columnsubMatrix = A(2:end, 1:end-1);% Result: [5, 6, 7; 9, 10, 11]
% Add a new row to the end of the matrixA(end+1, :) = [13, 14, 15, 16];% A is now a 4x4 matrixCommon Errors
Section titled “Common Errors”- Parse error at ‘end’: This usually means you have an extra
endstatement that doesn’t correspond to any openingfunction,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
endis expected. - Using
endoutside an indexing context: Typingendon its own in the command window will result in an error, as it only has meaning for terminating blocks or for indexing.