<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Existence in MATLAB</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Eric Jaquay</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-31169</link>
		<dc:creator>Eric Jaquay</dc:creator>
		<pubDate>Fri, 19 Mar 2010 18:46:05 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-31169</guid>
		<description>Thanks for your response, Loren.  I&#039;m running an optimization, and each pass of my MATLAB loop launches a new cluster job.  As a result, the filename that I need to look for is changing each time, albeit in a fairly predictable way.  Thus, I check for the filename with this statement

outputfilename=[&#039;optimize&#039; num2str(counter) &#039;.pbs.e&#039; num2str(jobID)&#039;&#039;];

which of course creates the variable, then do

exist(outputfilename,&#039;file&#039;)

(I&#039;ve actually tried it with and without the quotes around outputfilename -- in neither case does it work, but I think the correct syntax is without.  In this case, the variable outputfilename is a string representing the filename.)

A typical file name that I want to check for is something like optimize34.pbs.e7765493, which means that it&#039;s the 34th iteration of my MATLAB loop, which submitted a job that was auto-assigned jobID 7765493 on the cluster.  Once the job is finished this file is generated, and then MATLAB can import/process the data in the remainder of the loop.  The pause(300) is there because the job takes some time to finish on the cluster, and I&#039;m only checking every 5 minutes to see if it&#039;s done.

Anyway, I&#039;ll see if I can get it work using dir.  Thanks for the suggestion (I&#039;m of course open to others, too.)

Best,
Eric</description>
		<content:encoded><![CDATA[<p>Thanks for your response, Loren.  I&#8217;m running an optimization, and each pass of my MATLAB loop launches a new cluster job.  As a result, the filename that I need to look for is changing each time, albeit in a fairly predictable way.  Thus, I check for the filename with this statement</p>
<p>outputfilename=['optimize' num2str(counter) '.pbs.e' num2str(jobID)''];</p>
<p>which of course creates the variable, then do</p>
<p>exist(outputfilename,&#8217;file&#8217;)</p>
<p>(I&#8217;ve actually tried it with and without the quotes around outputfilename &#8212; in neither case does it work, but I think the correct syntax is without.  In this case, the variable outputfilename is a string representing the filename.)</p>
<p>A typical file name that I want to check for is something like optimize34.pbs.e7765493, which means that it&#8217;s the 34th iteration of my MATLAB loop, which submitted a job that was auto-assigned jobID 7765493 on the cluster.  Once the job is finished this file is generated, and then MATLAB can import/process the data in the remainder of the loop.  The pause(300) is there because the job takes some time to finish on the cluster, and I&#8217;m only checking every 5 minutes to see if it&#8217;s done.</p>
<p>Anyway, I&#8217;ll see if I can get it work using dir.  Thanks for the suggestion (I&#8217;m of course open to others, too.)</p>
<p>Best,<br />
Eric</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-31166</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 19 Mar 2010 11:30:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-31166</guid>
		<description>Eric-

The way to check for a file is using exist.  You may need to wait, as you do with the pause, to let the OS catch up.  Another way to check is to use the dir function, checking the return values.  If you can&#039;t get this to work, I suggest contacting support (link on the right).

exist with a single argument looks for the argument.  Do you try exist(arg) or exist(&#039;arg&#039;)?  That can confuse people...  exist(&#039;arg&#039;) looks for the literal string &#039;arg&#039; and exist(arg) looks for whatever the variable arg refers to.  So if using exist(arg), it doesn&#039;t automatically return 1.

--loren</description>
		<content:encoded><![CDATA[<p>Eric-</p>
<p>The way to check for a file is using exist.  You may need to wait, as you do with the pause, to let the OS catch up.  Another way to check is to use the dir function, checking the return values.  If you can&#8217;t get this to work, I suggest contacting support (link on the right).</p>
<p>exist with a single argument looks for the argument.  Do you try exist(arg) or exist(&#8216;arg&#8217;)?  That can confuse people&#8230;  exist(&#8216;arg&#8217;) looks for the literal string &#8216;arg&#8217; and exist(arg) looks for whatever the variable arg refers to.  So if using exist(arg), it doesn&#8217;t automatically return 1.</p>
<p>&#8211;loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Jaquay</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-31164</link>
		<dc:creator>Eric Jaquay</dc:creator>
		<pubDate>Thu, 18 Mar 2010 23:45:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-31164</guid>
		<description>Hi,

So I&#039;m having a similar problem to Peter&#039;s, but I actually want to check for the existence of a file, not its contents.  I&#039;m using MATLAB to control the submission of jobs to a cluster, and I don&#039;t want to proceed to the next step in my MATLAB code (which is to interpret the output of the cluster job) until it finishes. 

&lt;pre&gt;
ctlfile=[&#039;optimize&#039; num2str(counter) &#039;.ctl&#039;];   outfile=[&#039;optimize&#039; num2str(counter) &#039;.out&#039;];    runmeepcmd=[&#039;runmeep-mpi &#039; ctlfile &#039; &#039; outfile &#039; 30:00 32 2&#039;];
unix(runmeepcmd);
[status,jobID]=unix(&#039;qstat -u jaquay &#124; cut -c 1-7 &#124; sed -n -e &#039;&#039;6 p&#039;&#039;&#039;);
outputfilename=[&#039;optimize&#039; num2str(counter) &#039;.pbs.e&#039; num2str(jobID)&#039;&#039;];
pause(300)
while exist(outputfilename,&#039;file&#039;) == 0
     pause(300)
     disp(&#039;still waiting&#039;)
end
&lt;/pre&gt;

Using exist with a single argument, outputfilename, returns a 1 because the variable exists in the workspace.  Using the 2nd argument and forcing it to look for a file with that name returns 0, even though I can see the file in the directory (and &#039;ls&#039; in MATLAB shows me that file).

Any ideas?  A simpler way, perhaps?

Thanks.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>So I&#8217;m having a similar problem to Peter&#8217;s, but I actually want to check for the existence of a file, not its contents.  I&#8217;m using MATLAB to control the submission of jobs to a cluster, and I don&#8217;t want to proceed to the next step in my MATLAB code (which is to interpret the output of the cluster job) until it finishes. </p>
<pre>
ctlfile=['optimize' num2str(counter) '.ctl'];   outfile=['optimize' num2str(counter) '.out'];    runmeepcmd=['runmeep-mpi ' ctlfile ' ' outfile ' 30:00 32 2'];
unix(runmeepcmd);
[status,jobID]=unix('qstat -u jaquay | cut -c 1-7 | sed -n -e ''6 p''');
outputfilename=['optimize' num2str(counter) '.pbs.e' num2str(jobID)''];
pause(300)
while exist(outputfilename,'file') == 0
     pause(300)
     disp('still waiting')
end
</pre>
<p>Using exist with a single argument, outputfilename, returns a 1 because the variable exists in the workspace.  Using the 2nd argument and forcing it to look for a file with that name returns 0, even though I can see the file in the directory (and &#8216;ls&#8217; in MATLAB shows me that file).</p>
<p>Any ideas?  A simpler way, perhaps?</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aramendi</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-30663</link>
		<dc:creator>Aramendi</dc:creator>
		<pubDate>Tue, 06 Oct 2009 17:00:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-30663</guid>
		<description>Peter &amp; Saadie, i had the same problem: how checking existence of a MAT file, and I have to say that 

exist([filepath,&#039;filename.mat&#039;]) 

works for me, returning a 2 (not a 0).</description>
		<content:encoded><![CDATA[<p>Peter &amp; Saadie, i had the same problem: how checking existence of a MAT file, and I have to say that </p>
<p>exist([filepath,'filename.mat']) </p>
<p>works for me, returning a 2 (not a 0).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sung Soo</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-30061</link>
		<dc:creator>Sung Soo</dc:creator>
		<pubDate>Sat, 28 Feb 2009 19:07:58 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-30061</guid>
		<description>What a cool feature. As an old MATLAB user, I wanted this feature for a long time, but I&#039;ve not noticed that it&#039;s been already implemented long time ago. I had to find your blog earlier...</description>
		<content:encoded><![CDATA[<p>What a cool feature. As an old MATLAB user, I wanted this feature for a long time, but I&#8217;ve not noticed that it&#8217;s been already implemented long time ago. I had to find your blog earlier&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-29788</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 03 Oct 2008 13:51:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-29788</guid>
		<description>Hannah-

If you are looking to see if a value of a field is set, use one of the string comparison functions, e.g.,

&lt;pre class=&quot;code&quot;&gt;
if strcmp(c.cheese,&#039;gruyere&#039;)
   disp(&#039;Thank you&#039;)
else
   disp(&#039;How disappointing!&#039;)
end
&lt;/pre&gt;

and if you are looking to see if the field exists, use isfield

&lt;pre class=&quot;code&quot;&gt;
if isfield(c,&#039;cheese&#039;)
   if strcmp(c.cheese,&#039;gruyere&#039;)
      disp(&#039;Thank you&#039;)
   else
      disp(&#039;How disappointing!&#039;)
   end
   disp(&#039;no cheese here&#039;)
end
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Hannah-</p>
<p>If you are looking to see if a value of a field is set, use one of the string comparison functions, e.g.,</p>
<pre class="code">
if strcmp(c.cheese,'gruyere')
   disp('Thank you')
else
   disp('How disappointing!')
end
</pre>
<p>and if you are looking to see if the field exists, use isfield</p>
<pre class="code">
if isfield(c,'cheese')
   if strcmp(c.cheese,'gruyere')
      disp('Thank you')
   else
      disp('How disappointing!')
   end
   disp('no cheese here')
end
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hannah</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-29787</link>
		<dc:creator>Hannah</dc:creator>
		<pubDate>Fri, 03 Oct 2008 13:37:50 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-29787</guid>
		<description>Hi, 

Is there any way to check for the existence of structure elements? 

For example...

&gt;&gt; c
c = 
    cheese: &#039;gruyere&#039;
      wine: &#039;sauvignon blanc&#039;
&gt;&gt; d
d = 
    wine: &#039;sauvignon blanc&#039;
&gt;&gt; if (exist(&#039;c.cheese&#039;)) disp(c.cheese); else disp(&#039;no cheese here&#039;); end
no cheese here
&gt;&gt; if (exist(&#039;d.cheese&#039;)) disp(d.cheese); else disp(&#039;no cheese here&#039;); end
no cheese here</description>
		<content:encoded><![CDATA[<p>Hi, </p>
<p>Is there any way to check for the existence of structure elements? </p>
<p>For example&#8230;</p>
<p>&gt;&gt; c<br />
c =<br />
    cheese: &#8216;gruyere&#8217;<br />
      wine: &#8216;sauvignon blanc&#8217;<br />
&gt;&gt; d<br />
d =<br />
    wine: &#8216;sauvignon blanc&#8217;<br />
&gt;&gt; if (exist(&#8216;c.cheese&#8217;)) disp(c.cheese); else disp(&#8216;no cheese here&#8217;); end<br />
no cheese here<br />
&gt;&gt; if (exist(&#8216;d.cheese&#8217;)) disp(d.cheese); else disp(&#8216;no cheese here&#8217;); end<br />
no cheese here</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-25224</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 22 Jan 2008 21:09:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-25224</guid>
		<description>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&#039;t a file itself named &#039;myFile&#039; but the contents of myFile.  You need to do this:

if exist(myFile,&#039;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&#039;t think which will work for something not on the matlabpath).

--Loren</description>
		<content:encoded><![CDATA[<p>Saadia and Peter-</p>
<p>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&#8217;t a file itself named &#8216;myFile&#8217; but the contents of myFile.  You need to do this:</p>
<p>if exist(myFile,&#8217;file)<br />
&#8230;</p>
<p>which will use the contents of the variable myFile </p>
<p>or you might be able to use which, but in functional form</p>
<p>which(myFile)</p>
<p>and see if it returns empty or not (though I don&#8217;t think which will work for something not on the matlabpath).</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Saadia Iftikhar</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-25214</link>
		<dc:creator>Saadia Iftikhar</dc:creator>
		<pubDate>Tue, 22 Jan 2008 17:29:54 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-25214</guid>
		<description>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</description>
		<content:encoded><![CDATA[<p>How to deal with problem which Peter addressed? If File is in MAT format and want to check its existence? How to do this?</p>
<p>peter replied on October 4th, 2006 at 7:49 pm : </p>
<p>Yes, but what about</p>
<p>myFile = [path filesep ‘foo.mat’];<br />
if ~exist(’myFile’, ‘file’)<br />
%create the file<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://blogs.mathworks.com/loren/2006/02/01/existence-in-matlab/#comment-12911</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Thu, 05 Oct 2006 00:49:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=16#comment-12911</guid>
		<description>Yes, but what about

myFile = [path filesep &#039;foo.mat&#039;];
if ~exist(&#039;myFile&#039;, &#039;file&#039;)
    %create the file
end

This fails, because Matlab returns 0, thinking that myFile is a var (which it is, but it&#039;s also a file).  Hardcoding the string, of course, works.</description>
		<content:encoded><![CDATA[<p>Yes, but what about</p>
<p>myFile = [path filesep 'foo.mat'];<br />
if ~exist(&#8216;myFile&#8217;, &#8216;file&#8217;)<br />
    %create the file<br />
end</p>
<p>This fails, because Matlab returns 0, thinking that myFile is a var (which it is, but it&#8217;s also a file).  Hardcoding the string, of course, works.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

