Question

MyCell is a 5x10 cell of string cells (file names). I want to remove one element in each 5 cells based on string matching.

If I type:

setdiff(MyCell{1,1}, {'Dontwant.mat'})

it works, I get a 9-elements cell with the remaining elements.

Now I want to do this for each 5 elements, but if my script includes:

MyCell=cellfun(@(x) setdiff({x},{'Dontwant.mat'}), MyCell , 'uniformoutput', 0); 

I get the following error:

Error using cell/setdiff>cellsetdiffR2012a (line 292) Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.

Error in cell/setdiff (line 84) [varargout{1:nlhs}] = cellsetdiffR2012a(varargin{:});

Any help would be much appreciated.

Was it helpful?

Solution

The x that will be passed to your anonymous function is already a cell array, you don't need to wrap it in braces {x} so the correct version is:

MyCell=cellfun(@(x) setdiff(x,{'Dontwant.mat'}), MyCell , 'uniformoutput', 0); 

setdiff also works if one argument is string so you can simplify it by using

MyCell=cellfun(@(x) setdiff(x,'Dontwant.mat'), MyCell , 'uniformoutput', 0); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top