function AxesHandel=PlotInPanle(PanelTag,Layout)
% PanelTag: handle of a existing panel in GUI. By the way, PanelTag cold
% also be a figure handle.
% Layout: is a struct variable contain field of 'Subplot' and 'Figure',
% where Subplot contains the row and column of subplot, and Figure
% represents the position of each figure in subplot.
%
% For example
% Layout.Subplot=[2,2]; Layout.Figure=[{[1]},{[3]},{[2,4]}];
% This indicates subplot of subplot(2,2,1),subplot(2,2,3) and subplot(2,2,[2,4]).
%
% Example1: SubplotPanel;
% Example2: h=figure;Layout.Subplot=[2,2]; Layout.Figure=[{[1]},{[3]},{[2,4]}];AxesHandel=PlotInPanle(h,Layout);
% Complied by Qianxu Yang, nioll2hyhh@gmail.com, yangqianxu@dicp.ac.cn
%
%
%--Delete old axes handle
Children=get(PanelTag,'children');
h=[findobj(Children,'Type','axes')];
if ~isempty(h)
for i=1:length(h),delete(h(i));end
end
%--Get relative position of each axes
FigureNumber=length(Layout.Figure);
H=figure('Visible','off');
for i=1:FigureNumber
h(i)=subplot(Layout.Subplot(1),Layout.Subplot(2),Layout.Figure{i});
end
%--Plot subplot in certain panle
for i=1:FigureNumber
AxesPosition=get(h(i),'position');
AxesHandel(i)= axes('units','normalized',...
'position',AxesPosition,...
'Parent',PanelTag,...
'XTick',[], 'YTick',[],...
'box','on', 'Layer','top',...
'tag',['AxesHandel',num2str(i)]);
end
close(H);