How to test for a 1 x 0 cell array?

조회 수: 10 (최근 30일)
Gian Carpinelli
Gian Carpinelli 2018년 4월 24일
편집: Stephen23 2018년 4월 24일
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
Stephen23
Stephen23 2018년 4월 24일
편집: Stephen23 2018년 4월 24일

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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2018년 4월 24일
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).

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by