Error using delete after closing file

Hi
I have a function that closes either two files or a file and a serial port obj. But after closing them and calling delete I get an error message (Invalid or deleted object.). I'm not sure if I'm doing something wrong here. I thought you were supposed to call delete after closing a file obj? Fclose worked fine. I checked its error code, and its closing my files.
This is my code:
myfunc(x, etc)
fclose(x);
delete(x);
clear x;
I tried ishandle on it first, and got an error there too. It didn't seem to recognise x as a fileid. I tried nesting the function so i didn't have to pass any vars in, and that didn't make a difference. I also tried removing delete and calling it from the calling function, and still got an error.

 Accepted Answer

Jan
Jan on 7 May 2016
Edited: Jan on 7 May 2016
Please read the documentation of the commands ishandle, fclose, clear and delete.
  • ishandle does not recognize file handles, but graphc and Java handles.
  • delete requires the file name, if you want to delete a file. With using a handle, delete removes graphic objects.
  • clear does not give any meaningful effect here. At the end of the function, local variables are cleared automatically, butin the caller the value is still existing.
If you do not have the file handle insider this function, you can do this:
myfunc(fid, etc) % Do not call a file handle "x"
File = fopen(fid); % Obtain the file name
fclose(fid);
delete(File);
Note: It is a good programming practise to close a file in the same function, in which it is opened, and in the same level of indentation. If seen many codes, which did not care about a proper closing of files and if it is done in an IF-branch or another function, later modifications of the code could have the side-effect, that the fclose is not reached anymore.

5 Comments

Thank you Jan, appreciate it. I must have missed that in the documentation. I did read it but obviously not carefully enough. The reason I had it in a function was that it was called by various parts of that function due to stopping the program at different points. I thought it more efficient than retyping a few lines of code each time. But even in this instance you think it would be better practice to have it done in the same function?
Quick question - would using clear in a nested function still clear it in the outer function?
btw, in my code I didn't call my file handle x, that was just in the example code I gave which conveyed what I was trying to do. I appreciate your help though.
Yes, clearing a variable in a nested function will also clear it in the outer function because it is the same variable. This is easy to check with a small example
function main_function()
% create variable
a = 2;
% call nested function which clears it
clear_var_a()
% try to display it again (this will fail)
disp(a)
function clear_var_a()
disp(a)
clear a;
end
end
@Rebecca: There is no general "best" method for programming. When you are sure, that the file is closed under all possible conditions, it is usually a benefit not to have several fclose. If it is possible I prefer schemes like:
fid = fopen(FileName);
if fid == -1
error('Cannot open file: %s', FileName);
end
failed = false;
try
Data = ImportFile(fid);
catch ME
failed = true;
end
fclose(fid);
if failed
rethrow(ME);
end
You see: This looks ugly, too. :-(
Rebecca, just a note on Ced's excellent example. For a function to be nested, you must use "end" statements (which I usually don't use) to finish off your functions. Even if the functions are in the same m-file, if you don't use the "end"s, the functions are not nested. The program will still run, though it would throw an error when it gets to disp(a) in the clear_var_a() function because "a" was not passed in or defined.
I'm not sure if you want to delete the file (on disk) or the filehandle (variable). What you are doing is deleting the file handle and then clearing the variable, neither of which is necessary, like Jan said. If you want to delete the actual from from disk then do this:
recycle on; % Send deleted files to recycle bin
delete(fullFileName); % Send file to recycle bin.
Thanks so much for the feedback guys, really appreciate it. I use the end statement when writing functions just as I prefer to, anyway. I have been using some persistent variables in a timer and in another interface, variables I'd created using the handles structure and guidata, so that's why I was wanting to clear them. Thanks so much.

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!