Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

February 1st, 2006

Existence in MATLAB

Users have needed access to information about the existence of variables, files, etc. in MATLAB for a long time. The function exist allows us to programmatically check for these entities.

exist can be called with one argument (a string) to test if that specific name is known and available in MATLAB in any form. If we program carefully and check the output result of exist, then we can be sure we know whether we are dealing with a MATLAB variable, a Simulink model, etc.

I often see user's calling exist with a single input argument, a name. This answers whether or not that name is somehow in the MATLAB environment. Very often, however, we really want more specific information, such as whether or not that name refers to a variable. A careful check of the output value (not just >0) can supply this information, though I rarely see code check for this.

Using the two input syntax for exist, we are able to write code that is both more robust, more readable, and frequently more efficient.

For example, if we have a program in which we ask the user to type in a variable name to process, we may then want to verify that the variable exists or else prompt the user again for a valid variable name. To do so, we might write code like this:

varname = '';
while isempty(varname)
     varname = input('Which variable would you like to process?','s');
     if ~exist(varname,'var')
         varname = '';
     end
end

When we run this code, since we are only checking for variables, MATLAB does not need to go look on the file system for all the other possible items MATLAB might be interested in such as M-Files, etc. This can result in a tremendous speed gain! And, by confining the search to variables, the code is more robust since it can't get misled if the name refers to an M-File.

5 Responses to “Existence in MATLAB”

  1. david replied on :

    Hello.

    Great blog. It is really helpfull with some aspects of Matlab.
    I would like to ask you if it is possible to control a device with matlab or “data acquisition toolbox” trough the serial port RS-232.

    thank you.

  2. loren replied on :

    You can in MATLAB or the Instrument Control Toolbox. A good place to look for this sort of information is The MathWorks support page. You can search for the function serial and read the documentation beyond the reference pages. There are also some illustrative demo pages.

  3. peter replied on :

    Yes, but what about

    myFile = [path filesep ‘foo.mat’];
    if ~exist(’myFile’, ‘file’)
    %create the file
    end

    This fails, because Matlab returns 0, thinking that myFile is a var (which it is, but it’s also a file). Hardcoding the string, of course, works.

  4. Saadia Iftikhar replied on :

    How to deal with problem which Peter addressed? If File is in MAT format and want to check its existence? How to do this?

    peter replied on October 4th, 2006 at 7:49 pm :

    Yes, but what about

    myFile = [path filesep ‘foo.mat’];
    if ~exist(’myFile’, ‘file’)
    %create the file
    end

  5. Loren replied on :

    Saadia and Peter-

    The problem is, you have a created a variable called myFile so asking if it exists gets things in a mess. And there probably isn’t a file itself named ‘myFile’ but the contents of myFile. You need to do this:

    if exist(myFile,’file)

    which will use the contents of the variable myFile

    or you might be able to use which, but in functional form

    which(myFile)

    and see if it returns empty or not (though I don’t think which will work for something not on the matlabpath).

    –Loren

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • J.B. Brown: Ah, and I am at fault for simply testing collinearity with the origin in the example above.
  • J.B. Brown: Indeed, > collinear( [0 3],[0 8],[0 -1e21+2e-15] ) ans = 1 > collinear( [0 3],[0 8],[0 -1e22+2e-15]...
  • OkinawaDolphin: Loren, thank you for telling me where to download timeit. Here are the two functions I just tested...
  • Loren: JB- It looks to me like Ilya’s solution and therefore yours are equivalent to the determinant. As Tim...
  • Loren: OkinawaDolphin, timeit can be downloaded from the File Exchange. Steve Eddins is the author. It does not ship...
  • OkinawaDolphin: It seems that neither R2007a nor R2007b have the function timeit, but I investigated computation time...
  • J.B. Brown: It would appear to me that Ilya Rozenfeld’s solution would be the cleanest. Just to help those who...
  • Loren: Markus- Congratulations on winning! And a nice illustration of how the size matters. Small enough, and the...
  • Markus: Hi Loren, which version is fastest also depends very much on the matrix dimensions. Look at my test function:...
  • Duncan: OkinawaDolphin, Regarding why your third example is slower than your second example, the result is in fact...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics