Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!news2!postnews.google.com!k1g2000prb.googlegroups.com!not-for-mail
From: Rune Allnor <allnor@tele.ntnu.no>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Severing ties between Matlab and data files when programming
Date: Wed, 31 Dec 2008 08:40:03 -0800 (PST)
Organization: http://groups.google.com
Lines: 45
Message-ID: <36aa9a03-e3ee-4f42-82c3-67b4c138552f@k1g2000prb.googlegroups.com>
References: <gjg5s2$533$1@fred.mathworks.com>
NNTP-Posting-Host: 77.17.71.248
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1230741603 15026 127.0.0.1 (31 Dec 2008 16:40:03 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 31 Dec 2008 16:40:03 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: k1g2000prb.googlegroups.com; posting-host=77.17.71.248; 
	posting-account=VAp5gAkAAAAmkCze5hvZtMeedpZWNthI
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET 
	CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:509351


On 31 Des, 17:16, "Ryan Utz" <r...@al.umces.edu> wrote:
> I'm constantly constructing m-files that batch process or analyze a numbe=
r of .csv files. When I'm working on one and it messes up for whatever reas=
on (i.e. an error occurs and the process is interrupted), I find that I can=
not 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 operati=
ng 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 >> is in the command=
 line.
>
> The only solution I've found to this is to shut down Matlab completely, d=
o 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. =A0
>
> Anyone have a solution?

The file stays in OPEN mode after the routine aborts. The solution
is to call FCLOSE on the file handle.

The potential problem is how to get to the file handle. If the file
was opened inside a function, you might not have access to the handle
from the matlab workspace. One solution would be to organize
the code such that you open the file on one function level, and then
call a function with the file handle as argument to do the actual
work.
If the processing function fails, you still have access to the file
handle and can close it:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data =3DmainFunction(filename)

data=3D[];
fid =3D open(filename);
try
    % loadFile.m does the work associated with the file
    data=3DloadFile(fid);
catch
    % Error during processing are handled here...
end
% ...and this statement is executed in any case
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Rune