In MATLAB, you delete columns by assigning empty square brackets to the columns you want gone: A(:, cols) = []. The colon keeps every row, and cols names the columns to remove. This works for numeric arrays, cell arrays, string arrays, tables, and timetables with small syntax differences. The one requirement is that the target must be a variable you can reassign; you can't shrink a function input or a read-only property in place. The rest is picking the right column reference, keeping row and column dimensions straight, and avoiding the classic mistake of dropping data you still needed.

The core mechanic: why [] removes a dimension

An empty assignment tells MATLAB to delete the indexed positions along that dimension. With A(:, 3) = [], MATLAB removes column 3, shifts later columns left, and returns an array with one fewer column. You must assign the result back to a variable: A = A(:, [1 2 4:end]) and A(:, 3) = [] produce the same array. What you can't do is write A(:, 3) = [] where A is a function parameter inside the function's own signature—inputs are local copies but the deletion still works normally on that local variable; what genuinely fails is attempting to modify an object whose property is read-only, or indexing into a function call result and assigning empty, like size(A)(2) = []. Delete through a named variable, then use it.

The indexing expression itself has a few valid forms. A single number (A(:, 3) = []) removes one column. A vector (A(:, [2 5]) = []) removes several. A logical mask (A(:, mask) = []) removes whichever columns the mask flags true. The colon on the row position is what makes it "columns only"; A(3, :) = [] with the colon on columns deletes a row instead. Logical and linear indexing are the two places people get tripped up, so they get their own sections below.

If you're deleting many columns at once, do it in one assignment rather than in a loop. A(:, [3 7 12]) = [] reassigns once; three successive A(:, k) = [] calls reassign three times and, worse, force you to recompute indices after each shift. MATLAB's column positions move left every time you delete, so a loop that deletes column 3, then column 7, then column 12 is only correct if those numbers refer to the original layout. They rarely do after the first deletion.

Deleting by name in tables and timetables

Tables are where column deletion gets genuinely useful and genuinely different. Instead of numeric positions, you remove variables by name:

removevars accepts a character vector, string, cell array of names, or a vartype specification, so you can drop every variable of a given class in one call. The equivalent empty-bracket form also works and is what many people reach for first: T.Height = [] deletes that variable, and T(:, {'Height','Weight'}) = [] deletes several at once. Both are fine; removevars reads more clearly when the goal is explicitly removal and composes well when the names come from a variable list.

Timetables follow the same rule but carry a time column that you can't casually delete. removevars works the same way, and deleting the time variable itself is prevented or strips the timetable-ness, so treat the time vector as off-limits. For both tables and timetables, the row count stays fixed while the variable count shrinks—this is the opposite of arrays where rows can also shrink, and it's why table code that mixes up "row" and "variable" produces confusing dimension errors.

One caution with tables: T(:, 3) = [] uses positional indexing and is legal, but it's fragile. If someone reorders variables upstream, position 3 is now a different column. Name-based deletion survives reordering; positional deletion doesn't.

Deleting by pattern, type, or position programmatically

Hard-coded indices break when data changes shape, so the robust approach computes the columns to delete. Common patterns:

The contains, startsWith, and endsWith functions return logical vectors the same length as the variable list, which is exactly what the empty-bracket deletion expects. For matrices without names, A(:, 5:10) = [] and A(:, [2 4 6]) = [] are the workhorses. A useful habit: print size(A, 2) before and after, or check width(T) for tables, to confirm the column count changed by exactly the number you intended.

If you're building the deletion list dynamically, make sure it's a row vector of valid indices. A(:, idx) = [] where idx is a column vector of indices generally still works for deletion, but keeping index vectors as rows avoids surprises when you later use them for selection. And if the computed list could be empty, A(:, []) = [] is a no-op, which is usually what you want—but verify you didn't mean "delete everything" by accident.

Logical indexing versus numeric indexing

This is the single biggest source of silent bugs. Numeric indexing counts positions: A(:, [1 0 1 0]) is an error because zero isn't a valid position, while A(:, [1 3]) keeps columns 1 and 3. Logical indexing tests conditions: A(:, [true false true false]) keeps columns 1 and 3. The mask must match the number of columns exactly, or MATLAB throws "index exceeds array bounds" or a dimension mismatch.

Deleting with a logical mask inverts intuition: the true positions are the ones removed. A(:, [true false true false]) = [] deletes columns 1 and 3 and keeps 2 and 4. Many people expect the opposite. The mnemonic that holds: A(:, mask) selects the trues, and A(:, mask) = [] deletes the trues. Selection and deletion are complements. If you want to keep columns matching a condition, select with the mask; if you want to drop them, delete with the same mask.

For matrices with repeated values, don't confuse value-based and position-based deletion. A(:, A(1,:) == 0) = [] deletes every column whose first-row entry is zero—useful when cleaning sensor data with sentinel values. That's logical indexing doing real work, and it's worth knowing because "delete the columns where the header row says..." is a common task that has no clean numeric-index equivalent.

Cell arrays, string arrays, and char arrays

The same (:, idx) = [] syntax applies to cell arrays and string arrays. C(:, 3) = [] removes the third column of a cell array; S(:, {'b','d'}) = [] removes named columns of a string array if you're working with a table-like structure—though string arrays don't carry variable names, so use positions or logical masks there. Char arrays are different: a 2-D char array's "columns" are characters, so ch(:, 3) = [] removes the third character from every row, which can leave ragged-looking results if rows had different logical meanings. For text data organized by column meaning, you almost always want a cell array, string array, or table, not a char matrix.

Structured arrays (structs) don't support column deletion in the array sense; you remove fields with rmfield, not columns. If you're thinking "delete a column" but your variable is a struct array, you probably want to delete the corresponding field: S = rmfield(S, 'Height'). This trips people up when data arrives as a struct array and they try matrix syntax on it.

Common errors and what they mean

Index exceeds matrix dimensions on a delete usually means the column number is greater than size(A, 2), often because a prior deletion shrank the array and your index was computed before that. Recompute indices after every structural change, or delete everything in one call. Matrix index is out of range for deletion in newer MATLAB versions says the same thing more explicitly.

Assignment has more non-singleton rhs dimensions than non-singleton subscripts means the left side names positions and the right side is [], but something about the shape doesn't line up—double-check that the colon and index are in the right slots for the dimension you intended to remove. If you wrote A(3, 1:2) = [] you're trying to delete part of a row across two columns, which isn't a valid shape operation; deletions remove whole columns or whole rows, not rectangular patches.

For tables, Unrecognized table variable name means a name in your list doesn't exist—often a case mismatch, since table variable names are case-sensitive. The VariableNames property must contain one name for each variable in the table means you tried to set names to a shorter list to "delete" variables; that's rejected. Use removevars or empty assignment instead.

A subtler failure: deleting columns from a variable inside a loop that also uses that variable's size. If you resize while iterating for k = 1:size(A,2) and delete a column each pass, the loop bound is stale and you'll index past the end. Delete in a single vectorized call, or iterate downward (for k = size(A,2):-1:1) if you must mutate inside a loop.

Worked example: cleaning a matrix and a table

Suppose you load a numeric matrix raw with 12 columns and want to drop columns 2, 5, and 9 through 11. One assignment does it:

If raw started as 100×12, it's now 100×7, because you removed 6 columns. Check with size(raw); the second number should be 7. A common arithmetic check: removed count equals original width minus new width, and the indices you listed should all be within 1:original_width.

For a table T with variables including PatientID, Height_cm, Weight_kg, Temp_C, and Notes, drop the measurement columns by type while keeping identifiers and notes:

If PatientID is also numeric, that gets dropped too—so either convert it to a string first or list the measurement names explicitly: T = removevars(T, {'Height_cm','Weight_kg','Temp_C'}). The type-based call is fast but blunt; the name-based call is precise but needs the names to be right. Either way, width(T) tells you how many variables remain, and T.Properties.VariableNames lists them so you can eyeball the result.

When to delete in place versus copy

Deleting columns changes the array's shape and requires MATLAB to allocate a new array internally; the original variable gets the new one. This means any other variable that referenced the original data (through assignment, not through handles) keeps the old copy. That's usually desirable—it protects you from accidentally mutating shared data—but it also means deletion isn't free for large arrays. If you're processing a large matrix repeatedly, consider building a keep-list and selecting once: A = A(:, keep) copies the needed columns in a single pass, which is often as fast or faster than many successive deletions and easier to reason about.

For very large tables, removevars is implemented to do the removal efficiently, but the same copy semantics apply. If memory is tight, clear the original after reassignment only when you're certain nothing else needs it.

Verification habit

After any deletion, confirm three things: the column count changed by the expected amount, the row count is unchanged (for column deletion), and the surviving data is what you meant to keep. size(A), width(T), and head(T) or A(1:3,:) make these checks quick. When deleting by name, print the variable names before and after; when deleting by mask, verify the mask length equals the current column count. A 30-second check catches almost every deletion bug before it propagates downstream.

A final practical note on version differences: removevars and vartype are modern table functions and are the clearest choice if your codebase targets recent MATLAB releases; the empty-bracket T(:, {'a','b'}) = [] form has been stable for far longer and works essentially everywhere tables exist. If you're writing code for colleagues on unknown versions, the bracket form is the safer default, and removevars is the more readable one when version support is known.