6

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.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 1
    It's worth clarifying for future readers that this is a misinterpretation of the docs and, contrary to what you've stated, **the documented method does work**. You have simply implemented the incorrect method, from the two examples on the page you linked. Curly braces vs rounded brackets can be an annoying concept to grasp in Matlab, in this case you wanted rounded brackets as shown in [Tommaso's answer](https://stackoverflow.com/a/48437850/3978545). – Wolfie Jan 25 '18 at 12:44

1 Answers1

7

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

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98