MATLAB: How to close a particular figure file if it exists

close figure file

how to close a particular figure file
i used the below figure file…..
figure(6),
set(gcf, 'Name','Selected Image','numbertitle','off');
imshow(inputImage);
now the second time i execute if that figure file exists i want to delete only that particular figure file… i did as below…
if exist('Selected Image', 'file')
close 'Selected Image'
end
no error is showing nor it is getting deleted….. how to write the syntax… please do reply…..

Best Answer

The created "figure" is not a "file". "Files" are found on the hard disk, not on the screen, while "figures" are the windows on the screen. Although you can store the contents of a figure to a file in the ".fig" format on the harddisk, you cannot "close" such a file.
I guess, you want to check if a figure with this name is open already. Then exist() is not sufficient, but:
existingFig = findobj(get(0), 'Children', 'flat', 'Name', 'Selected Image');
close(existingFig); % Or "delete()"
If no existing figure is found, existingFig is the empty matrix and close([]) does not perform anything.