Search code examples
matlabfindcell-array

How do I find if a cell in a cell array contains a certain value in MATLAB?


I have an array of cells B. I want to find if one of the cell contains a certain value, in this case [1 1 1440 1920], and if so remove it.

I tried using:

ismember(mat2cell([1 1 1440 1920],1),B)

I got an error saying "Input A of class cell and input B of class cell must be cell arrays of character vectors".

I thought that mat2cell() would give me a cell array. What am I doing wrong?

Is there an easier way to find this component if exist and remove it?


Solution

  • Here's how you can do it using cellfun:

    B(cellfun(@(c) isequal(c, [1 1 1440 1920]), B)) = [];
    

    The anonymous function is applied to each cell of B, returning a logical index that is true anywhere the contents of a cell is equal to [1 1 1440 1920]. This index is then used to remove those cells.