Search code examples
matlabcell

search through cell elements that contain a string, then delete it


Hi I want to search a cell array for any elements containing the letter 'x'. I can delete a cell element by doing the following:

mycell(3) = []

but trying to search through the elements id the difficult part. I am using:

offending_cell  = strcmp('x', mycell);

however this is just picking out all elements regardless of x appearing in them. Anyone have any suggestions?


Solution

  • There you go:

    ind = (~cellfun('isempty',(regexp(mycell,'x'))));
    

    This gives logical indices for cells that contain 'x'. If you want to delete those cells:

    mycell(ind) = [];
    

    The problem with you apporach was that strcmp looks for exact matching, not if the string contains a given character.