string data type is not a class?

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 Comment

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.

Sign in to comment.

 Accepted Answer

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 Comments

Yes, it makes sense now that I think about it.
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
This change made it to the release notes a while ago, by the way.
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.
It makes sense to solve the ambiguity this way, but I still think it would merit a remark in the doc.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Asked:

on 14 Jul 2021

Commented:

Rik
on 15 Jul 2021

Community Treasure Hunt

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

Start Hunting!