arraysmatlabcell-array

Deleting consecutive and non-consecutive columns from a cell array in Matlab


I'm trying to delete multiple consecutive and non-consecutive columns from a 80-column, 1-row cell array mycells. My question is: what's the correct indexing of a vector of columns in Matlab?

What I tried to do is: mycells(1,[4:6,8,9]) = [] in an attempt to remove columns 4 to 6, column 8 and 9. But I get the error: A null assignment can have only one non-colon index.


Solution

  • Use a colon for the first index. That way only the 2nd index is "non-colon". E.g.,

    mycells(:,[4:6,8,9]) = []
    

    MATLAB could have been smart enough to recognize that when there is only one row the 1 and : amount to the same thing and you will still get a rectangular array result, but it isn't.