- 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.
Error using delete after closing file
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rebecca Prescott
am 7 Mai 2016
Kommentiert: Rebecca Prescott
am 19 Mai 2016
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.
0 Kommentare
Akzeptierte Antwort
Jan
am 7 Mai 2016
Bearbeitet: Jan
am 7 Mai 2016
Please read the documentation of the commands ishandle, fclose, clear and delete.
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 Kommentare
Image Analyst
am 8 Mai 2016
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!