删除元胞数组中的所有空单元格

Delete all empty cells in a cell array

我有一个单元格数组,我想在其中 select 3 行,所以我使用了这个 temp = testresults(13:15,1:end)

数组越大,我得到很多空单元格

{'Summary Test Re…'}    {'Overall'   }    {0×0 char          }    {'OVP Transition …'}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}
{'Pass/Fail'       }    {'Passed'    }    {'No Transition t…'}    {'Passed'          }    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}
{'Failed cases'    }    {'No failure'}    {0×0 char          }    {'No failure'      }    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}

因此,我尝试使用 temp(~cellfun('isempty',temp)) 删除空单元格,但是,如果所有空单元格都消失了,它会将我的所有数据放在一列中:

{'Summary Test Results'          }
{'Pass/Fail'                     }
{'Failed cases'                  }
{'Overall'                       }
{'Passed'                        }
{'No failure'                    }
{'No Transition time change'     }
{'OVP Transition level pass/fail'}
{'Passed'                        }
{'No failure'                    }

我尝试了函数的一些变体,我也尝试过 cat(2, temp{:}) 但我不知道如何将数据保持在正确的位置。

如何在不触及其余数据位置的情况下删除空单元格?

给定这样一个元胞数组:

temp = {
'Summary Test Re…', 'Overall',    '', '',                 'OVP Transition …', '', '', '', ''
'Pass/Fail',        'Passed',     '', 'No Transition t…', 'Passed',           '', '', '', ''
'Failed cases',     'No failure', '', '',                 'No failure',       '', '', '', ''
'',                 '',           '', '',                 '',                 '', '', '', ''};

我们可以使用

找到空单元格(正如您已经发现的那样)
empty = cellfun('isempty',temp);

接下来,我们可以使用

删除所有单元格为空的行
temp(all(empty,2),:) = [];

所有单元格都为空的列

temp(:,all(empty,1)) = [];

all(empty,1) returns 一个逻辑行向量,其中一个元素是 true 如果该列中的所有单元格都为空。我们使用这个逻辑向量来索引那些数组元素并将它们设置为空数组。分配一个空数组是 MATLAB 所说的删除数组元素。删除完整的行和列可以保留数组形状。

注意 temp(:,1)=[]temp{:,1}=[] 之间的区别。第一个删除一列数组元素,第二个为元胞数组的一列中的每个单元格分配一个空数组。