MATLAB removes a column by assigning an empty array to that column—most commonly A(:, 3) = [], which deletes the third column of A and returns a matrix with one fewer column. This is not a function call; it is MATLAB's general deletion-by-assignment rule, and it works on matrices, tables, timetables, cell arrays, and string arrays with small but important differences in syntax. The rest of this article explains why that rule exists, how it behaves across MATLAB's main data containers, and which mistakes most often make column removal fail or produce a surprising result.

The Core Rule: Assigning [] Deletes

In MATLAB, the empty matrix [] is a real value with size 0-by-0. When you assign it to a slice of an existing variable on the left side of an assignment, MATLAB does not store an empty value in that slot. Instead, the assignment is interpreted as a deletion request: the named elements are removed, and the remaining elements shift to close the gap.

For a numeric matrix A, the general form is:

The colon selects every row, and k selects one or more columns. Because the column count shrinks, MATLAB also updates the size of A itself; no new variable is required unless you want to keep the original. For example:

After this, A is a 3-by-2 matrix containing columns 1 and 3 ([1 3; 4 6; 7 9]). The old second column is gone, and the former third column now occupies index 2. That reindexing is the single most important consequence of deletion: if you plan to delete several columns, do it in one combined statement or work from the highest index down.

Why the Rule Works This Way

The deletion behavior comes from MATLAB's array model. A variable is a handle to an array of a fixed size, and assignment to an indexed slice normally resizes or updates that array. Because [] has zero columns, assigning it to a full column produces a shape mismatch unless MATLAB treats the operation as removal. MATLAB resolves that by permitting [] as a special right-hand side for indexed assignment: the indexed elements are deleted, and the array is reshaped accordingly. The same mechanism explains why A(2, :) = [] removes a row and why A(2) = [] removes a single element from a vector, converting it from a row or column vector to a shorter one.

This also means you cannot use [] to "blank out" a column while preserving the matrix's dimensions. If you want a column of placeholders—zeros, NaN, empty strings, or missing values—assign those values explicitly. A(:, 2) = 0 fills the column with zeros; A(:, 2) = NaN fills it with not-a-number for floating-point arrays; A(:, 2) = missing works for certain table and string contexts. Deletion and replacement are different operations, and choosing the wrong one is a common source of downstream bugs.

Canonical Syntax

The essential forms for a 2-D array are:

A(:, k) = [] removes column k. A(:, [2 4]) = [] removes columns 2 and 4 in a single operation. A(:, 2:4) = [] removes a contiguous block of columns. A(:, logicalVector) = [] removes the columns where the logical vector is true.

The logical-index form deserves attention because it is the idiomatic way to remove columns based on a condition. Given a logical row vector keep with one entry per column, A(:, ~keep) = [] removes exactly the columns flagged for deletion. This is cleaner than building an explicit list of indices and less error-prone when the number of columns changes.

Removing Columns from a Matrix

For ordinary numeric, logical, or character matrices, the story is straightforward: A(:, k) = [] shrinks the second dimension. Two details matter in practice.

First, indices must be valid and within range. A(:, 5) = [] on a three-column matrix produces an index-out-of-bounds error rather than silently doing nothing. MATLAB requires the deleted indices to exist, which prevents accidental no-ops from typos.

Second, keep the colon on the row side. Writing A(k) = [] on a matrix removes the element at linear position k, not the column. Linear indexing counts down columns first, so A(2) = [] on a 3-by-3 matrix deletes the element in row 2, column 1—not column 2. For a vector, linear indexing and column selection coincide in a way that can mask the confusion, which is why the mistake often surfaces only when someone switches from vectors to matrices.

If you need to remove a column without modifying the original variable—useful when the original is an input argument or is reused later—copy first:

MATLAB uses copy-on-write semantics for arrays, so the copy is cheap until one of the variables is modified. Once B is changed, the two variables no longer share storage. There is no built-in "remove column and return new matrix" function analogous to some other languages' list operations; the assignment-to-empty idiom is the standard approach, and wrapping it in a small helper function is a reasonable stylistic choice for code that does this frequently.

Deleting Multiple Columns Safely

When several columns must go, do it in one statement:

The indices refer to the original matrix, so combining them avoids the shifting problem entirely. If you must delete iteratively, sort the indices in descending order and remove from the highest down:

Ascending order fails because deleting column 2 shifts the old column 5 to position 4; the loop's later index no longer points where you expect. This is a logic error, not a MATLAB quirk, but it is common enough to warrant the explicit warning.

Removing Columns from Tables and Timetables

Tables are where column removal becomes more than a slicing exercise, because tables carry variable names and types. MATLAB provides dedicated syntax for tables that is clearer and safer than positional indexing.

The modern approach is removevars:

removevars accepts variable names, a cell array of names, a string array, a vector of variable indices, or a logical vector, and it returns a table without those variables. It also accepts a function handle for pattern-based selection in recent versions, which is useful for wide tables with systematically named columns. Unlike positional deletion, name-based removal is robust to column reordering and to intermediate edits that change positions.

The bracket-assignment idiom also works on tables:

Both forms are valid, and both return a table with the named variables removed. The difference is mostly one of intent and readability: removevars signals "drop these variables" explicitly, while T(:, name) = [] reads as a slicing operation. Either is acceptable; consistency within a codebase matters more than the choice.

Timetables inherit the same behavior because they are tables with a time vector attached. Removing a data variable with removevars or bracket assignment leaves the time vector intact, which is usually what you want. Removing the time vector itself is a different operation and generally converts the timetable into a table; if that is the goal, use timetable2table rather than deleting the time variable, because the conversion handles metadata more predictably.

One subtlety: table variable names must be valid MATLAB identifiers in most contexts. If a variable was created with a name that is not a valid identifier—possible when reading certain files—you may need to reference it by position or use the VariableNames property rather than the dot or string form. When in doubt, T.Properties.VariableNames lists the exact names MATLAB is using internally.

Cell Arrays, String Arrays, and Struct Arrays

Cell arrays behave like matrices for column deletion: C(:, 2) = [] removes the second column of cells and shrinks the array. The same is true of string arrays created with string, and of categorical arrays. The idiom is consistent across these container types because they all support two-dimensional indexing.

Struct arrays are the exception worth flagging. A struct array's "columns" are elements along its dimensions, and removing an element changes the array's shape and the linear positions of subsequent elements. S(:, 2) = [] works on a 2-D struct array, but the operation is element removal, not field removal. Removing a field from every element is a different task: S = rmfield(S, 'fieldName'). Confusing fields with columns is a frequent error for newcomers, because dot notation (S.fieldName) looks like column access but operates on the field dimension of every element.

For a cell array of tables or a struct array holding matrices, decide at which level you are operating. The [] assignment always deletes along the indexed dimension of the container you name. It does not reach inside elements to modify their contents.

Sparse Matrices, GPU Arrays, and Other Special Cases

Sparse matrices support column deletion with the same syntax. S(:, k) = [] removes the column and its stored nonzeros, and the result remains sparse. This is efficient for removing a few columns but can be costly when repeated in a loop, because each deletion reallocates internal storage. For large-scale pruning, it is usually faster to build a keep-index and reassign in one pass: S = S(:, keep).

GPU arrays and distributed arrays follow the same rules as their in-memory counterparts, subject to the usual constraints of those environments. The syntax does not change; only performance characteristics and memory limits do. Tall arrays, which represent data too large for memory, support column removal by variable name rather than by arbitrary position in some contexts, so consult the documentation for the specific array type when working at that scale.

Common Errors and What They Mean

A few failure modes account for most questions about column removal in MATLAB.

"Index exceeds matrix dimensions." The requested column does not exist. Check the current size with size(A, 2) rather than assuming it. This error is protective: it prevents silent no-ops.

"A null assignment can have only one non-colon index." This occurs when the left side has more than one non-colon index, such as A(2, 3) = [] on a matrix, or when the deletion pattern is ambiguous. MATLAB permits null assignment only when the non-colon indices are single-valued and the deletion reduces the array along one dimension. If you want to delete a single element from a matrix, use linear indexing (A(5) = []), which converts the matrix to a row vector—usually not what you want on a 2-D array, so prefer explicit reshaping or logical masking.

"Undefined function or variable" after deletion. This is not a deletion error but a scoping one. If you delete columns inside a function and expect the caller's variable to change, remember that MATLAB passes arguments by value with copy-on-write semantics. The caller's array is unchanged unless you return the modified array and assign it.

Unexpected orientation change. Deleting from a row vector with v(2) = [] returns a row vector; deleting from a column vector returns a column vector. But deleting a single element from a matrix via linear indexing flattens it to a row vector. When shape matters, use two-subscript indexing and verify with size after the operation.

Deleting from a table by position after reordering. If code removes a table column by index, later code that assumes the original order breaks. Prefer name-based removal for tables unless positions are guaranteed stable.

Choosing Between Deletion and Masking

Deletion physically shrinks the array. Masking keeps the array's size and replaces values. The right choice depends on what happens next. If the removed column is genuinely irrelevant—an identifier that should not enter a computation, a redundant measurement, a column of constants—deletion simplifies downstream code and reduces memory. If the column's absence must be recorded for alignment with another dataset, a mask or a table with missing values preserves that information. Many data-cleaning bugs come from deleting columns that later needed to be matched by position against a partner dataset; keeping a record of what was removed, or using named variables instead of positions, prevents that class of error.

For tables specifically, removevars and its counterpart keepvars (available in recent releases) express intent directly and compose well with pattern-based selection. For matrices, the [] assignment remains the canonical tool, and a small helper such as dropCols = @(A, idx) A(:, setdiff(1:size(A,2), idx)) can make intent explicit when readability matters more than terseness.

A Complete, Practical Summary

To remove one column from a matrix: A(:, k) = []. To remove several: A(:, [k1 k2]) = [] or A(:, k1:k2) = []. To remove by condition: A(:, ~keep) = []. To remove from a table by name: T = removevars(T, 'name') or T(:, 'name') = []. To remove a field from a struct array: S = rmfield(S, 'field'). To keep the original intact, copy before deleting. To avoid shifting errors in loops, delete in descending index order or in a single statement.

The underlying principle is uniform: MATLAB deletes by assigning the empty array to an indexed slice, and the container's dimensions shrink accordingly. Once that rule is clear, column removal stops being a collection of memorized syntax and becomes one predictable operation applied across MATLAB's data types.