I would expect the following to pull up a list of methods for the string data type, but it does not:
methods("1")
No class 1.
Is there any rationale to this behavior?

댓글 수: 1

s={string([])};cellfun('isempty',s),cellfun(@isempty,s)
ans = logical
0
ans = logical
1
This implies the isempty function is overloaded for strings (which is what the cellfun doc warns about for the legacy syntax), but this fact is not reflected in the values methods returns.
Why all these implementation details are hidden is a mystery to me.

댓글을 달려면 로그인하십시오.

 채택된 답변

Rik
Rik 2021년 7월 14일

0 개 추천

help methods
METHODS Display class method names. METHODS CLASSNAME displays the names of the methods for the class with the name CLASSNAME. Use the functional form of METHODS, such as METHODS(S), when CLASSNAME is a string scalar. METHODS(OBJECT) displays the names of the methods for the class of OBJECT. M = METHODS(...) returns the method names in a cell array of character vectors. If CLASSNAME represents a MATLAB or Java class, then only public methods are returned, including those inherited from base classes. METHODS differs from WHAT in that the methods from all method directories are reported together, and METHODS removes all duplicate method names from the result list. METHODS CLASSNAME -full displays a full description of the methods in the class, including inheritance information and, for MATLAB and Java methods, method attributes and signatures. Duplicate method names with different signatures are not removed. M = METHODS( ..., '-full') returns the full method descriptions in a cell array of character vectors. The word METHODS is also used in a MATLAB class definition to denote the start of a methods definition block. Examples: %Example 1: %Retrieve the names of the public methods of class 'memmapfile' %and capture the result in a cell array of character vectors. methodnames = methods('memmapfile'); %Example 2: %Construct a java.lang.String instance and display the names of %the public methods of that instance. s = java.lang.String; methods(s); See also METHODSVIEW, PROPERTIES, EVENTS, CLASSDEF, WHAT, WHICH. Documentation for methods doc methods Other functions named methods icdevice/methods imaqdevice/methods icgroup/methods iviconfigurationstore/methods icinterface/methods serial/methods imaqchild/methods
This suggests that you can't use the methods(object) syntax for char and string inputs.
methods('1')
No class 1.
methods(class("1"))
Methods for class string: append contains eq extractAfter gt issorted lt pad reverse startsWith cellstr count erase extractBefore insertAfter join matches plus sort strip char double eraseBetween extractBetween insertBefore le ne replace split strlength compose endsWith extract ge ismissing lower or replaceBetween splitlines upper

댓글 수: 7

Matt J
Matt J 2021년 7월 14일
Yes, it makes sense now that I think about it.
Rik
Rik 2021년 7월 14일
The documentation doesn't explicitly mention this restriction. Have you filled in the feedback form yet? Otherwise I'll do that. I think this restriction should be mentioned somewhere.
Well, it seems self-evident in hindsight. If the input is a char or string, naturally methods() has to assume that the string contains the name of a class. How could it be expected to differentiate between the usage
methods('double')
and
methods('1')
Yes. We break the potential ambiguity between "is this a char vector or string scalar containing the name of a class" and "is this a char vector or string scalar that tells MATLAB it should return the methods of char or string respectively" by choosing the former. If you want the methods of string:
methods('string')
Methods for class string: append contains eq extractAfter gt issorted lt pad reverse startsWith cellstr count erase extractBefore insertAfter join matches plus sort strip char double eraseBetween extractBetween insertBefore le ne replace split strlength compose endsWith extract ge ismissing lower or replaceBetween splitlines upper
methods("string")
Methods for class string: append contains eq extractAfter gt issorted lt pad reverse startsWith cellstr count erase extractBefore insertAfter join matches plus sort strip char double eraseBetween extractBetween insertBefore le ne replace split strlength compose endsWith extract ge ismissing lower or replaceBetween splitlines upper
Walter Roberson
Walter Roberson 2021년 7월 15일
This change made it to the release notes a while ago, by the way.
Steven Lord
Steven Lord 2021년 7월 15일
There are a couple entries in the Release Notes related to the interaction between string arrays and the ismethod function, but I don't see any about string arrays and the methods function. I could have overlooked one, though, since searching the Release Notes for the word "method" or "methods" finds a lot of hits.
Rik
Rik 2021년 7월 15일
It makes sense to solve the ambiguity this way, but I still think it would merit a remark in the doc.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 7월 14일

댓글:

Rik
2021년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by