如何检测单元格阵列中的空单元格? [英] How do I detect empty cells in a cell array?
问题描述
如何检测单元格阵列中的空单元格?我知道删除空单元格的命令是a(1) = []
,但是我似乎无法让MATLAB自动检测哪些单元格为空.
How do I detect empty cells in a cell array? I know the command to remove the empty cell is a(1) = []
, but I can't seem to get MATLAB to automatically detect which cells are empty.
背景:
我使用a=cell(1,53)
预分配了一个单元格数组.
然后,我使用if exist(filename(i))
和textscan检查文件并读入.结果,当filename(i)
不存在时,将产生一个空单元格,然后我们移至下一个文件.
Background:
I preallocated a cell array using a=cell(1,53)
.
Then I used if exist(filename(i))
and textscan to check for a file, and read it in. As a result, when the filename(i)
does not exist, an empty cell results and we move onto the next file.
读完所有文件后,我想删除a
的空白单元格.我尝试了if a(i)==[]
When I'm finished reading in all the files, I would like to delete the empty cells of a
. I tried if a(i)==[]
推荐答案
Note: a(i)==[]
won't work. If you want to know whether the the i-th cell is empty, you have to use curly brackets to access the content of the cell. Also, ==[]
evaluates to empty, instead of true
/false
, so you should use the command isempty
instead. In short: a(i)==[]
should be rewritten as isempty(a{i})
.
这篇关于如何检测单元格阵列中的空单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!