首页 > 解决方案 > MATLAB:替换缺失的单元格条目而不替换空格

问题描述

我正在使用 readcell 读取一个 excel 文件,并且所有空单元格都被导入为“缺失”。想补缺的,发现了以下建议(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'}

在此示例中,我希望保留“带空格的文本”,仅替换实际的“缺失”单元格。我如何实现这一目标?使用 2019b。

谢谢!

标签: matlabcell

解决方案


问题与any周围的呼叫有关ismissing

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

>> 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'}

推荐阅读