MATLAB: Replacing values in a cell which is part of matrix

cells and replacing values in matrix

I have a cell column, which is a third column of a matrix named 'a'. The cell column contains values A,B,C,D and E. I just want to change all E's to D. Unfortunately this does not work with cells: a(a=='E')='D'; (I am aware that if I cell2mat the column the above will work, but I have to keep it in a cell format.) I suppose it can be done somehow with cellfun …but how??? Regards

Best Answer

I have to keep it in a cell format
Why? If it's a cell array of scalar, a matrix is a lot more efficient.
There are several ways to do this.
1) convert to matrix, replace and convert back to cell array:
tmp = cell2mat(a);
tmp(tmp == E) == D;
a = num2cell(tmp)
2) use cellfun:
a(cellfun(@(elem) elem == E, a)) = {D}