ismethod(string('test'), 'endsWith') under R2016b and 2018b

1 回表示 (過去 30 日間)
Jan
Jan 2019 年 2 月 9 日
コメント済み: Julian 2020 年 3 月 6 日
R2016b:
ismethod(string('asd'), 'endsWith')
ans = logical(1)
R2018b:
ismethod(string('asd'), 'endsWith')
ans = logical(0)
Is this documented? I did not finf in in the release notes.
Actually ismethod uses the class of the first input. But for string there seems to be an exception. Is this the correct usage:
data = string('asd')
ismethod(class(data), 'endsWith')
?

採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 9 日
ismethod() calls upon methods(). methods() has two forms: it can be passed a class name, or an object. string('asd') is the same as "asd" and in all current versions, you can specify class names either as character vectors or as scalar string objects.
In R2016b when string objects had just been introduced, there were a lot of routines that had not been upgraded to accept string objects in place of character vectors or cell array of character vectors. Each release after that converted more and more routines. Most of them had been converted by R2018a, but R2018b release notes indicates changes continued even into R2018b, especially an new weird change to table(), timetable() and addVars() that is likely to lead to confusion.
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 2 月 20 日
It is not documented that ismethod accepts the name of a class as the first argument. You should file a documentation complaint.
Jan
Jan 2019 年 2 月 21 日
Thank you.

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

その他の回答 (1 件)

Jan
Jan 2019 年 3 月 22 日
The release notes of R2019a explain:
---------------
First argument to ismethod must be an object
Behavior change in future release
The ismethod function is documented to look for a method of the object that is specified as the first input. However, the ismethod function treats string and char inputs as a class name and looks for the specified method in that class. Therefore, you cannot use ismethod to find a method of an input object that is a string or char array. In future releases, ismethod will return true only if the second input is the name of a method of the first input object. ismethod will not treat the first input as a class name.
For code that uses ismethod with a class name specified as a string scalar or character vector, you can substitute this expression as an alternative that will work in current and future versions.
any(strcmp('methodName', methods('ClassName')))
----------------
Then my idea of a workaround:
ismethod(class(data), 'endsWith')
is not compatible with older and future Matlab releases, but this is needed:
any(strcmp('endsWith', methods(class(data)))
What a pity. If MathWorks has decided, that the first input of ismethod must be a string or char vector containing the class name, my idea would be a nice and clean workaround. Because this is a "change in a future release" we don't know, when the bahavior is modified and using ismethod remains fragile.
  1 件のコメント
Julian
Julian 2020 年 3 月 6 日
I just hit this problem - and derived the same workaround! I should have checked Answers first ... I searched only in bug reports for ismethod but there was nothing. The current flawed implementation is a bug because it contradicts the doc.
The future behaviour described in release notes of R2019a will correct the bug, although not yet there in R2019b. Avoid calling ismethod until it gets fixed!

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

カテゴリ

Help Center および File ExchangeClass Introspection and Metadata についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by