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

조회 수: 6 (최근 30일)
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
Walter Roberson
Walter Roberson 2016년 11월 21일
Can .p files be classdef files? Hmmm, I guess they could be, but it does sound odd.
per isakson
per isakson 2016년 11월 21일
편집: per isakson 2016년 11월 21일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 20일
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개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by