MATLAB:替换缺失的单元格条目而不替换空格

MATLAB: Replace missing cell entries without replacing whitespaces

我正在使用 readcell 读取 excel 文件,所有空单元格都导入为 'missing'。 想补缺的,发现如下建议(cellfun+匿名函数)

https://www.mathworks.com/matlabcentral/answers/473295-how-to-replace-missing-values-in-a-cell-array https://www.mathworks.com/matlabcentral/answers/464998-readcell-is-filling-cell-locations-with-1x1-missing

但是,此解决方案还将空格标记为缺失并替换它们

A = {1, 'test123', 2, 1, 'texthere';2, 'test456',  3 ,4, missing;...
    3, 'test789', missing, 1, 'text with spaces'}

A(cellfun(@(x) any(ismissing(x)), A)) = {'REPLACED'}

在这个例子中,我希望 'text with spaces' 保持不变,只替换实际的 'missing' 单元格。我如何实现这一目标?使用 2019b.

谢谢!

问题与 any 调用有关 ismissing

作为 ismissing 的替代方法,您可以使用 isa 明确测试缺失数据 class:

>> A = {1, 'test123', 2, 1, 'texthere';2, 'test456',  3 ,4, missing;...
    3, 'test789', missing, 1, 'text with spaces'}
>> A(cellfun(@(x) isa(x,'missing'), A)) = {'REPLACED'}
A =
  3×5 cell array
    {[1]}    {'test123'}    {[       2]}    {[1]}    {'texthere'        }
    {[2]}    {'test456'}    {[       3]}    {[4]}    {'REPLACED'        }
    {[3]}    {'test789'}    {'REPLACED'}    {[1]}    {'text with spaces'}