isempty function does not work for me

조회 수: 14 (최근 30일)
NeGM
NeGM 2021년 4월 9일
댓글: Stephen23 2021년 4월 9일
hi everyone,
I have the following code
L{1} = L1{1};
L{2} = L2{1};
[U, P, L1SBD, L2SBD] = abu_sbd(L);
L11 = L1SBD;
L22 = L2SBD;
bw1 = logical(L11); % Obtaing 1st L1 SBD
CC1 = bwconncomp(bw1, 4); %4 connectivity, No diagonals
S1 = regionprops(CC1,'SubarrayIdx');
Blocks_1 = arrayfun(@(s)L1SBD(s.SubarrayIdx{:}), S1, 'uniform', 0);
[s1,d1] = cellfun(@size,Blocks_1);
out1 = [s1,d1];
I am applying a simultanoues block diagonalization and need to determine whether my "out1" vector is empty or not. Sometimes I get an emtpy array and Sometimes I do not, However, when I use the "isempty" function it tells me that it's not empty even when it is.
Since "out1" is empty shouldnt the logical output be 1 and not 0? any help is greatly appreciated. Thank you
  댓글 수: 2
NeGM
NeGM 2021년 4월 9일
Thank you, you are right!
Stephen23
Stephen23 2021년 4월 9일
"However, when I use the "isempty" function it tells me that it's not empty even when it is."
No, you are confusing function syntax (which is what you should be using) with command syntax (which treats all trailing arguments as literal strings. Your code is exactly equivalent to:
isempty('out1')
where that 1x4 character vector is clearly not empty. The difference is well documented:

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

채택된 답변

Chendi Lin
Chendi Lin 2021년 4월 9일
Hi Nelson,
I believe the correct way to use "isempty" is to call "isempty(out1)". Here is the sample code:
>> a = zeros(0,2)
a =
0x2 empty double matrix
>> isempty(a)
ans =
logical
1
Best,
CD
  댓글 수: 1
Steven Lord
Steven Lord 2021년 4월 9일
That is correct.
isempty x
is equivalent to
isempty('x')
not
isempty(x)
The char vector 'x' is not empty. The variable x may or may not be empty.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by