Why does exist('string') return 2?
조회 수: 24 (최근 30일)
이전 댓글 표시
I'm using a function to validate the type of variable passed against a string describing the type of variable.
For the most part, I'm using the following facts from here: https://www.mathworks.com/help/matlab/ref/exist.html
- exist('name') returns an 8 if 'name' is the name of a (built-in*) class
*The documentation on mathworks just says a class, but if it's my custom class rather than built in, I get the value of 2 for the .m file I have describing the class...
- exist('name') returns a 5 if 'name' describes a built-in MATLAB function, not including classes.
- exist('name') returns a 2 if 'name' describes the name of a '.m' file.
(NOTE: I know these values are also returned for other cases, but these are the one I'm interested in).
I'm trying to use switch exist(name) (where name is a string variable in this case) to determine what actions I take, which works in most cases.
Per guidance in the documentation, I'm trying to avoid using eval as much as possible.
However, i have to make a special case when strcmp(name, 'string') is true - because the actions I need to take for 'string' are what I would normally need if I got a 5... but 'string' returns a 2! (If I use eval I wouldn't need special cases - but again, trying to avoid that) Why is that?
Further, if I do which('string') it returns "string is a built-in method % string constructor" - which tells me it should be returning a 5, not a 2...
BUT.... all other built-in functions (e.g. 'char' or 'int8') return "built-in (X:\Program Files\MATLAB\....)" wherever the file is in the MATLAB installation when I use which - which is different than what which("string") returns....
is this a bug?
Update:
I found out (I think, at least) I can avoid the whole thing by using the 2-argument version of exist . I can effectively ignore the "file" matches, and get everything that matches class with the if statement if ( exist( name, 'class' ) ) and not only will it match built-in classes, it will also match custom user classes and bypass the #2 file match, including when name == 'string'.
With some further experimentation I have determined that you are right, a match of file takes priority over a match of class. It appears the match numbers of 1-8 are priority, where if it matches more than one, it stops at the lower number and returns that.
What I still don't understand it why certain types match what they match and don't match what they don't. Any type or class that isn't a user-defined class appears to have a .m file in the "datatypes" subdirectory in MATLAB. And, of course, a user-defined class is found as an m-file in the active directory.
Here are my observations:
The base built-in "Types" such as double, char, and cell all have .m files in the "datatypes" directory but also have files in directories starting with "@" - so I understand why they return true when exist( 'char', 'builtin' ) is queried for example - and perhaps if such an "@" directory exists, it won't match #2... but exist( 'char', 'file' ) does match #7, which is folder. So they won't match #2 despite having a .m file (ALL things in MATLAB seem to have .m files, so since that's #2, there has to be some trigger to ignore that match).
However, I still don't understand why 'string' is not considered a builtin, or why other types that match builtin that aren't one of the base types types are not also considered file matches. For example, int8 and int16 which aren't base types, but still match builtin. In other words, I don't see anything apparent difference in how string is defined and how int8, int16 etc are defined that make them match differently.
댓글 수: 2
Stephen23
2022년 12월 11일
편집: Stephen23
2022년 12월 11일
"I'm using a function to validate the type of variable passed against a string describing the type of variable."
It sounds like you should be using ISA(), not EXIST().
"I'm trying to use switch exist(name)"
It looks like you should be using CLASS(), not EXIST().
"Why is that?"
Because EXIST() has to check things in some order, and apparently it checks for files before classes.
답변 (1개)
Walter Roberson
2022년 12월 12일
In the case where NAME is the name of a built-in class, and there is a method with that name inside a @ class definition, and the class with that method is loaded into memory, then exist() may report 2 because it might find the .m file.
It is not uncommon for this to happen: it is common for there to be .m files that exist to hold documentation .
In this particular case, toolbox/matlab/strfun/@string/string.m exists for the sake of documentation, and is the one found when you ask exist('string')
Remember that exist() allows optional arguments
exist('string', 'builtin')
exist('string', 'class')
exist('string', 'file')
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!