Videos
How can I use an empty matrix in a logical if statement?
How to check if an array is empty without using any built in functions
Why does isempty return false?
isempty function does not work for me
I am fairly new to MatLab and am using it to inspect an array of strings to determine whether each element contains a string (specifically, the letter 'a'). When prompting isempty(strfind(TEST(1), 'a')), where TEST(1) does not contain an 'a', it returns FALSE (i.e. 0).
strfind(TEST(1), 'a') returns {[]}
Any help resolving this issue and possibly helping me understand why the result is not TRUE (i.e. 1)?
Edit: Wow thank-you for all of the thorough responses. I'll get back to working on my project tomorrow at work. This looks to be a very supportive community - although the submitted posts seem to get downvoted a lot.
For checking if an array is empty (that is, it doesn't contain any elements), you can use A.size == 0:
import numpy as np
In [2]: A = np.array([[1, 2], [3, 4]])
In [3]: A.size
Out[3]: 4
In [4]: B = np.array([[], []])
In [5]: B.size
Out[5]: 0
To check whether it only contains 0's you can check for np.count_nonzero(A):
In [13]: Y = np.array([[0, 0], [0, 0]])
In [14]: np.count_nonzero(Y)
Out[14]: 0
you can compare your array x, with 0 and see if all values are False
np.all(x==0)
Use CELLFUN
%# find empty cells
emptyCells = cellfun(@isempty,a);
%# remove empty cells
a(emptyCells) = [];
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}).
All above mentioned answers are incorrect, because in my case when i used them, they removed empty cells and then all elements of my cell array situated in a row manner instead of preserving their actual shape. In fact after using this kind of approach your cell array elements tend to be a row cell vector.
I have found this approach which works correctly in my case.
source : https://groups.google.com/forum/#!topic/comp.softsys.matlab/p3NX0fI6u90
approach:
myCellARRAY(all(cellfun(@isempty,myCellARRAY),2), : ) = [];