Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

how to close a particular figure file if it exists

Asked by Elysi Cochin on 9 Apr 2013

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.....

0 Comments

Elysi Cochin

Products

No products are associated with this question.

2 Answers

Answer by Jan Simon on 9 Apr 2013
Accepted 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.

4 Comments

Teja Muppirala on 10 Apr 2013

Modify it just a bit...

figure(6), 
set(gcf, 'Name','Selected Image','numbertitle','off');
imshow(rand(100,100,3));
pause(1);
existingFig = findobj(0,'Name', 'Selected Image');
close(existingFig);  % Or "delete()"
Elysi Cochin on 10 Apr 2013

thank u sir... now its working when i modified as you told... thank u all for your help.....

Jan Simon on 10 Apr 2013

@Elysi: if exist('Selected Image', 'file')==0 checks, if there is no file with the name "Selected Image". Because there is most likely no such file, when you did not create it explicitly, the close() command is executed. But this is a very strange test, because you can set any other ununsed file name also:

if exist('ThisFileDoesNotExistBecauseItsNameIsToSTRANGE!', 'file')==0
  close('Selected Image');
end
As I've explained already, the figures do not have any relation to files, and checking, if a certain file name does not exist has no connection to closing a figure.

Instead of searching the name of the figure, using the handle would be more convenient and clear:

figHandle = figure();
...
if ishandle(figHandle)  % Not closed before
  close(figHandle)
end
Jan Simon
Answer by Mahdi on 9 Apr 2013

I tried running the same thing and it worked, however, I assume that your main problem is in the if statement, not the close line.

Run the script in debugger and see if it's actually going through the if statement. Look up how the exist() outputs work because 1 is not always the output. I would suggest changing it to something like

if exist('Selected Image', 'file')~=0
close 'Selected Image'
end

0 Comments

Mahdi

Contact us