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

1 Ansicht (letzte 30 Tage)
Jan
Jan am 9 Feb. 2019
Kommentiert: Julian am 6 Mär. 2020
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')
?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Feb. 2019
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 Kommentare
Walter Roberson
Walter Roberson am 20 Feb. 2019
It is not documented that ismethod accepts the name of a class as the first argument. You should file a documentation complaint.
Jan
Jan am 21 Feb. 2019
Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 22 Mär. 2019
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 Kommentar
Julian
Julian am 6 Mär. 2020
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Class Introspection and Metadata finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by