Why does exist('string') return 2?

22 visualizaciones (últimos 30 días)
Captain Karnage
Captain Karnage el 11 de Dic. de 2022
Comentada: Captain Karnage el 12 de Dic. de 2022
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 comentarios
Stephen23
Stephen23 el 11 de Dic. de 2022
Editada: Stephen23 el 11 de Dic. de 2022
"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.
Captain Karnage
Captain Karnage el 12 de Dic. de 2022
I'm using isa to validate the variable itself, but in the case it doesn't validate, I want to check that the string being passed describes a valid class for verbose logging & troubleshooting. The biggest hurdle there is when it's a custom class that's part of a project - hence why I'm trying to split up the "easy" to check strings that describe built in functions vs. the custom classes. From it's description, exist should work for that purpose, and for the most part it does except for the specific case of 'string'.
Also, the explanation that "it cheks for files before classes" doesn't make sense. If that was the case, then nearly all the built-in classes would return 2.
For example:
exist('int8') returns 5 but which('int8') returns built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datatypes\int8) which also points to an m-file.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de Dic. de 2022
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')
ans = 0
exist('string', 'class')
ans = 8
exist('string', 'file')
ans = 2
  1 comentario
Captain Karnage
Captain Karnage el 12 de Dic. de 2022
Thank you for mentioning the 2 argument version of exist.
It's still puzzling me, though... why doesn't string match builtin when int8 and int16 and other classes like that do? And why do they match file with 7 and not 2 when string matches with 2? It doesn't make sense to me.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by