Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Severing ties between Matlab and data files when programming
Date: Wed, 31 Dec 2008 16:52:02 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 64
Message-ID: <gjg7vi$c7e$1@fred.mathworks.com>
References: <gjg5s2$533$1@fred.mathworks.com> <36aa9a03-e3ee-4f42-82c3-67b4c138552f@k1g2000prb.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1230742322 12526 172.30.248.37 (31 Dec 2008 16:52:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 31 Dec 2008 16:52:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869871
Xref: news.mathworks.com comp.soft-sys.matlab:509354


Rune Allnor <allnor@tele.ntnu.no> wrote in message <36aa9a03-e3ee-4f42-82c3-67b4c138552f@k1g2000prb.googlegroups.com>...
> 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

If for some reason, you don't have access to the file ID (fid), then you can either do

fclose('all')

to close all open files. Or if you have multiple files and want to close particular files,

fid = fopen('all')

to get all of the IDs of the files that are currently open. Then do

fopen(fid)

to find the file that you want to close and then issue fclose on that ID.

Hope this helps.

jiro