matlabcell-array

How to append an empty array to a cell array


How do you append an empty array to a (non-empty) cell array?

For example, starting with

c={[1],[2]}

desire

c={[1],[2],[]}

Concatenation would remove the empty array whether it is double, char or cell.


Solution

  • You can just append it by using end+1:

    c={[1],[2]}
    c = 
        [1]    [2]
    c{end+1} = []  % end+1 "appends"
    c = 
        [1]    [2]    []
    

    MATLAB note: appending is usually used as a way to grow an array in size within a loop, which is not recommended in MATLAB. Instead, use pre-allocation to initially apply the final size whenever possible.