Using the C Math Library | ![]() ![]() |
Deleting Elements from a Cell Array
Cell arrays follow the same rules as numeric arrays (and structure arrays, as you'll see in the next section) for element deletion. You can delete a single element from a cell array, or an entire dimension element, for example, a row or column of a two-dimensional cell array or a row, column, or page of a three-dimensional cell array. In MATLAB, you delete elements by assigning []
to them. In the MATLAB C Math Library, you use mlfIndexDelete()
, which takes exactly the same type of arguments as mlfIndexRef()
.
Deleting a Single Element
In order to delete a single element from an array of any type, you must use one-dimensional indexing. Deleting a single element from a two-dimensional cell array collapses it into a vector cell array. For example, deleting the (2,1)
element of N
(the complex number 2-4i
), produces a three-element cell array. N(2)
refers to element (2,1)
of N
using one-dimensional indexing. Therefore, you'd write
N(2) = []
mlfIndexDelete(&N, "(?)", mlfScalar(2));
in C to remove element 2,1 from N
.
Deleting an Entire Dimension
You can delete an entire dimension by using vector subscripting to delete a row or column of cells. Use parentheses within the indexing string to indicate that you are deleting the cells themselves.
mlfIndexDelete(&N, "(?,?)", mlfScalar(2),mlfCreateColonIndex());
N(2,:) = []
performs the same operation in MATLAB. N{2, :} = []
is an error, because the number of items on the right- and left-hand side of the assignment operator is not the same. MATLAB does not do scalar expansion on cell arrays. In MATLAB if you want to set both cells in the second row of N
to []
, write N(2,:) = {[] []}
, thereby assigning a 1-by-2 cell array to another 1-by-2 cell array.
![]() | Assigning Values to a Cell Array | Structure Array Indexing | ![]() |