
function graph_merge(nfig,nmftit);
% Input [numers of figures to be merged: first the figure collecting all
% others]
% In order to avoid problems merge the graphs before amending them
% (changing them changes the number of 'Children') and messes up the rest
if nargin<1
    nfig=input('Give indexes of figures e.g. [1,3,6] to merge into first >> ');
end

handle_subp=get(figure(nfig(1)),'Children');
% if ~isfield(get(handle_subp(1,1)),'NextPlot');
%     handle_subp=handle_subp(2:end,:);
% end

if nargin<2
    chtit=input('CHANGE TITLE OF PLOTS? (y/n, default n) >> ' ,'s');
elseif ~isempty(nmftit)
    chtit='y';
else
    chtit='n';
end

if isempty(chtit);chtit='n';end
%   keyboard
if strcmpi(deblank(chtit),'y'); yestit=1;else;yestit=0;end;
if yestit & nargin<2
    nmftit=input('Give name of file with titles >> ','s');
    newtit=textread(nmftit,'%s','commentstyle','matlab','delimiter',';','bufsize',100000);
    newtit=newtit(end:-1:1,:);
elseif yestit & nargin>1
    newtit=textread(nmftit,'%s','commentstyle','matlab','delimiter',';','bufsize',100000);
    newtit=newtit(end:-1:1,:);
end
allhand={};
for jj=1:size(nfig,2);
    allhand{jj,1}=get(figure(nfig(jj)),'Children');
end

for kk=1:size(handle_subp,2);
    for jj=1:size(handle_subp,1);
        if handle_subp(jj,kk)~=0
            hh=get(handle_subp(jj,kk));
            if strcmp(get(handle_subp(jj,kk),'Type'),'axes')
            set(handle_subp(jj,kk),'NextPlot', 'add') %
            
            %             if isempty(strmatch(deblank(style),'y'))
            %                 ff=get(hh.Children,'LineStyle');
            %                 ff{1}=char(style);
            %                 set(hh.Children,{'LineStyle'}, ff);
            %             end
            tit=get(hh.Title,'String');
            if yestit
                set((get(handle_subp(jj,kk),'Title')),'String',newtit{jj+size(handle_subp,1)*(kk-1)})
                %                 set(hh,{'String'},newtit{jj+size(handle_subp,1)*(kk-1)});
            end
            for tt1=2:size(allhand,1);
                tmp=allhand{tt1};
                %                 if ~isfield(tmp,'Title');
                %                     tmp=tmp(2:end,:);
                %                 end
                for tt2=1:size(tmp,2);
                    for tt3=1:size(tmp(:,tt2),1)
                        %                         keyboard
                        hhtmp=get(tmp(tt3,tt2));
                        if isfield(hhtmp,'Title');
                            
                            tit2=get(hhtmp.Title,'String');
                            if strcmpi(tit,tit2)
                                for subc=1:length(hhtmp.Children)
                                    if isfield(get(hhtmp.Children(subc)),'YData');
                                        uub=get(hhtmp.Children(subc),'YData');
                                        set(hhtmp.Children(subc),'LineWidth', [1]);
                                        if ~iscell(uub)
                                            tmpuu{1,1}=uub;
                                            uub=tmpuu;
                                        end
                                        ttmm=[];
                                        for cn=1:size(uub,1);
                                            ttmm(cn,1)=size(uub{cn},2);
                                        end
                                        [ii,ser]=max(ttmm);
                                        
                                        
                                        gg=get(hhtmp.Children(subc),'Marker');
                                        if ~iscell(gg)
                                            tmpgg{1,1}=gg;
                                            gg=tmpgg;
                                        end
                                        ggl=get(hhtmp.Children(subc),'LineStyle');
                                        if ~iscell(ggl)
                                            tmpggl{1,1}=ggl;
                                            ggl=tmpggl;
                                        end
                                        ggc=get(hhtmp.Children(subc),'Color');
                                        if ~iscell(ggc)
                                            tmpggc{1,1}=ggc;
                                            ggc=tmpggc;
                                        end
                                        hold on
                                        plot(handle_subp(jj,kk),[1:length(uub{ser})],uub{ser},'LineStyle',ggl{ser},'Marker','none','MarkerSize',4,'Color',ggc{ser});
%                                         hold on
%                                         plot(handle_subp(jj,kk),[1:length(uub{ser})],0*uub{ser},'LineStyle','--','Marker','none','MarkerSize',4,'Color','k');
                                        hold off
                                        axis(handle_subp(jj,kk),'tight')
                                        limL=get(handle_subp(jj,kk),'YLim');
                                        
                                        set(handle_subp(jj,kk),'YTick',[limL(1):diff(limL)/4:limL(end)])
                                        
                                        %                                     set(handle_subp(jj,kk),'YTickLabel',sprintf('%10.2d|',[limL(1):diff(limL)/4:limL(end)]))
                                        
                                        set(handle_subp(jj,kk),'LineWidth', [1.5]);
                                        axis(handle_subp(jj),'tight')
                                        
                                    end
                                end
                                
                            end
                        end
                    end
                end
            end
            axis(handle_subp(jj),'tight')
            
            end
        end
    end
end
if nargin<2
    nmsave=input('Give name to print figure (eps & pdf) >> ','s' );
    if ~isempty(nmsave)
        if ~exist('figures','dir');
            mkdir('figures');
        end
        print(nfig(1),'-depsc2','-r800',['figures/',nmsave])
        [a,b]=system(['epstopdf ',['figures/',nmsave,'.eps']]);
        saveas(nfig(1),['figures/',nmsave,'.fig']);
        %if epstopdf is not installed use the following (the margins are not optimal though)
        %     [a,b]=system(['ps2pdf ',[nmsave,'.eps']]);
        %     [a,b]=system(['mv ',[nmsave,'.eps '],' ',[nmsave,'.pdf'],' ',[nmsave,'.pdf'],' ','../figures/.']);
    end
end

