How to implement isClassdef(filespec), isFunction(filespec) and isScript(filespec)?

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

How could you distinguish functions from scripts without reading the files and checking, if the first keyword is not "function"?
How would you use such functions if they existed? Why do you need to differentiate between script, function, and class files?
@Jan, Checking the first keyword would work well for my own m-files. My first attempt to write a regexp(), which returned the first keyword failed. Then I came to think about p-files and decided reading files wasn't a good idea.
@Steven, I need support to make better programs. To that end, I try to use UML class diagrams to remind me of the "big picture". I envision diagrams in Matlab Web Browser on my second monitor. However, since Matlab is far from pure OOP, I need to include functions in the diagram. A method of an object may call a function, which in turn depends on another object. I need to distinguish between classes and functions in the diagrams.
Yesterday, I though, I try to make a class diagram of GUI Layout Toolbox by David Sampson. The first task was to generate lists of classes and functions, respectively. BTW: FEX knows the difference between, classes, functions and scripts.
Can .p files be classdef files? Hmmm, I guess they could be, but it does sound odd.
@Walter, Examples in the documentation says so. See "P-Coding Files That Belong to a Package and/or Class" in http://uk.mathworks.com/help/matlab/ref/pcode.html. And I did it successfully with a tiny class.

Sign in to comment.

 Accepted Answer

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

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!