function ChangeIcon(hfig,iconfile)
%This function is used to change the Figure HFIG's icon to the icon specified by ICONFILE
%
% Created By: Kaichang CHENG, Jul 10,2019
%**************************************************************************
narginchk(2,2);
%error(nargchk(2,2,nargin));
if ~ishandle(hfig) && ~isequal(get(hfig,'Type'),'figure')
error('The first input argument must be a Figure handle.');
end
if ~ischar(iconfile) || ~exist(iconfile,'file')
error('Icon file name is not input properly.');
end
mde = com.mathworks.mde.desk.MLDesktop.getInstance;
if isequal(get(hfig,'NumberTitle'),'off') && isempty(get(hfig,'Name'))
figTag = 'junziyang'; %Name the figure temporarily
set(hfig,'Name',figTag);
elseif isequal(get(hfig,'NumberTitle'),'on') && isempty(get(hfig,'Name'))
figTag = ['Figure ',num2str(hfig)];
elseif isequal(get(hfig,'NumberTitle'),'off') && ~isempty(get(hfig,'Name'))
figTag = get(hfig,'Name');
else
figTag = ['Figure ',num2str(hfig),': ',get(hfig,'Name')];
end
drawnow %Update figure window
jfig = mde.getClient(figTag); %Get the underlying JAVA object of the figure.
jfig.setClientIcon(javax.swing.ImageIcon(iconfile));
if isequal(get(hfig,'Name'),'junziyang')
set(hfig,'Name',''); %Delete the temporary figure name
end


