Why, oh why does isempty return 0? Questions related to dataset access and return from strfind.

5 ビュー (過去 30 日間)
Hello all,
I have a dataset array for example called measurement which contains a subfield called tempSequence containing single digits stored as strings (eg. '1', '2', etc.) I want to perform a check for the contents of this field and am using a combination of isempty and strfind and can't seem to get a TRUE return.
Specifically, I want to check that the field contains both a '1' and a '5' like so:
if( ~isempty(strfind(measurement.TempSequence, '1')) && isempty(strfind(messung.TempSequence, '5')) )
% Both 1 and 5 are present, perform operation on different subfield.
end
If tempSequence is as follows:
K>> measurement.TempSequence
ans =
'3'
'4'
'5'
'2'
And strfind returns:
K>> strfind(measurement.TempSequence, '1')
ans =
[]
[]
[]
[]
Why why why:
K>> isempty(strfind(measurement.TempSequence, '1'))
ans =
0
Is it something to do with it being a dataset and therefore has attached metadata? Am I using strfind in a dumb way? What am I missing?
Why:
K>> isempty(strfind(measurement.TempSequence, 'foobar'))
ans =
0
Thank you for reading! Marshall

採用された回答

Matt J
Matt J 2014 年 9 月 29 日
編集済み: Matt J 2014 年 9 月 29 日
The output of strfind is a cell array. To be considered empty, a cell array must have no cells whatsoever,
>> isempty({})
ans =
1
This is different from a cell array containing cells with empty matrices
>> c={[]};
>> isempty(c)
ans =
0
>> isempty(c{1})
ans =
1
  2 件のコメント
G A
G A 2014 年 9 月 29 日
>> c={[]}
>> isempty(cell2mat(c))
ans =
1
Marshall
Marshall 2014 年 9 月 30 日
Ah, that makes sense. Strfind makes much more sense now (and I even got my code working, so that's a plus).
Thanks so much for the answers!

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

その他の回答 (2 件)

Stephen23
Stephen23 2014 年 9 月 29 日
編集済み: Stephen23 2014 年 9 月 29 日
Expanding on Matt J's answer, if you wish to check if every cell of a cell array contains an empty array, then you can use cellfun :
cellfun('isempty',cell_array)
  2 件のコメント
Marshall
Marshall 2014 年 9 月 30 日
Ah, thanks! I'm coming from embedded C so vector operations and so on are all very new territory for me.
Stephen23
Stephen23 2014 年 9 月 30 日
It is a good thing that you keep this in mind. Basically in MATLAB think of the arrays/matrices more as the mathematical concept, and not as a programming abstraction of some lower-level data (like in C).

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


David Young
David Young 2014 年 9 月 29 日
The two earlier answers are both correct. Another suggestion which might help is to use ismember() instead of ~isempty(strfind()), with the opposite argument ordering:
if ismember('1', measurement.TempSequence) && ismember('5', measurement.TempSequence)
...
  2 件のコメント
Marshall
Marshall 2014 年 9 月 30 日
Great suggestion. I think it's cleaner and reads better too. I'm coming from embedded C to OO MatLab so it's a steep learning curve!
Thanks for the help.
Marshall
Marshall 2014 年 9 月 30 日
編集済み: Marshall 2014 年 9 月 30 日
ismember is the sort of function I was looking for in the first place, but there's such a huge number of functions to learn (but so it is with all languages). Your suggestion worked perfectly. So glad for this rad community. M_

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by