function varargout=dopGraph3laterality(varargin)
% dopOSCCI Mark II: dopGraph3laterality~ 15-11-2010 (last edit)
%
% make laterality graph
%
% accept dop.stack data structure
% OR 'data' input = epochs x epoch length x 2 (left right) matrix
%
% fprintf('\n%s\n\t%s',['Running ',mfilename],...
%     'Making Graph:'); % 25-11-2010 hidden these labels - lots of
%     unnecessary text in the output
% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
try
    inputs.varargin=varargin;
    inputs.turnOn={'test'}; % makes the figure visible automatically
    inputs.defaults=struct('data',[],...); send in a stack of data
        'stack',[],... dop.stack structure with event marker labels of data
        'sampleRate',25,...); % down sampleRate in hertz to figure out timing
        'indName','Bruce',... % individual data file identification
        'graphAbb','Lat',... % graph abbreviation - used for saving
        'graphName','Laterality',...); % graph name
        'outPos',[0 .3 1 .7],...); % outer position of figure in normalized units
        'actRej',[70 130],...); % activation rejection range
        'mk',[],...); % dop.mk structure - event names + raw and matched markers
        'epoch',[],... % lower and upper epoch values for events
        'base',[],...); % lower and upper epoch values for events
        'poi',[],...); % period of interest
        'zeroPt',0,...% event marker placement
        'yExt',.6,...); % y-axis extention beyond minMax of data
        'li',[],...); % laterality index structure
        'actWin',2,...); % activation window duration
        'actWinExt',1.2,...); % .2 portion extention on activation window area
        'evName',{'event1'},...); % event marker label
        'baseData',[],...); % if stim only event type, then input associated baseline data & create subplot
        'baseName',[]); % separate base event name
    
    gl=setGetInputsStruct(inputs);
    
    gl.translation='graph laterality';
    gl.structAbbreviations='gl';
    gl.sampleSec=gl.sampleRate^-1;
    gl.subNames={'Left & Right','Difference (Left - Right)'};
    %% check for events:
    gl.ev.names=gl.evName;
    if ~isempty(gl.stack);
        gl.ev.names=gl.stack.evNames;%fieldnames(gl.stack);
    end
    gl.ev.n=length(gl.ev.names);
    for j=1:gl.ev.n %
        %% setup figure
        gl.ev.name=gl.ev.names{j};
        gl.h(j)=figure('NumberTitle','off','Name',[gl.graphName,' ',gl.ev.name,': ',gl.indName],...
            'Units','normalized','OuterPosition',gl.outPos,'Visible','Off');%,...
        %% test
        if gl.test; set(gl.h(j),'Visible','on'); end
        %             'CloseRequestFcn',@closeFig);
        gl.hNames{j}=[gl.graphAbb,'_',gl.ev.name,'_',gl.indName];
        if ~isempty(gl.stack);
            gl.data=gl.stack.(gl.ev.name);
            if isfield(gl.zeroPt,gl.ev.name)
                gl.zeroPt=gl.zeroPt.(gl.ev.name);
            end
        end
        if ~isempty(gl.li)
            gl.ev.li=gl.li;
            if isfield(gl.li,gl.ev.name)
                gl.ev.li=gl.li.(gl.ev.name).scrn.all;
            elseif isfield(gl.li,'scrn')
                gl.ev.li=gl.li.scrn.all;
            end
            %               note: [1x444 char]
            %               LI: 2.0652
            %         LIepochs: 10
            %     LIpeakSample: 206
            %        LIpeakSec: 8.2400
            %            LIsem: 0.1877
            %            LIsig: 0
            %              LIp: 0.1523
            %          LIcohen: 0.4945
        end
        gl.xnsp=2; % X dim number of subplots (LR - upper, Dif - lower)
        gl.ynsp=1; % Y dim number of subplots (stimulus)
        if ~isempty(gl.baseData)
            % if we have baseData then we want 4 subplots:
            % LR - base & stim (upper), Dif - base & stim (lower)
            gl.ynsp=2;
        end
        gl.subplot=0; gl.subH=[]; % subplot handles
        for jj=1:gl.xnsp % L R (top), Dif (bottom)
            gl.xsubplot=jj;
            %             subplot(gl.xnsp,gl.ynsp,gl.xsubplot);
            gl.areaHandles=[];
            
            
            %% plot data
            for jjj=1:gl.ynsp
                gl.subplot=gl.subplot+1;
                figure(gl.h(j)); if ~gl.test; set(gl.h(j),'Visible','Off'); end
                % having some trouble with this figure not maintaining
                % status are current figure - this pops in on
                gl.subH(end+1)=subplot(gl.xnsp,gl.ynsp,gl.subplot);
                gl.ysubplot=jjj; % column number
                % if multiple events
                gl.dn='data'; % data name
                gl.df = ones(1,size(gl.(gl.dn)(:,:,1),2));
                if and(~isempty(gl.baseData),sum(gl.subplot==[1 3]))
                    gl.dn='baseData';
                    gl.df = zeros(1,size(gl.(gl.dn)(:,:,1),2));
                    gl.match = 1; % need to input this
                    gl.df((gl.base(gl.match,1)/gl.sampleSec)+gl.zeroPt(gl.ysubplot):(gl.base(gl.match,2)/gl.sampleSec)+gl.zeroPt(gl.ysubplot))=1;
                end
                gl.df = logical(gl.df);
                switch gl.xsubplot % L R (upper) or Dif (lower)
                    case 1
                        %% left right data = subplot 1
                        gl.lr.translation='left right';
                        gl.lr.labels={'L (left)','R (right)'}; % TX data
                        gl.lr.colours={'b','r'};
                        gl.xdata=((1:length(gl.(gl.dn)))-gl.zeroPt(gl.ysubplot))*gl.sampleSec;
                        if strcmp(gl.dn,'baseData')
                            gl.xdata = gl.xdata(gl.df);% correcting baseline for multiple events
                        end
                        gl.ydata=[];
                        
                        for i=1:size(gl.data,3)
                            
                            if size(gl.(gl.dn),1)==1 % single rows - epoch plot
                                gl.ydata(i,:)=gl.(gl.dn)(:,gl.df,i);
                            else % multiple rows - individual/overall plot
                                gl.ydata(i,:)=mean(gl.(gl.dn)(:,gl.df,i));
                            end
                            
                            plot(gl.subH(end),gl.xdata,gl.ydata(i,:),'color',gl.lr.colours{i},...
                                'DisplayName',gl.lr.labels{i},'LineWidth',2)
                            % error bars
                            %                             if isfield(gl,'li') && isfield(gl.li,'LIsem')
                            %                                 plot(gl.xdata,gl.ydata(i,:)-gl.li.LIsem,'color',gl.lr.colours{i},...
                            %                                     'LineStyle',':','LineWidth',1); % 'DisplayName',gl.lr.labels{i},
                            %                                 plot(gl.xdata,gl.ydata(i,:)+gl.li.LIsem,'color',gl.lr.colours{i},...
                            %                                     'LineStyle',':','LineWidth',1); % 'DisplayName',gl.lr.labels{i},
                            %                             end
                            gl.minMax(i,:)=[min(gl.ydata(i,:)) max(gl.ydata(i,:))];
                            if ~ishold; hold; end %if i==1; hold; end
                        end
                    case 2
                        gl.xdata=((1:length(gl.(gl.dn)))-gl.zeroPt(gl.ysubplot))*gl.sampleSec;
                        if strcmp(gl.dn,'baseData')
                            gl.xdata = gl.xdata(gl.df);% correcting baseline for multiple events
                        end
                        if size(gl.(gl.dn),1)==1
                            gl.ydata=diff(flipdim(gl.(gl.dn)(:,gl.df,:),3),1,3);
                        else
                            gl.ydata=diff(flipdim(mean(gl.(gl.dn)(:,gl.df,:)),3),1,3);
                        end
                        plot(gl.subH(end),gl.xdata,gl.ydata,'color','k','DisplayName','L - R'); hold;
                        
                        %                         if isfield(gl,'li') && isfield(gl.li,'LIsem')
                        %                             plot(gl.xdata,gl.ydata-gl.li.LIsem,'color','k',...
                        %                                 'LineStyle',':','LineWidth',1); % 'DisplayName',gl.lr.labels{i},
                        %                             plot(gl.xdata,gl.ydata+gl.li.LIsem,'color','k',...
                        %                                 'LineStyle',':','LineWidth',1); % 'DisplayName',gl.lr.labels{i},
                        %                         end
                        gl.minMax=[min(gl.ydata)...
                            max(gl.ydata)];
                end
                gl.dataAverage=mean(gl.ydata); % use this for legend placement
                gl.yLim(gl.subplot,:)=[min(min(gl.minMax))-max(max(abs(gl.minMax)))*gl.yExt ...
                    max(max(gl.minMax))+max(max(abs(gl.minMax)))*gl.yExt];
            end % ploted base & stim only if stim only is being plotted
            % now repeat processes that we want for both sides
            
            %% set axes
            %                 gl.axes=get(gl.subH(gl.subplot),'Axes');%get(gl.h(j),'CurrentAxes');
            set(get(gl.subH(gl.subplot),'Title'),'string',...
                [gl.graphName,' ',gl.ev.name,': ',strrep(gl.indName,'_','\_')]);
            
            set(gl.subH(gl.subplot),'XLim',[min(gl.xdata) max(gl.xdata)]);
            set(gl.subH(gl.subplot),'YLim',gl.yLim(gl.subplot,:));
            if gl.ysubplot==2 % make base axis same as stim only
                gl.tmp.sp=gl.xsubplot*gl.ysubplot-1;
                set(subplot(gl.xnsp,gl.ynsp,gl.tmp.sp),'YLim',gl.yLim(gl.subplot,:));
                subplot(gl.xnsp,gl.ynsp,gl.subplot); % select current plot
            end
            %                 gl.axes=get(gl.h,'CurrentAxes');
            %                 set(gl.subH(gl.subplot),'XTick',(1:size(gl.data,1)*(gl.sampleSec/60))*60)
            
            gl.tmp.sp=gl.subplot;
            if gl.ysubplot==2
                gl.tmp.sp=gl.xsubplot*gl.ysubplot-1;
            end
            if gl.xsubplot==1
                set(get(gl.subH(gl.tmp.sp),'YLabel'),'String',... %get(gl.axes,'YLabel')
                    sprintf('%s\n%s','Change in Velocity (%)','Relative to Baseline'));
            elseif gl.xsubplot==2 %&& gl.ysubplot==1
                set(get(gl.subH(gl.tmp.sp),'YLabel'),'String',...
                    sprintf('%s\n%s','Difference Velocity','(Left - Right % Change)'));
            end
            if gl.xsubplot==2 % bottom row has x-label
                set(get(gl.subH(gl.subplot),'XLabel'),'String',...
                    ['Epoch Time in seconds (0 = ',gl.ev.name,')']);
                if gl.ysubplot==2 % separate base event
                    set(get(gl.subH(gl.subplot-1),'XLabel'),'String',...
                        ['Base Epoch (0 = ',gl.baseName{1},')']);
                end
            end
            %% base
            % only do this if there is base + stimulus event type
%             if and(~isempty(gl.baseData),sum(gl.subplot==[2 4]))...
%                     || 
            if isempty(gl.baseData)
                %                     gl=plotBase(gl,j); % j = event number
                gl.tmp.sp=gl.subplot;
                if gl.ysubplot==2
                    gl.tmp.sp=gl.xsubplot*gl.ysubplot-1;
                end
                subplot(gl.xnsp,gl.ynsp,gl.tmp.sp);
                gl.areaHandles(end+1)=area(gl.subH(end),gl.base(j,:),[gl.yLim(gl.subplot,1) gl.yLim(gl.subplot,1)],...
                    gl.yLim(gl.subplot,2),'FaceColor',[.9 .9 .9],...
                    'DisplayName','base');
                subplot(gl.xnsp,gl.ynsp,gl.subplot);
            end
            
            
            %             %% baseline
            %             if ~isempty(gl.base)
            %                 gl.areaHandles(end+1)=area(gl.base(j,:),[gl.yLim(1) gl.yLim(1)],...
            %                     gl.yLim(2),'FaceColor',[.9 .9 .9],...
            %                     'DisplayName','base');
            %             end
            if and(~isempty(gl.baseData),~sum(gl.subplot==[1 3]))...
                    || isempty(gl.baseData)
                %% period of interest
                if ~isempty(gl.poi)
                    gl.areaHandles(end+1)=area(gl.subH(end),gl.poi(j,:),...
                        [gl.yLim(gl.subplot,1) gl.yLim(gl.subplot,1)],...
                        gl.yLim(gl.subplot,2),'FaceColor',[.1 .8 .3],...
                        'DisplayName','POI');
                end
                %% peak difference
                if isfield(gl,'ev') && isfield(gl.ev,'li')
                    gl.liSel=(gl.ev.li.LIpeakSample-((gl.actWin*.5)/gl.sampleSec)...
                        :gl.ev.li.LIpeakSample+((gl.actWin*.5)/gl.sampleSec))+gl.zeroPt(gl.ysubplot);
                    % this can still be outside range - make sure it's okay
                    if min(gl.liSel)<1
                        gl.liSel=1-min(gl.liSel)+gl.liSel;
                    elseif max(gl.liSel)>length(gl.xdata)
                        gl.liSel=gl.liSel-(max(gl.liSel)-length(gl.xdata));
                    end
                    gl.liXdata=gl.xdata(gl.liSel);
                    switch jj
                        case 1
                            gl.liYdata(1,:)=ones(1,length(gl.liXdata))...
                                *(max(max(abs(gl.ydata(:,gl.liSel)))*gl.actWinExt)); %gl.ydata(:,gl.liSel);
                            gl.areaHandles(end+1)=area(gl.subH(end),...
                                gl.liXdata,... % x values
                                gl.liYdata(1,:),... % y values
                                -(max(max(abs(gl.ydata(:,gl.liSel))))*gl.actWinExt));%min(min(abs(gl.ydata(:,gl.liSel))))...
                        case 2
                            gl.liYdata=gl.ydata(gl.liSel);
                            gl.areaHandles(end+1)=area(gl.subH(end),...
                                gl.liXdata,gl.liYdata,0);
                    end
                    set(gl.areaHandles(end),'FaceColor','y',...
                        'DisplayName','act. win.','EdgeColor','r',...
                        'LineStyle',':');
                    %% > peak point
                    gl.peakHandle=plot(gl.subH(end),ones(1,2)*gl.ev.li.LIpeakSec,gl.yLim(gl.subplot,:),...
                        'color','c','LineStyle','-','LineWidth',2,... % needs to be thin...maybe even behind data
                        'DisplayName','peak dif.');
                    %% >> 95% CI
                    if jj==2 && isfield(gl,'li') && isfield(gl.li,'LI95pctCI')
                        %                         gl.tmp.LIx=round(numel(gl.xdata)*.001);
                        gl.CI95pct=plot(gl.subH(end),[ones(1,3)*gl.ev.li.LIpeakSec gl.ev.li.LIpeakSec+[-(gl.actWin*.5) (gl.actWin*.5)]],...
                            [ gl.li.LImean+[-gl.li.LI95pctCI gl.li.LI95pctCI] ones(1,3)*gl.li.LImean],...
                            'color','r','LineStyle','-','LineWidth',1,... % needs to be thin...maybe even behind data
                            'DisplayName','LI & 95%CI');
                        
                        %                         gl.LIpt=plot(,...
                        %                             ones(1,2)*gl.li.LImean,...
                        %                             'color','r','LineStyle','-','LineWidth',1,... % needs to be thin...maybe even behind data
                        %                             'DisplayName','95%CI');
                        
                    end
                end
            end
            
            %                 if gl.xsubplot==1 % prev jj
            %                     disp('Subplot!!!');
            %                     % just left and right for top section
            %                     gl.lgdString=get(gl.lgd,'String');
            %                     set(gl.lgd,'String',gl.lgdString([1 2]));
            %                 end
            %             set(gl.lgd,'Box','off');
            %                 %% set axes
            %                 gl.axes=get(gl.h,'CurrentAxes');
            %                 set(get(gl.axes,'Title'),'string',[gl.subNames{jj},': ',gl.indName]);
            %
            % %                 set(gl.axes,'XLim',[min(gl.xdata) max(gl.xdata)]);
            % %                 set(gl.axes,'YLim',gl.yLim(gl.subplot,:));
            % %                 if gl.ysubplot==2 % make base axis same as stim only
            % %                     gl.tmp.sp=gl.xsubplot*gl.ysubplot-1;
            % %                     set(subplot(gl.xnsp,gl.ynsp,gl.tmp.sp),'YLim',gl.yLim(gl.subplot,:));
            % %                     subplot(gl.xnsp,gl.ynsp,gl.subplot); % select current plot
            % %                 end
            %                 %             set(gl.axes,'XTick',(1:size(gl.data,1)*(gl.sampleSec/60))*60)
            %                 gl.tmp.sp=gl.subplot;
            %                 if gl.ysubplot==2
            %                     gl.tmp.sp=gl.xsubplot*gl.ysubplot-1;
            %                 end
            %                 if gl.xsubplot==1
            %                     set(get(gl.subH(gl.tmp.sp),'YLabel'),'String',... %get(gl.axes,'YLabel')
            %                         sprintf('%s\n%s','Change in Velocity (%)','Relative to Baseline'));
            %                 elseif gl.xsubplot==2 %&& gl.ysubplot==1
            %                     set(get(gl.subH(gl.tmp.sp),'YLabel'),'String',...
            %                         sprintf('%s\n%s','Difference Velocity','(Left - Right % Change)'));
            %                 end
            %                 if gl.xsubplot==2 % bottom row has x-label
            %                     set(get(gl.axes,'XLabel'),'String',...
            %                         ['Epoch Time in seconds ( 0 = ',gl.ev.name,')']);
            %                 end
            
            %% legend
            % need to adjust for lateralisation: (probably - doesn't always
            % work out to be sensible)
            % - left (below) = north placement
            % - right (above) = south placement
            if ~isempty(gl.baseData) || isempty(gl.baseData) % nothing in the top right if base & stim only markers
                if or(gl.ynsp==1,and(gl.ynsp==2,gl.xsubplot==2))
                    gl.lgd=legend('location','southWest','orientation','horizontal');
                end
                if gl.ynsp==2
                    subplot(gl.xnsp,gl.ynsp,gl.subplot-1);
                    gl.lgd=legend('location','southWest','orientation','horizontal');
                    subplot(gl.xnsp,gl.ynsp,gl.subplot);
                end
                if gl.dataAverage<0 % right lateralised, go in top half
                    set(gl.lgd,'location','northWest');
                end
            end
            %% Error Bars/Region
            % just for the difference subplot - looks very messy otherwise
            if jj==2
                if isfield(gl,'li') && isfield(gl.li,'LIsem')
                    plot(gl.subH(end),gl.xdata,gl.ydata-(gl.li.LIsem),'color','b',...
                        'LineStyle','--','LineWidth',.5); % 'LineStyle',':''DisplayName',gl.lr.labels{i},
                    plot(gl.subH(end),gl.xdata,gl.ydata+(gl.li.LIsem),'color','b',...
                        'LineStyle','--','LineWidth',.5); % 'DisplayName',gl.lr.labels{i},
                end
            end
            %% zero point (event marker)
            for ii=1:gl.ynsp
                gl.tmp.en=gl.ev.name; % event name
                if ii==2
                    gl.tmp.en=gl.baseName;
                    subplot(gl.xnsp,gl.ynsp,gl.subplot-1);
                    %                     subplot(gl.xnsp,gl.ynsp,gl.subplot);
                end
                gl.zeroHandle=plot(gl.subH(end),[0 0],gl.yLim(gl.subplot,:),'color','g',...
                    'LineWidth',2,'DisplayName',gl.tmp.en);
            end
            subplot(gl.xnsp,gl.ynsp,gl.subplot);
            %% zero point - actvation
            for ii=1:gl.ynsp
                gl.tmp.ep=ii; % epoch
                if gl.ynsp==2
                    gl.tmp.ep=2;
                end
                if ii==2
                    gl.tmp.ep=1;
                    subplot(gl.xnsp,gl.ynsp,gl.subplot-1);
                end
                plot(gl.subH(end),gl.epoch(gl.tmp.ep,:),[0 0],'color','k','LineStyle',':',...
                    'LineWidth',1,'DisplayName','y = 0');
            end
            subplot(gl.xnsp,gl.ynsp,gl.subplot);
            %% place points & areas under
            if isfield(gl,'zeroHandle') && ~isempty(gl.zeroHandle)
                uistack(gl.zeroHandle,'bottom');
            end
            if isfield(gl,'peakHandle') && ~isempty(gl.peakHandle)
                uistack(gl.peakHandle,'bottom');
            end
            if ~isempty(gl.areaHandles) %exist(gl.baseH,'var')
                for i=1:length(gl.areaHandles);
                    uistack(gl.areaHandles(length(gl.areaHandles)+1-i),'bottom')
                end
            end
            %% LI information
            if jj==2
                if isfield(gl,'li') && isfield(gl.li,'LImean')
                    gl.LIsig='';
                    %                     if ~sum(gl.li.LIt1sig==[0 1]);gl.li.LIt1sig=0;end
                    if isfield(gl.li,'LIt1sig') && gl.li.LIt1sig
                        gl.LIsig='*';
                    end
                    gl.LIsem=999;
                    if isfield(gl.li,'LIsem')
                        gl.LIsem=gl.li.LIsem;
                    end
                    gl.LIepochs=1;
                    if isfield(gl.li,'LIepochs') && ~sum(size(gl.(gl.dn))==1)
                        gl.LIepochs=gl.li.LIepochs;
                    end
                    
                    gl.LIreport=sprintf('%s %.3g%s %s %.3g%s%i',...
                        'LI',gl.li.LImean,gl.LIsig,'(sem',gl.LIsem,...
                        '), n=',gl.LIepochs);
                    gl.LIrepPos=[gl.xdata(round(numel(gl.xdata)*.8)) gl.yLim(gl.subplot,1)*.7];%gl.minMax(1)*.99];
                    
                    if gl.dataAverage<0 % right lateralised, go in top bottom
                        gl.LIrepPos(2)=gl.yLim(gl.subplot,2)*.7;%gl.minMax(2)*.99;
                    end
                    gl.repH=text(gl.LIrepPos(1),gl.LIrepPos(2),gl.LIreport,...
                        'BackgroundColor',[.5 .5 .5],'FontSize',8,...); % default is 10
                        'Color','w');
                    %                      gl.repExt=get(gl.repH,'Extent');
                    %                      gl.LIrepPos(1)=gl.ev.li.LIpeakSec-gl.repExt(3)*.5;
                    %                      set(gl.repH,'Position',gl.LIrepPos);
                end
            end
            %             end
            %% figure portions of subplots
            if ~isempty(gl.baseData)
                gl.tmp.h=gl.subH([gl.subplot-1 gl.subplot]);
                gl.tmp.xdifs=[diff(get(gl.tmp.h(1),'xlim')) diff(get(gl.tmp.h(2),'xlim'))]; % x lengths of each column
                gl.tmp.xportion=gl.tmp.xdifs./[sum(gl.tmp.xdifs) sum(gl.tmp.xdifs)]; % relative portions of x units
                gl.tmp.pos(1,:)=get(gl.tmp.h(1),'position'); gl.tmp.pos(2,:)=get(gl.tmp.h(2),'position');
                % pos = [left up width height]
                
                gl.tmp.total_width = sum(gl.tmp.pos(:,3)); % used by graphs
                gl.tmp.gap = gl.tmp.pos(2,1) - sum(gl.tmp.pos(1,[1 3]));
                gl.tmp.new_pos = gl.tmp.pos;
                if diff(gl.tmp.xportion) % is there a difference?
                    % adjust the portions
                    gl.tmp.new_pos(:,3) = gl.tmp.total_width*gl.tmp.xportion;
                    gl.tmp.new_pos(2,1) = sum(gl.tmp.new_pos(1,[1 3]))+gl.tmp.gap*.5;
                    
                    set(gl.tmp.h(1),'position',gl.tmp.new_pos(1,:)); 
                    set(gl.tmp.h(2),'position',gl.tmp.new_pos(2,:));  
                end
                
%                 gl.tmp.sepUse=.7; % portion to use
%                 gl.tmp.sep=gl.tmp.pos(2,1)-(gl.tmp.pos(1,1)+gl.tmp.pos(1,3)); % separation between subplots
%                 gl.tmp.xfig=gl.tmp.pos(2,1)+gl.tmp.pos(2,3)-gl.tmp.pos(1,1)-gl.tmp.sep; % length of x distance of figure
%                 
%                 % adjust length for portions base
%                 gl.tmp.pos(1,3)=gl.tmp.xfig*gl.tmp.xportion(1); % adjust length of left plot
%                 
%                 % move stim starting x pos
%                 gl.tmp.adjVal=-1; % move to the left
%                 if gl.tmp.xportion(1)>gl.tmp.xportion(2) % stim longer
%                     gl.tmp.adjVal=1; % move to the right
%                 end
%                 gl.tmp.stimAdj=diff([gl.tmp.pos(2,3) gl.tmp.xfig*gl.tmp.xportion(2)]);
%                 gl.tmp.pos(2,3)=gl.tmp.xfig*gl.tmp.xportion(2); % adjust length for portions stim
%                 gl.tmp.pos(2,1)=gl.tmp.pos(2,1)+gl.tmp.stimAdj*gl.tmp.adjVal;
%                 
%                 % close gap/separation:
%                 % extend length of x
%                 gl.tmp.pos(1,3)=gl.tmp.pos(1,3)+gl.tmp.sep*gl.tmp.sepUse*gl.tmp.xportion(1); % adjust length to bit of separation
%                 gl.tmp.pos(2,3)=gl.tmp.pos(2,3)+gl.tmp.sep*gl.tmp.sepUse*gl.tmp.xportion(2); % adjust length to bit of separation
%                 % adjust stim left position...
%                 gl.tmp.pos(2,1)=gl.tmp.pos(2,1)-gl.tmp.sep*gl.tmp.sepUse*gl.tmp.xportion(2); % adjust length to bit of separation
%                 
%                 set(gl.tmp.h(1),'position',gl.tmp.pos(1,:)); set(gl.tmp.h(1),'xlim',gl.epoch(1,:));
%                 set(gl.tmp.h(2),'position',gl.tmp.pos(2,:)); set(gl.tmp.h(2),'xlim',gl.epoch(2,:));
            end
        end
        
    end
    
    %         fprintf('%s\n','Test procedure on - throwing error for debugging');
    %         plang; % should generate an error
    %     end
    
    %% set ouputs
    %    gl.out.note='li = laterality index output';
    varargout{1}=gl.h;
    varargout{2}=gl.hNames;
    %     fprintf('\t%s\n','Done');
    
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end

clear gl inputs
end
% %% ------------------------------------
% %% embedded:
%
% %% closeFig
% function closeFig(varargin)
% % fprintf('\n\t%s\n','Figure closed manually...');
% delete(varargin{1});
% % fprintf('\n\t%s\n','> turning pause off');
% % uiresume;
% end

% %% plotBase
% function gl=plotBase(gl,j)
% %% baseline
% if ~isempty(gl.base)
%     gl.areaHandles(end+1)=area(gl.base(j,:),[gl.yLim(1) gl.yLim(1)],...
%         gl.yLim(2),'FaceColor',[.9 .9 .9],...
%         'DisplayName','base');
% end
% end