How to implement isClassdef(filespec), isFunction(filespec) and isScript(filespec)?
2 views (last 30 days)
Show older comments
I'm trying to implement
isClassdef(filespec)
isFunction(filespec)
isScript(filespec)
which take full filespecs as input and return true/false. I think that the code exists in Matlab because it's used to sort the files in "Current Folder", but I failed to find it.
The function, exist fails me for classes, which are defined in @-folders. It returns 2, whereas I expected 8.
>> which tree
h:\m\FEX\InUse\TreeDataStructure\@tree\tree.m % tree constructor
>> exist( 'tree' )
ans =
2
>> meta.class.fromName('tree')
ans =
class with properties:
Name: 'tree'
...
I try to avoid to read the files and search for key-words.
What's the best way?
5 Comments
Walter Roberson
on 21 Nov 2016
Can .p files be classdef files? Hmmm, I guess they could be, but it does sound odd.
Accepted Answer
Walter Roberson
on 20 Nov 2016
You can specifically check for class
exist('tree', 'class')
The 2 that you are getting back reflects that fact that there is a tree.m on your MATLAB path. It is the constructor for the tree class, and would be invoked if you called tree on something that did not otherwise dispatch tree.
To tell the difference between functions and scripts, you could use something like
is_a_function = false;
try
nargin(TheCandidateName);
is_a_function = true;
end
nargin() fails for scripts
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!