MATLAB Answers

Jan
0

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

Asked by Jan
on 9 Feb 2019
Latest activity Answered by Jan
on 22 Mar 2019 at 14:04
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')
?

  0 Comments

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 9 Feb 2019
 Accepted Answer

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 Comments

Does this mean, that
ismethod(class(data), 'endsWith')
is the reliable way? Is it documented, that ismethod(class(1), 'xyz') really understands, that the argument is the name of the class, or does it treat 'double' as a char vector?
It is not documented that ismethod accepts the name of a class as the first argument. You should file a documentation complaint.

Sign in to comment.


Answer by Jan
on 22 Mar 2019 at 14:04

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.

  0 Comments

Sign in to comment.