Anomaly with the number 32??

I am using this line to determine if an element of a cell array is empty:
tf = isequal({' '}, Array(row,col));
When
Array(row,col) = 32
tf returns 1, which should actually be 0. Why is this happening?

 採用された回答

Oleg Komarov
Oleg Komarov 2011 年 8 月 11 日

0 投票

Be careful with cell arrays:
isequal({' '}, 32) % 0
Array = {32};
isequal({' '},Array) % true
It is not comparing what's inside but is asking the following question: "Is a cell equal to a cell?"

3 件のコメント

Nathan McCaughey
Nathan McCaughey 2011 年 8 月 11 日
Can you then explain this?
array = {33};
is equal({' '},array) % 0
Oleg Komarov
Oleg Komarov 2011 年 8 月 11 日
Ok, I guess I was wrong about the question the isequal is asking.
Then it does ask enquire the content (but I do remember there was a peculiar behaviour, can't find the post).
The point here is that double(' ') == 32, i.e. the empty space is converted to it's corresponding numeric value according to the ASCII table.
Nathan McCaughey
Nathan McCaughey 2011 年 8 月 11 日
I thought something curious was going on. Thanks.

サインインしてコメントする。

その他の回答 (1 件)

Arturo Moncada-Torres
Arturo Moncada-Torres 2011 年 8 月 11 日

0 投票

To know if a cell array is empty, I would recommend you to do the following:
myEmptyCells = cellfun(@isempty,cellArray);
As to this strange conduct, sometimes cell arrays can be tricky. I recommend you to read this great post by Loren regarding them.

3 件のコメント

Nathan McCaughey
Nathan McCaughey 2011 年 8 月 11 日
Thank you Arturo. I have read that post (many times actually).
I am importing an array from excel that contains all different data types. If the excel cell is empty, the cell in the raw matlab array is ' '. Suppose for example my array is:
A =
[1] 'hello' ' ' [45]
and I want to see if A(1,3) is empty (or equal to ' '). Can you adapt your previous sugesstion to accomodate?
Arturo Moncada-Torres
Arturo Moncada-Torres 2011 年 8 月 12 日
It would work anyway. If you want to use it with _isequal_ you can do something like this:
a{1} = 1;
a{2} = 'hello';
a{3} = '';
a{4} = 45;
b = cell(1,4);
myEmptyCells = cellfun(@isequal, a, b);
Jan
Jan 2011 年 8 月 13 日
BTW, cellfun('isempty') is much faster than cellfun(@isempty)!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by