From: Kimberly on
Hi folks,

I have a basic problem which I haven't been able to solve, though I think it must be quite common. I need to unlock files after the fid has been deleted ... snippets follow:

this works:

>> fid = fopen('foobar.bin', 'w');
>> fwrite(fid,7);
>> fclose(fid)
>> delete foobar.bin

but this doesn't:

>> fid = fopen('foobar.bin', 'w');
>> clear fid
>> fclose(fid)
??? Undefined function or variable 'fid'.
>> delete foobar.bin
Warning: File not found or permission denied

The only way to delete foobar.bin is to close MATLAB and do it from the OS, or reopen MATLAB.

Why on earth would I clear fid? Not on purpose, like the above example, but I have code that is crashing halfway through, (I am debugging) and often the program ends abruptly and the workspace is cleared before I get my prompt back. But each time I have to close MATLAB, hand-delete my data files, and start again from scratch. Tedious. Surely there is an easier way?

Cheers,
Kimberly

Thanks for any help
From: Pekka Kumpulainen on
"Kimberly " <k.moravec(a)cs.ucl.ac.uk> wrote in message <i1h9sr$e3k$1(a)fred.mathworks.com>...
> Hi folks,
>
> I have a basic problem which I haven't been able to solve, though I think it must be quite common. I need to unlock files after the fid has been deleted ... snippets follow:
>
> this works:
>
> >> fid = fopen('foobar.bin', 'w');
> >> fwrite(fid,7);
> >> fclose(fid)
> >> delete foobar.bin
>
> but this doesn't:
>
> >> fid = fopen('foobar.bin', 'w');
> >> clear fid
> >> fclose(fid)
> ??? Undefined function or variable 'fid'.
> >> delete foobar.bin
> Warning: File not found or permission denied
>
> The only way to delete foobar.bin is to close MATLAB and do it from the OS, or reopen MATLAB.
>
> Why on earth would I clear fid? Not on purpose, like the above example, but I have code that is crashing halfway through, (I am debugging) and often the program ends abruptly and the workspace is cleared before I get my prompt back. But each time I have to close MATLAB, hand-delete my data files, and start again from scratch. Tedious. Surely there is an easier way?
>
> Cheers,
> Kimberly
>
> Thanks for any help

You can often help yourself by reading the help of the functions you use.
doc fclose:
fclose('all') closes all open files

hth
From: Edric M Ellis on
"Kimberly " <k.moravec(a)cs.ucl.ac.uk> writes:

> I have a basic problem which I haven't been able to solve, though I
> think it must be quite common. I need to unlock files after the fid
> has been deleted ... snippets follow:
>

This sounds like a case for onCleanup - the ref page

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/oncleanup.html

uses this as its example.

Cheers,

Edric.
From: Kimberly on

>
> You can often help yourself by reading the help of the functions you use.
> doc fclose:
> fclose('all') closes all open files
>
> hth

I DO read the help and fclose('all') hadn't worked, but it does solve the problem above. I should have checked.

If I truly can't get fclose('all') to work in my code I'll repost, but probably I misspelled fclose or something.

Thanks.
From: Kimberly on

> This sounds like a case for onCleanup - the ref page
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/oncleanup.html
>
> uses this as its example.
>
Many thanks Edric, I think this is exactly what I needed!

Kimberly