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
.
Delete sets of cells using standard array indexing with smooth parentheses, (). For example, remove the second row of C.
Delete the contents of a particular cell by assigning an empty array to the cell, using curly braces for content indexing, {}. C= 3×3 cell array { [1]} { [ 2]} { [3]} { [4]} {0x0 double} { [6]} { [7]} { [ 8]} { [9]} Delete sets of cells using standard array indexing with smooth parentheses, (). For example, remove the second row of C.
In a matrix, to remove the columns in which the element of the first line is 0, we can use: ind2remove = (A(1,:) == 0); A(:,ind2remove) = [];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With