如何删除包含文本和数字的 excel sheet 的任何列中具有空单元格的整行

How to delete entire row which has empty cell in any column, of a excel sheet containing both texts and numbers

这是我在 excel 中的数据。如何删除 MATLAB 中任何列中具有任何空单元格的整行。 sheet 包含文本和数字,

(col1  col2 col3) 
OAS01   0   74
OAS02   0   55
OAS03   0.5 73
OAS04       24
OAS05       21
OAS06       20
OAS07   0   74
OAS08   0   52
OAS09   1   30
OAS01       81

我想通过删除具有任何空单元格的整个行和所有行来获得这样的输出

(col1 col2 col3) 
OAS01 0 74 
OAS02 0 55
OAS03 0.5 73 
OAS07 0 74 
OAS08 0 52 
OAS09 1 30 

我尝试过但效果不佳

[num, text, a] = xlsread('data.xlsx');

for i = 1:size(data,1)

if isnan(num(i,1))

    a(i,:) =[];

    num(i,:) =[];

else 
   a(i,:) =a(i,:);   
   num(i,:) =num(i,:);   
end

end

xlswrite('newfile.xlsx', a);

更优雅的方式:

T = {'a','b';'','c';'d','e'}

>> T = 

'a'    'b'
''     'c'
'd'    'e'

 T(any(cellfun(@isempty,T),2),:) = []

>> T =

'a'    'b'
'd'    'e'

------编辑-----

OP 说它不起作用,所以我检查了一下,这是因为 xlsread 函数将空单元格加载为 NaN,所以这一行应该修复它:

 [num, text, a] = xlsread('data.xlsx');
 a(any(cellfun(@(x) any(isnan(x)),a),2),:) = [];

其中 a 是 OP 加载的 3 x 3 单元格。

说明:cellfun 被大量使用并且有据可查,在这种特殊情况下,我们有兴趣将带有 NaN 的行设置为 [],因此我们使用 matlab 的 isnan 来检测包含 NaN 的单元格,我们然后将 any 函数包装在外面,其中 returns 如果有 NaN 则为布尔值 1,如果没有 NaN 则为 0。外部 any 生成布尔索引(0 是没有 NaN 的行,1 是有 NaN 的行)我们过滤数据。