IsField method not returning correct value

The isfield() method is not currently working for me. Check out this output from the console:
K>> TFORM
TFORM =
projective2d with properties:
T: [3x3 double]
Dimensionality: 2
K>> TFORM.T
ans =
0.9889 -0.0068 -0.0000
0.0003 0.9918 -0.0000
3.7452 5.5412 1.0000
K>> isfield(TFORM, 'T')
ans =
0
I can see that my object (TFORM) is there, and that it has a field named 'T'. However, when I run isfield(TFORM, 'T'), I get a 0 result. What gives?
*Edit*
I'm running R2015a (8.5.0.197613), on a 64-bit Mac.
AND
I was able to work around this by using:
any(strcmp(fieldnames(TFORM),'T'))
In place of:
isfield(TFORM, 'T')

 採用された回答

David Foti
David Foti 2016 年 4 月 8 日

3 投票

isfield finds fields in structs. The isprop function finds properties in objects.

8 件のコメント

Bob
Bob 2023 年 12 月 11 日
Then I would expect the isfield method to return an error instead of a wrong value.
Image Analyst
Image Analyst 2023 年 12 月 12 日
@Bob why is it a wrong value? Zero means false. And it is saying that T is not a field of TFORM, which it is not. So it is not wrong -- it's being accurate. T is a property, not a field. Again, there is no field called T so it should return false/0, which it does.
Paul
Paul 2023 年 12 月 12 日
IDK. The doc page isfield says that the first input should be a structure array.
So, on the one hand it would be reasonable to expect an error if that input is not a structure.
On the other hand, the doc page does say explicitly that: "If the input argument S is an object, then isfield always returns 0, even when field is the name of a property."
Fair enough. If the input is an object, than false should be returned as in the original Question.
BUT, does this result make sense?
x = 1;
class(x)
ans = 'double'
isfield(x,'a')
ans = logical
0
x is a numeric, which is not considered an object according to isobject
isobject(x)
ans = logical
0
Should isfield(x,'a') throw an error?
Steven Lord
Steven Lord 2023 年 12 月 12 日
No, isfield should not throw an error for a numeric first input. From the isfield documentation page, "If S is not a structure array, then isfield returns 0."
Paul
Paul 2023 年 12 月 12 日
Thanks for pointing that out.
Bob
Bob 2023 年 12 月 12 日
That isfield is consistent to its documentation is one thing, whether or not its makes sense is an other.
In my opinion this behaviour of isfield is the source of unexpected behaviour of code more often than not in case of handling of structs and classes. Simply because the user is not always aware if the object in question is a struct or a class and as well, such a strict distinction of structs and classes is somewhat unexpected as in other languages as C++ they are basically the same. And I would say as well that most of the users do not read the documentation before using a method - which is I suppose a nice feature of matlab that often times it is evident how methods are to be used.
Steven Lord
Steven Lord 2023 年 12 月 12 日
In my opinion this behaviour of isfield is the source of unexpected behaviour of code more often than not in case of handling of structs and classes.
If you feel strongly about this, you could send a bug report or enhancement request to Technical Support directly using this link. Be sure to explain your use case for why we should change this behavior that has been in the product literally for decades. [I just checked and in one of the older releases I can run on this machine, release R11 which was released in January 1999, isfield(1, 'field') returns 0.]
Paul
Paul 2023 年 12 月 12 日
編集済み: Paul 2023 年 12 月 13 日
My issue is that the Input Arguments section of the doc page strongly implies (at least to me) that the only valid input for the first argument is a structure array. Insofar that it seems the intent of the function is to accept any type of input, it seems to me that should be clearly indicated in the Input Arguments section that describes S.
I don't feel strongly enough about this to file a suggestion with the doc support folks.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2015 年 4 月 10 日

編集済み:

2023 年 12 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by