what are the differences between [] and '' in a cell array, how to identify and differentiate them?

c1={'a',[],'b'}-->
isempty(c1(2))=0; Not empty
size(c(2))=1 1;
strcmp(c(2),[])=0; Not a string
c2={'a','','b'}-->
isempty(c2(2))=0; Not empty
size(c(2))=1 1;
strcmp(c(2),'')=1; Is a string
So I can use strcmp(c,'')==1 to find ''. But how about []?
Thanks...

추가 답변 (1개)

In isempty(c1(2)), the expression c1(2) is equivalent to: {[]}, which is a 1x1 cell. This cell contains 1 element and is not empty in consequence. But you want to test the element of the cell. As Walter has pointed out this requires the curly braces: c1{2} is the 2nd element of the cell array, which is the empty matrix.
This might be helpful:
c = {'1', 2, '', []}
emptyC = cellfun('isempty', c)
charC = cellfun('isclass', c, 'char')
emptyC & charC
emptyC & ~charC
Alternatively: cellfun(@isempty, c) and cellfun(@ischar, c), but this is a little bit slower.

댓글 수: 3

By the way, what is the difference between , @isempty and 'isempty', in the cellfun? Thanks...
@J.Hu: When the function to be applied is defined by a string, the job is performed directly in the MEX file. When a function handle is provided, cellfun calls this command by mexCallMATLAB for each element, which causes a noticeable overhead. This is even worse for anonymous functions, e.g. : cellfun(@(x) isempty(x), c).
Older versions of Matlab contains the source code cellfun.c of this command, such that this behavior could be understood easily. Unfortunately in modern Matlab versions, you can not read the source code and the very efficient string commands appear under the term "Backward compatibility" in the documentation only.

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2013년 10월 1일

댓글:

Jan
2013년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by