We just had an interesting thread at MathWorks prompted by a customer request to programmatically distinguish between script and function MATLAB files. The File Browser already shows these separately. I will show some ideas we had and what we agreed was a good way to attack this.
The first idea was to create a function, perhaps called isfunction, and might be implemented by opening and reading the file, and looking for a function signature. Of course, you'd need to be sure that the function signature was not embedded in a comment. The tedium of doing this struck me and led me to ...
The next thing I thought of was to see what the function nargin did with various files on my path. I encourage you to read the documentation for this function. It's not very long.
Let's first see what happens for a "typical" function.
maxInputs = nargin('fftshift')
maxInputs = 2
We see that nargin finds that fftshift is called with up to 2 inputs.
Now let's try a function that can have any number of inputs.
maxInputs = nargin('ndgrid')
maxInputs = -1
Here, the negative value indicates that you can input a variable number of inputs, and the first formal input is varargin.
If I were to try a script, e.g., this one, here's what I would type
And here's the output from the command window.
Error using nargin whatIsThis is a script. Error in whatIsThis (line 31) nargin('whatIsThis')
We get an error message for this case. If we want to write a program to determine the file type, we can't have the program error out. So, in order to robustly check to see if the file is a function, we need to plan for scripts. For that, I recommend using the try/catch construct.
try maxInputs = nargin('whatIsThis') catch exception if strcmp(exception.identifier, 'MATLAB:nargin:isScript') disp('This file is a script.') else % We are only looking for scripts and functions so anything else % will be reported as an error. disp(exception.message) end end
This file is a script.
try maxInputs = nargin('blogTopics.doc') catch exception if strcmp(exception.identifier, 'MATLAB:nargin:isScript') disp('This file is a script.') else % We are only looking for scripts and functions so anything else % will be reported as an error. disp(exception.message) end end
Not a valid MATLAB file.
So now we can identify scripts without causing an error.
There's another kind of code file in MATLAB, pertaining to classes (the updated style, since R2008a, where classes are defined with (http://www.mathworks.com/help/matlab/ref/classdef.html classdef>), and we haven't done anything special to see if the file in question is for a class. I will demonstrate with the class dataset from Statistics Toolbox.
maxInputs = nargin('dataset')
maxInputs = -1
The fact that dataset can take a variable number of inputs doesn't tell us that dataset is a class file. To probe for that information, we can use the function exist. Looking at the help, we see that if the file in question is a class file, then exist returns the value 8. To ask specifically if it's a class, we use this code.
classy = exist('dataset','class')
classy = 8
To be sure the files we probed originally are not class files, we need to check them out as well.
classy = exist('fftshift','class')
classy = 0
classy = exist('ndgrid','class')
classy = 0
classy = exist('whatIsThis','class')
classy = 0
Do you have a similar need, where you need to characterize different file types, not based on their extension (e.g., .m)? I'd love to hear about cases where you need similar functionality. Let me know here.
Get the MATLAB code
Published with MATLAB® R2013a