If AOI is a cell array of cell arrays of character vectors, like this:
AOI = {{'AOI 1';'AOI 2';'NO AOI';'AOI 3'}; {'NO AOI';'NO AOI'}; {'AOI 1';'NO AOI';'NO AOI';'AOI 2'}};
AOI{:}
ans =
{'AOI 1' }
{'AOI 2' }
{'NO AOI'}
{'AOI 3' }
ans =
{'NO AOI'}
{'NO AOI'}
ans =
{'AOI 1' }
{'NO AOI'}
{'NO AOI'}
{'AOI 2' }
Then the code works:
idx = strcmp(nested_cell, 'NO AOI');
AOI{:}
ans =
{'AOI 1'}
{'AOI 2'}
{'AOI 3'}
ans =
0×1 empty
cell array
ans =
{'AOI 1'}
{'AOI 2'}
new_AOI = AOI(~cellfun('isempty', AOI));
new_AOI{:}
ans =
{'AOI 1'}
{'AOI 2'}
{'AOI 3'}
ans =
{'AOI 1'}
{'AOI 2'}
So I'm guessing you actually have a cell array of cell arrays of strings, like this:
AOI = {{"AOI 1";"AOI 2";"NO AOI";"AOI 3"}; {"NO AOI";"NO AOI"}; {"AOI 1";"NO AOI";"NO AOI";"AOI 2"}};
AOI{:}
ans =
{["AOI 1" ]}
{["AOI 2" ]}
{["NO AOI"]}
{["AOI 3" ]}
ans =
{["NO AOI"]}
{["NO AOI"]}
ans =
{["AOI 1" ]}
{["NO AOI"]}
{["NO AOI"]}
{["AOI 2" ]}
If that's the case, you can make an adjustment as follows. [nested_cell{:}] concatenates the contents of nested_cell, in this case producing a string array from the cell array of strings nested_cell, which strcmp works properly on.
idx = strcmp([nested_cell{:}], 'NO AOI');
AOI{:}
ans =
{["AOI 1"]}
{["AOI 2"]}
{["AOI 3"]}
ans =
0×1 empty
cell array
ans =
{["AOI 1"]}
{["AOI 2"]}
new_AOI = AOI(~cellfun('isempty', AOI));
new_AOI{:}
ans =
{["AOI 1"]}
{["AOI 2"]}
{["AOI 3"]}
ans =
{["AOI 1"]}
{["AOI 2"]}
An alternative would be to use cellstr to convert nested_cell to a cell array of character vectors regardless of whether it already was one or whether it was a cell array of strings. This might be useful in case AOI contains mixed types. Something like this:
AOI = {{'AOI 1';"AOI 2";"NO AOI";'AOI 3'}; {"NO AOI";"NO AOI"}; {'AOI 1';'NO AOI';'NO AOI';'AOI 2'}};
AOI{:}
ans =
{'AOI 1' }
{["AOI 2" ]}
{["NO AOI"]}
{'AOI 3' }
ans =
{["NO AOI"]}
{["NO AOI"]}
ans =
{'AOI 1' }
{'NO AOI'}
{'NO AOI'}
{'AOI 2' }
nested_cell = cellstr(AOI{i});
idx = strcmp(nested_cell, 'NO AOI');
AOI{:}
ans =
{'AOI 1' }
{["AOI 2"]}
{'AOI 3' }
ans =
0×1 empty
cell array
ans =
{'AOI 1'}
{'AOI 2'}
new_AOI = AOI(~cellfun('isempty', AOI));
new_AOI{:}
ans =
{'AOI 1' }
{["AOI 2"]}
{'AOI 3' }
ans =
{'AOI 1'}
{'AOI 2'}
Of course, new_AOI still contains the original contents (some strings, some character vectors) of those elements that were not 'NO AOI' or "NO AOI". If you want to end up with a cell array of cell arrays of only character vectors instead, you can convert AOI first. Something like this:
AOI = {{'AOI 1';"AOI 2";"NO AOI";'AOI 3'}; {"NO AOI";"NO AOI"}; {'AOI 1';'NO AOI';'NO AOI';'AOI 2'}};
AOI{:}
ans =
{'AOI 1' }
{["AOI 2" ]}
{["NO AOI"]}
{'AOI 3' }
ans =
{["NO AOI"]}
{["NO AOI"]}
ans =
{'AOI 1' }
{'NO AOI'}
{'NO AOI'}
{'AOI 2' }
AOI = cellfun(@cellstr,AOI,'UniformOutput',false);
AOI{:}
ans =
{'AOI 1' }
{'AOI 2' }
{'NO AOI'}
{'AOI 3' }
ans =
{'NO AOI'}
{'NO AOI'}
ans =
{'AOI 1' }
{'NO AOI'}
{'NO AOI'}
{'AOI 2' }
Then use your code as you already have it, since it has been shown to work on a cell array of cell arrays of character vectors at the beginning of this answer. Or rewrite the loop without the temporary variables nested_cell and idx:
AOI{i}(strcmp(AOI{i}, 'NO AOI'), :) = [];
AOI{:}
ans =
{'AOI 1'}
{'AOI 2'}
{'AOI 3'}
ans =
0×1 empty
cell array
ans =
{'AOI 1'}
{'AOI 2'}
Also, note that you can avoid creating a separate variable new_AOI, and call the result AOI (which has already been modified in the loop - so preserving it is obviously not a concern):
AOI = AOI(~cellfun(@isempty, AOI));
Or you can just operate on AOI directly:
AOI(cellfun(@isempty, AOI)) = [];