Loren on the Art of MATLAB

Turn ideas into MATLAB

What Kind of MATLAB File is This?

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.

Contents

Function Vs. Script: The First Idea

The first idea was to create a function, perhaps calledisfunction, 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 ...

Function Vs. Script: The Next Idea

接下来我想看看傅nctionnargindid 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 thatnarginfinds thatfftshiftis 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 isvarargin.

If I were to try a script, e.g., this one, here's what I would type

nargin('whatIsThis')

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 thetry/catchconstruct.

trymaxInputs = nargin('whatIsThis')catch异常ifstrcmp(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)endend
This file is a script.
trymaxInputs = nargin('blogTopics.doc')catch异常ifstrcmp(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)endend
Not a valid MATLAB file.

So now we can identify scripts without causing an error.

What about Classes?

There's another kind of code file in MATLAB, pertaining to classes (the updated style, since R2008a, where classes are defined with (//www.tatmou.com/help/matlab/ref/classdef.htmlclassdef>), and we haven't done anything special to see if the file in question is for a class. I will demonstrate with the classdatasetfrom Statistics Toolbox.

maxInputs = nargin('dataset')
maxInputs = -1

The fact thatdatasetcan take a variable number of inputs doesn't tell us thatdatasetis a class file. To probe for that information, we can use the functionexist. Looking at the help, we see that if the file in question is a class file, thenexistreturns 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 arenotclass 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 Need to Distinguish File Types in Your Work?

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 knowhere.




Published with MATLAB® R2013a

|
  • print
  • send email

Comments

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.