Effacer les filtres
Effacer les filtres

Why exist gives an answer even when the variable does not exist?

2 vues (au cours des 30 derniers jours)
Wister Huang
Wister Huang le 31 Août 2015
Commenté : Wister Huang le 18 Mai 2016
Why does the exist function sometime gives an answer even the variable does not exist, for example, on my computer
clear all;exist('a')
gives 7
clear all;exist('fil')
Also gives 7, why is that?

Réponse acceptée

Stephen23
Stephen23 le 31 Août 2015
Modifié(e) : Stephen23 le 31 Août 2015
When you actually read the documentation for exist, you will find that an output of seven means that exist has located a folder with that names, not a variable as you think it has. So somewhere on your path there are folders with those names. And running clear makes no difference to folders.
If you want to only search for variables, and not all of the other useful things that exist can locate (variables, mex files, simulink models, folders, functions, etc), then you should specify the optional argument like this and specify the output value:
X = 1==exist(name,'var');
really you should do this every time you use exist.
And of course the documentation has lots of other useful information on how to use MATLAB functions properly, so perhaps you should take a look!

Plus de réponses (0)

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by