Delete all empty cells in a cell array

Jac*_*ckA 2 arrays matlab cell-array

I have an array of cells in which I want to select 3 lines so I used this temp = testresults(13:15,1:end).

The array being bigger, I get a lot of empty cells

{'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}
Run Code Online (Sandbox Code Playgroud)

Hence, I'm trying to remove the empty cells using temp(~cellfun('isempty',temp)) but, if all the empty cells are gone, it puts all my data in one column:

{'Summary Test Results'          }
{'Pass/Fail'                     }
{'Failed cases'                  }
{'Overall'                       }
{'Passed'                        }
{'No failure'                    }
{'No Transition time change'     }
{'OVP Transition level pass/fail'}
{'Passed'                        }
{'No failure'                    }
Run Code Online (Sandbox Code Playgroud)

I've tried some variation of the function and I have also tried with cat(2, temp{:}) but I don't know how to keep the data in the right position.

How can I remove the empty cells without touching the position of the rest of the data?

Cri*_*ngo 5

给定一个像这样的单元格数组:

temp = {
'Summary Test Re…', 'Overall',    '', '',                 'OVP Transition …', '', '', '', ''
'Pass/Fail',        'Passed',     '', 'No Transition t…', 'Passed',           '', '', '', ''
'Failed cases',     'No failure', '', '',                 'No failure',       '', '', '', ''
'',                 '',           '', '',                 '',                 '', '', '', ''};
Run Code Online (Sandbox Code Playgroud)

我们可以使用找到空单元格(正如您已经发现的)

empty = cellfun('isempty',temp);
Run Code Online (Sandbox Code Playgroud)

接下来,我们可以删除所有单元格都为空的行

temp(all(empty,2),:) = [];
Run Code Online (Sandbox Code Playgroud)

和所有单元格都为空的列

temp(:,all(empty,1)) = [];
Run Code Online (Sandbox Code Playgroud)

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

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