- 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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rebecca Prescott
el 7 de Mayo de 2016
Comentada: Rebecca Prescott
el 19 de Mayo de 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 comentarios
Respuesta aceptada
Jan
el 7 de Mayo de 2016
Editada: Jan
el 7 de Mayo de 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 comentarios
Image Analyst
el 8 de Mayo de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!