Thanks to Lucius Domitius Ahenobarbus and RTL, I modified the code and it works. I just replaced this line :
close(handles.f);
...by this one :
set(handles.f,'WindowButtonUpFcn','close(gcf);');
The idea is to use the 'WindowButtonUpFcn' of the figure (images or axes don't have such callbacks) so that nothing happens on mouse click but then the figure closes itself on mouse click release. There might be neater solutions ?