<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760</link>
    <title>MATLAB Central Newsreader - Severing ties between Matlab and data files when programming</title>
    <description>Feed for thread: Severing ties between Matlab and data files when programming</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 31 Dec 2008 16:16:02 -0500</pubDate>
      <title>Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619401</link>
      <author>Ryan Utz</author>
      <description>I'm constantly constructing m-files that batch process or analyze a number of .csv files. When I'm working on one and it messes up for whatever reason (i.e. an error occurs and the process is interrupted), I find that I cannot move, delete, or make changes on the data file that it was last working on. If I try to, Windows tells me that another program (Matlab) is operating on the file (or something to the tune of that), even though Matlab is no longer 'Busy', the error has been posted, and a fresh &amp;gt;&amp;gt; is in the command line. &lt;br&gt;
&lt;br&gt;
The only solution I've found to this is to shut down Matlab completely, do what I need to to the data file in question, and restart Matlab to start work again. There has got to be a better way. I'm guessing (and hoping) it's something simple. 'clear' doesn't do the trick.  &lt;br&gt;
&lt;br&gt;
Anyone have a solution?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Ryan</description>
    </item>
    <item>
      <pubDate>Wed, 31 Dec 2008 16:40:03 -0500</pubDate>
      <title>Re: Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619406</link>
      <author>Rune Allnor</author>
      <description>On 31 Des, 17:16, &quot;Ryan Utz&quot; &amp;lt;r...@al.umces.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; I'm constantly constructing m-files that batch process or analyze a numbe=&lt;br&gt;
r of .csv files. When I'm working on one and it messes up for whatever reas=&lt;br&gt;
on (i.e. an error occurs and the process is interrupted), I find that I can=&lt;br&gt;
not move, delete, or make changes on the data file that it was last working=&lt;br&gt;
&amp;nbsp;on. If I try to, Windows tells me that another program (Matlab) is operati=&lt;br&gt;
ng on the file (or something to the tune of that), even though Matlab is no=&lt;br&gt;
&amp;nbsp;longer 'Busy', the error has been posted, and a fresh &amp;gt;&amp;gt; is in the command=&lt;br&gt;
&amp;nbsp;line.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The only solution I've found to this is to shut down Matlab completely, d=&lt;br&gt;
o what I need to to the data file in question, and restart Matlab to start =&lt;br&gt;
work again. There has got to be a better way. I'm guessing (and hoping) it'=&lt;br&gt;
s something simple. 'clear' doesn't do the trick. =A0&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Anyone have a solution?&lt;br&gt;
&lt;br&gt;
The file stays in OPEN mode after the routine aborts. The solution&lt;br&gt;
is to call FCLOSE on the file handle.&lt;br&gt;
&lt;br&gt;
The potential problem is how to get to the file handle. If the file&lt;br&gt;
was opened inside a function, you might not have access to the handle&lt;br&gt;
from the matlab workspace. One solution would be to organize&lt;br&gt;
the code such that you open the file on one function level, and then&lt;br&gt;
call a function with the file handle as argument to do the actual&lt;br&gt;
work.&lt;br&gt;
If the processing function fails, you still have access to the file&lt;br&gt;
handle and can close it:&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
function data =3DmainFunction(filename)&lt;br&gt;
&lt;br&gt;
data=3D[];&lt;br&gt;
fid =3D open(filename);&lt;br&gt;
try&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% loadFile.m does the work associated with the file&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data=3DloadFile(fid);&lt;br&gt;
catch&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% Error during processing are handled here...&lt;br&gt;
end&lt;br&gt;
% ...and this statement is executed in any case&lt;br&gt;
fclose(fid);&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Wed, 31 Dec 2008 16:40:19 -0500</pubDate>
      <title>Re: Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619407</link>
      <author>David </author>
      <description>&quot;Ryan Utz&quot; &amp;lt;rutz@al.umces.edu&amp;gt; wrote in message &amp;lt;gjg5s2$533$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I'm constantly constructing m-files that batch process or analyze a number of .csv files. When I'm working on one and it messes up for whatever reason (i.e. an error occurs and the process is interrupted), I find that I cannot move, delete, or make changes on the data file that it was last working on. If I try to, Windows tells me that another program (Matlab) is operating on the file (or something to the tune of that), even though Matlab is no longer 'Busy', the error has been posted, and a fresh &amp;gt;&amp;gt; is in the command line. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The only solution I've found to this is to shut down Matlab completely, do what I need to to the data file in question, and restart Matlab to start work again. There has got to be a better way. I'm guessing (and hoping) it's something simple. 'clear' doesn't do the trick.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyone have a solution?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; Ryan&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
status = fclose('all') closes all open files (except standard input, output, and error), returning 0 if successful and -1 if unsuccessful.</description>
    </item>
    <item>
      <pubDate>Wed, 31 Dec 2008 16:41:45 -0500</pubDate>
      <title>Re: Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619408</link>
      <author>ImageAnalyst</author>
      <description>On Dec 31, 11:16=A0am, &quot;Ryan Utz&quot; &amp;lt;r...@al.umces.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; I'm constantly constructing m-files that batch process or analyze a numbe=&lt;br&gt;
r of .csv files. When I'm working on one and it messes up for whatever reas=&lt;br&gt;
on (i.e. an error occurs and the process is interrupted), I find that I can=&lt;br&gt;
not move, delete, or make changes on the data file that it was last working=&lt;br&gt;
&amp;nbsp;on. If I try to, Windows tells me that another program (Matlab) is operati=&lt;br&gt;
ng on the file (or something to the tune of that), even though Matlab is no=&lt;br&gt;
&amp;nbsp;longer 'Busy', the error has been posted, and a fresh &amp;gt;&amp;gt; is in the command=&lt;br&gt;
&amp;nbsp;line.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The only solution I've found to this is to shut down Matlab completely, d=&lt;br&gt;
o what I need to to the data file in question, and restart Matlab to start =&lt;br&gt;
work again. There has got to be a better way. I'm guessing (and hoping) it'=&lt;br&gt;
s something simple. 'clear' doesn't do the trick. =A0&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Anyone have a solution?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; Ryan&lt;br&gt;
&lt;br&gt;
-------------------------------------------------------------------------&lt;br&gt;
Ryan:&lt;br&gt;
It's not a MATLAB solution but a more general one.  Just install&lt;br&gt;
Unlocker.&lt;br&gt;
&lt;a href=&quot;http://www.download.com/Unlocker/3000-2248_4-10493998.html&quot;&gt;http://www.download.com/Unlocker/3000-2248_4-10493998.html&lt;/a&gt;&lt;br&gt;
Here's the description:&lt;br&gt;
&quot;Ever had such an annoying message given by Windows: Cannot delete&lt;br&gt;
file: Access is denied. There has been a sharing violation. The source&lt;br&gt;
or destination file may be in use or the file is in use by another&lt;br&gt;
program or user. Make sure the disk is not full or write-protected and&lt;br&gt;
that the file is not currently in use. Unlocker is the solution.&lt;br&gt;
Unlocker is an Explorer extension that allows you with a simple right-&lt;br&gt;
click of the mouse on a file or folder to get rid of error message&lt;br&gt;
such as error deleting file or folder, cannot delete folder: it is&lt;br&gt;
used by another person or program.&quot;&lt;br&gt;
&lt;br&gt;
Unlocker can pry the fingers off the file of whatever application had&lt;br&gt;
gotten ahold of it.  Then you can delete the file, or reuse it, or&lt;br&gt;
whatever.  It sits in the Windows system tray and pops to life&lt;br&gt;
whenever your attempt to do something with a file is denied because&lt;br&gt;
some program has it locked.  It will bring up a window asking if you&lt;br&gt;
want to unlock the file, delete it, and I think some more options.&lt;br&gt;
Regards,&lt;br&gt;
ImageAnalyst</description>
    </item>
    <item>
      <pubDate>Wed, 31 Dec 2008 16:52:02 -0500</pubDate>
      <title>Re: Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619409</link>
      <author>Jiro Doke</author>
      <description>Rune Allnor &amp;lt;allnor@tele.ntnu.no&amp;gt; wrote in message &amp;lt;36aa9a03-e3ee-4f42-82c3-67b4c138552f@k1g2000prb.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On 31 Des, 17:16, &quot;Ryan Utz&quot; &amp;lt;r...@al.umces.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; I'm constantly constructing m-files that batch process or analyze a numbe=&lt;br&gt;
&amp;gt; r of .csv files. When I'm working on one and it messes up for whatever reas=&lt;br&gt;
&amp;gt; on (i.e. an error occurs and the process is interrupted), I find that I can=&lt;br&gt;
&amp;gt; not move, delete, or make changes on the data file that it was last working=&lt;br&gt;
&amp;gt;  on. If I try to, Windows tells me that another program (Matlab) is operati=&lt;br&gt;
&amp;gt; ng on the file (or something to the tune of that), even though Matlab is no=&lt;br&gt;
&amp;gt;  longer 'Busy', the error has been posted, and a fresh &amp;gt;&amp;gt; is in the command=&lt;br&gt;
&amp;gt;  line.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; The only solution I've found to this is to shut down Matlab completely, d=&lt;br&gt;
&amp;gt; o what I need to to the data file in question, and restart Matlab to start =&lt;br&gt;
&amp;gt; work again. There has got to be a better way. I'm guessing (and hoping) it'=&lt;br&gt;
&amp;gt; s something simple. 'clear' doesn't do the trick. =A0&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Anyone have a solution?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The file stays in OPEN mode after the routine aborts. The solution&lt;br&gt;
&amp;gt; is to call FCLOSE on the file handle.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The potential problem is how to get to the file handle. If the file&lt;br&gt;
&amp;gt; was opened inside a function, you might not have access to the handle&lt;br&gt;
&amp;gt; from the matlab workspace. One solution would be to organize&lt;br&gt;
&amp;gt; the code such that you open the file on one function level, and then&lt;br&gt;
&amp;gt; call a function with the file handle as argument to do the actual&lt;br&gt;
&amp;gt; work.&lt;br&gt;
&amp;gt; If the processing function fails, you still have access to the file&lt;br&gt;
&amp;gt; handle and can close it:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
&amp;gt; function data =3DmainFunction(filename)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; data=3D[];&lt;br&gt;
&amp;gt; fid =3D open(filename);&lt;br&gt;
&amp;gt; try&lt;br&gt;
&amp;gt;     % loadFile.m does the work associated with the file&lt;br&gt;
&amp;gt;     data=3DloadFile(fid);&lt;br&gt;
&amp;gt; catch&lt;br&gt;
&amp;gt;     % Error during processing are handled here...&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; % ...and this statement is executed in any case&lt;br&gt;
&amp;gt; fclose(fid);&lt;br&gt;
&amp;gt; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Rune&lt;br&gt;
&lt;br&gt;
If for some reason, you don't have access to the file ID (fid), then you can either do&lt;br&gt;
&lt;br&gt;
fclose('all')&lt;br&gt;
&lt;br&gt;
to close all open files. Or if you have multiple files and want to close particular files,&lt;br&gt;
&lt;br&gt;
fid = fopen('all')&lt;br&gt;
&lt;br&gt;
to get all of the IDs of the files that are currently open. Then do&lt;br&gt;
&lt;br&gt;
fopen(fid)&lt;br&gt;
&lt;br&gt;
to find the file that you want to close and then issue fclose on that ID.&lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
jiro</description>
    </item>
    <item>
      <pubDate>Wed, 31 Dec 2008 17:01:02 -0500</pubDate>
      <title>Re: Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619410</link>
      <author>James Tursa</author>
      <description>Rune Allnor &amp;lt;allnor@tele.ntnu.no&amp;gt; wrote in message &amp;lt;36aa9a03-e3ee-4f42-82c3-67b4c138552f@k1g2000prb.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On 31 Des, 17:16, &quot;Ryan Utz&quot; &amp;lt;r...@al.umces.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The file stays in OPEN mode after the routine aborts. The solution&lt;br&gt;
&amp;gt; is to call FCLOSE on the file handle.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The potential problem is how to get to the file handle. If the file&lt;br&gt;
&amp;gt; was opened inside a function, you might not have access to the handle&lt;br&gt;
&amp;gt; from the matlab workspace.&lt;br&gt;
&lt;br&gt;
As you mention, try-catch seems like a good way to go, but FYI file handles in MATLAB are simply doubles with integer values, not pointers like in C or C++. So even if the file was opened in a function and you don't have access to the handle directly, one could just guess the handle value. MATLAB seems to start with low numbers (e.g., 3) when opening files, so you could try&lt;br&gt;
&lt;br&gt;
fclose(1)&lt;br&gt;
fclose(2)&lt;br&gt;
fclose(3)&lt;br&gt;
fclose(4)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;:&lt;br&gt;
&amp;nbsp;etc.&lt;br&gt;
&lt;br&gt;
manually at the prompt until you got the file closed. Clumsy, but it should work.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Wed, 31 Dec 2008 17:25:03 -0500</pubDate>
      <title>Re: Severing ties between Matlab and data files when programming</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241760#619414</link>
      <author>Jiro Doke</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gjg8ge$e8d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Rune Allnor &amp;lt;allnor@tele.ntnu.no&amp;gt; wrote in message &amp;lt;36aa9a03-e3ee-4f42-82c3-67b4c138552f@k1g2000prb.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; On 31 Des, 17:16, &quot;Ryan Utz&quot; &amp;lt;r...@al.umces.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The file stays in OPEN mode after the routine aborts. The solution&lt;br&gt;
&amp;gt; &amp;gt; is to call FCLOSE on the file handle.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The potential problem is how to get to the file handle. If the file&lt;br&gt;
&amp;gt; &amp;gt; was opened inside a function, you might not have access to the handle&lt;br&gt;
&amp;gt; &amp;gt; from the matlab workspace.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; As you mention, try-catch seems like a good way to go, but FYI file handles in MATLAB are simply doubles with integer values, not pointers like in C or C++. So even if the file was opened in a function and you don't have access to the handle directly, one could just guess the handle value. MATLAB seems to start with low numbers (e.g., 3) when opening files, so you could try&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; fclose(1)&lt;br&gt;
&amp;gt; fclose(2)&lt;br&gt;
&amp;gt; fclose(3)&lt;br&gt;
&amp;gt; fclose(4)&lt;br&gt;
&amp;gt;    :&lt;br&gt;
&amp;gt;  etc.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; manually at the prompt until you got the file closed. Clumsy, but it should work.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&lt;br&gt;
The function &quot;fopen&quot; will give you the file ID of files that are open.&lt;br&gt;
&lt;br&gt;
fid = fopen('all');&lt;br&gt;
&lt;br&gt;
Then you can loop through each of the elements of fid to see if it's the file of your interest:&lt;br&gt;
&lt;br&gt;
for idx = 1:length(fid)&lt;br&gt;
&amp;nbsp;&amp;nbsp;if strcmp(fileIWantToClose, fopen(fid(idx)))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fclose(fid(idx));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end</description>
    </item>
  </channel>
</rss>

