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.
11:51 UTC |
Posted in Efficiency, Robustness |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
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.
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.
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.
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
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
Hi,
Is there any way to check for the existence of structure elements?
For example…
>> c
c =
cheese: ‘gruyere’
wine: ’sauvignon blanc’
>> d
d =
wine: ’sauvignon blanc’
>> if (exist(’c.cheese’)) disp(c.cheese); else disp(’no cheese here’); end
no cheese here
>> if (exist(’d.cheese’)) disp(d.cheese); else disp(’no cheese here’); end
no cheese here
Hannah-
If you are looking to see if a value of a field is set, use one of the string comparison functions, e.g.,
if strcmp(c.cheese,'gruyere') disp('Thank you') else disp('How disappointing!') endand if you are looking to see if the field exists, use isfield
if isfield(c,'cheese') if strcmp(c.cheese,'gruyere') disp('Thank you') else disp('How disappointing!') end disp('no cheese here') end–Loren
What a cool feature. As an old MATLAB user, I wanted this feature for a long time, but I’ve not noticed that it’s been already implemented long time ago. I had to find your blog earlier…
Peter & Saadie, i had the same problem: how checking existence of a MAT file, and I have to say that
exist([filepath,’filename.mat’])
works for me, returning a 2 (not a 0).