Search code examples
matlabcell-array

How to remove last element from cell array in Matlab?


How to remove last element from cell array in Matlab?

Documented method does not work:

>> A = {'a', 'b', 'c'}
A =
  1×3 cell array
    'a'    'b'    'c'
>> A{end}=[]
A =
  1×3 cell array
    'a'    'b'    []

I need array become 1x2.


Solution

  • You have to use round parentheses instead of curly braces (which act on the inner cell values and not on the cells themselves):

    A(end) = [];
    

    For more details, refer to the last section of the official documentation you linked:

    https://mathworks.com/help/matlab/matlab_prog/delete-data-from-a-cell-array.html