Clear Filters
Clear Filters

How to test for a 1 x 0 cell array?

12 views (last 30 days)
Gian Carpinelli
Gian Carpinelli on 24 Apr 2018
Edited: Stephen23 on 24 Apr 2018
Essentially what the question/ title is. Ive got a database and the user can add information into p where p is P = cell(1,1). The user can also delete information from this. at the moment ive got: if isempty(P{1,1})==1
fprintf('The library currently has no books. \n');
.... rest of code....
this only works if there are no books in the function originally, not for if the user has created books then deleted all, as this results with " a 1 x 0 empty cell array" ... how can I test for this?
thanks
  1 Comment
Stephen23
Stephen23 on 24 Apr 2018
Edited: Stephen23 on 24 Apr 2018

What is the purpose of the == here?:

isempty(...)==1

isempty returns a scalar logical, and that == operation returns an exactly identical scalar logical. What is the point of that? Which textbook is teaching so many beginners to use this pointless code?

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 24 Apr 2018
A 1x0 cell array is an empty array. isempty will return true for that, so your test is valid.
>> c = cell(1, 0);
>> isempty(c)
ans =
logical
1
If at the moment, you're not detecting 1x0 cell array as empty is because you're not testing what you say you're testing or your cell array is not as you say. Either way, show us the actual code you're using and the exact content of the cell array.
Note that:
if isempty(p)
and
if isempty(p{1})
test two completely different things. The first one checks if the cell array is empty. The second one assumes the cell array is not empty and checks if whatever is in the first cell of the array is empty.
Note2: the == 1 in your if expression is pointless. the output of isempty(x) == 1 is exactly the same as the output of isempty(x).

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!