如何检测单元格阵列中的空单元格? [英] How do I detect empty cells in a cell array?

查看:107
本文介绍了如何检测单元格阵列中的空单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测单元格阵列中的空单元格?我知道删除空单元格的命令是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)==[]

推荐答案

使用注意:a(i)==[]不起作用.如果要知道第i个单元格是否为空,则必须使用大括号来访问该单元格的内容.另外,==[]的计算结果为空,而不是true/false,因此您应该使用命令isempty.简而言之:a(i)==[]应该改写为isempty(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆