2

For example, let's say I have a file named my_file.m, I'd like to do:

file_fullpath = ['path_to_file', filesep, 'my_file.m'];
is_function(file_fullpath)

I'd like a command (which is not is_function) that would determine if file_fullpath is a function or just a script and throw an error if the file does not exist or has some syntax problem that makes it undecidable. I'm guessing there should exist some built-in function to do it since Matlab correctly assigns different icons for functions and scripts in the navigator.

I could write a function to parse the file, looking for the appropriate keywords, but it would probably neither be simple, fast nor necessary.

3
  • 1
    You could add the folder with the file to the path and then try to invoke the function my_file(...). If it's a function, it should run. If not, it should throw an error. Commented Sep 3, 2020 at 20:46
  • 3
    A function M-file’s first non-comment line starts with function. This is all you need to look for. Discard lines that start with %, and expect function. Commented Sep 4, 2020 at 1:56
  • This other question asks the same thing, but for the currently running code. All answers there start by finding the file name of the currently running code, so they all can easily be used to answer this question too. Commented Jun 7, 2023 at 15:39

2 Answers 2

1

From this FileExchange submission: isfunction(), you can determine whether it is a function using a combination of:

% nargin gives the number of arguments which a function accepts, errors if not a func
nargin( ___ ); 
% If nargin didn't error, it could be a function file or function handle, so check
isa( ___, 'function_handle' ); 

More broadly, Jos has added multiple outputs to isfunction:

function ID = local_isfunction(FUNNAME)
try    
    nargin(FUNNAME) ; % nargin errors when FUNNAME is not a function
    ID = 1  + isa(FUNNAME, 'function_handle') ; % 1 for m-file, 2 for handle
catch ME
    % catch the error of nargin
    switch (ME.identifier)        
        case 'MATLAB:nargin:isScript'
            ID = -1 ; % script
        case 'MATLAB:narginout:notValidMfile'
            ID = -2 ; % probably another type of file, or it does not exist
        case 'MATLAB:narginout:functionDoesnotExist'
            ID = -3 ; % probably a handle, but not to a function
        case 'MATLAB:narginout:BadInput'
            ID = -4 ; % probably a variable or an array
        otherwise
            ID = 0 ; % unknown cause for error
    end
end
Sign up to request clarification or add additional context in comments.

Comments

0

You may use the following code to check if a file is a function or a script.

file_fullpath = ['path_to_file', filesep, 'my_file.m'];

t = mtree(file_fullpath ,'-file');
x = t.FileType

if(x.isequal("FunctionFile"))
    disp("It is a function!");
end
if(x.isequal("ScriptFile"))
    disp("It is a script!");
end

1 Comment

Please don't post the same answer in two questions. If you see two questions that ask the same thing, flag the one as duplicate of the other. (For future reference: the duplicate answer is this one.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.