Home > matlabmk > gui_erp.m

gui_erp

PURPOSE ^

gui_erp() - Open a GUI for visualzing ERP butterfly plots and

SYNOPSIS ^

function gui_erp(cmnd_str,varargin)

DESCRIPTION ^

 gui_erp() - Open a GUI for visualzing ERP butterfly plots and
             topographies.  ERP t-scores, standard error and global field 
             power (Lehmann & Skrandies, 1980) can also be visualized. Hold 
             mouse cursor over GUI controls for an explanation of what they 
             do.  Click on waveform axes to visualize the scalp 
             topography at that point in time. Click on electrodes in scalp 
             topography to see electrode name.  Note, you can open more 
             than one "gui_erp" at a time in different windows, but you 
             cannot animate more than one gui_erp window at a time.

 Usage:
  >> gui_erp(cmnd_str,varargin);
   or
  >> gui_erp(GND_or_GRP,varargin);

 Required Input:
   cmnd_str   - One of the following strings:
                   1.  'initialize'(intialize the GUI)
                   2.  'time jump'(change the time point whose topography
                        is visualized)
                   3.  'back one'(go back one time point)
                   4.  'forward one'(go forward one time point)
                   5.  'back'(animate topography going backwards in time)
                   6.  'forward'(animate topography going forwards in time)
                   7.  'change stat'(change the ERP statistic that is being
                        visualized [t-score or standard error])
                   8.  'new time limits'(change the x-axis range on the
                        waveform x time axes)
                   9.  'new statistic limitsA'(change the y-axis range on the
                        ERP x time axis--i.e., Axis A)
                   10. 'new statistic limitsB' (change the y-axis range on the
                        t-score/stderr x time axis--i.e., Axis B)
                   11. 'change bin' (visualize a different bin)
                   12. 'update dashed lines' (redraws dashed lines
                        representing t-test time window and critical
                        t-scores)
                   13. 'redraw topo' (redraws ERP/t-score topography)
 

 Optional Inputs:
   fig_id         - [integer] ID number of the figure window in which GUI is
                    displayed (or will be displayed).
   bin            - [integer] The ID number of the bin whose ERPs will be
                    visualized. Note, Kutaslab Bin 0 is ignored and is 
                    assumed to contain cal pulses. Thus bin indexing starts 
                    at 1.  Use headinfo.m to see the set of bins stored in 
                    a GND or GRP variable
   t_test         - [integer] The ID number of the set of t-tests whose
                    results will be visualized. If specified, any optional 
                    input arguments inconsistent with this test's parameters
                    (e.g., 'bin') will be ignored.  Note, the results of
                    t-tests performed on voltages averaged across 
                    windows is not possible with this GUI; use sig_topo.m 
                    or sig_raster.m instead. Use headinfo.m to see the 
                    sets of t-test results stored in a GND or GRP variable      
   show_wind      - [vector] Two numbers (in milliseconds) indicating
                    beginning and end time points to visualize (e.g., [20 800]);
   GNDorGRP       - MATLABmk GND structure variable containing the
                    ERP/t-score data and t-test results to visualize.
   critical_t     - [vector] One or two numbers indicating critical
                    t-score(s). Time points/channels with t-scores that 
                    exceed the critical t-score(s) are significantly 
                    different from the mean of the null hypothesis.
   alpha_or_q     - [number] Number between 0 and 1 indicating the family-
                    wise alpha or FDR q level of the critical t-scores.
   test_wind      - [vector] Pairs of numbers (in milliseconds) indicating
                    beginning and end time points of the t-test
                    time window(s) (e.g., [300 500]).  Multiple time windows
                    can be specified by using semicolons to separate pairs
                    of time points (e.g., [160 180; 300 500]).
   ydir           - [-1 or 1] If -1, negative voltage is plotted up.  If 1 
                    positive voltage is plotted up. {default: -1}  
   stat           - ['t', 'gfp', or 'stder'] Statistic shown in lower GUI axis.
                    If 't', ERPs will be shown in units of t-scores. If 'GFP',
                    ERPs will be visualized using global field power. If 
                    'stder', the standard error of the ERPs will be showin 
                    in units of microvolts.
   exclude_chans  - Cell array of channel labels to exclude from
                    visualization (e.g., {'A2','lle','rhe'}).  If only one
                    channel, a single string (e.g., 'A2') is acceptable.
                    You cannot use this option AND the 't_test' option.
   include_chans  - Cell array of channel labels to include in the
                    visualization (e.g., {'MiPf','MiCe','MiPa','MiOc'}). If 
                    only one channel, a single string (e.g., 'MiCe') is 
                    acceptable.  All other channels will be ignored. You
                    cannot use this option AND the 't_test' option.

 
 Outputs:
   None


 Author:
 David Groppe
 Kutaslab, 2/2010


 Notes:
 -If you try to use this function to visualize only channels that
 have no scalp coordinates (e.g., the difference between left and right
 hemisphere homologues), the function will automatically use sig_wave.m or
 plot_wave.m instead.

 -If you try to use this function to visualize only two channels that
 have scalp coordinates (e.g., MiPf & MiCe), the function will 
 automatically plot the channel locations instead of the scalp topographies.
 This is because you can't interpolate a topography with only two
 channels.  sig_wave.m is probably a better method than gui_pow.m for 
 visualizing test results at only two channels since when you click on the 
 waveforms the name of the channel corresponding to the waveform appears 
 (i.e., it's easier to determine which waveform corresponds to which channel).
 plot_wave.m is also better than gui_erp.m for one or two channels of
 data.


 References:
    Lehmann D. & Skrandies, W. (1980) Reference-free identification of
 components of checkerboard-evoked multichannel potential fields.
 Electroencephalography and Clinical Neurophysiology. 48:609-621.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % gui_erp() - Open a GUI for visualzing ERP butterfly plots and
0002 %             topographies.  ERP t-scores, standard error and global field
0003 %             power (Lehmann & Skrandies, 1980) can also be visualized. Hold
0004 %             mouse cursor over GUI controls for an explanation of what they
0005 %             do.  Click on waveform axes to visualize the scalp
0006 %             topography at that point in time. Click on electrodes in scalp
0007 %             topography to see electrode name.  Note, you can open more
0008 %             than one "gui_erp" at a time in different windows, but you
0009 %             cannot animate more than one gui_erp window at a time.
0010 %
0011 % Usage:
0012 %  >> gui_erp(cmnd_str,varargin);
0013 %   or
0014 %  >> gui_erp(GND_or_GRP,varargin);
0015 %
0016 % Required Input:
0017 %   cmnd_str   - One of the following strings:
0018 %                   1.  'initialize'(intialize the GUI)
0019 %                   2.  'time jump'(change the time point whose topography
0020 %                        is visualized)
0021 %                   3.  'back one'(go back one time point)
0022 %                   4.  'forward one'(go forward one time point)
0023 %                   5.  'back'(animate topography going backwards in time)
0024 %                   6.  'forward'(animate topography going forwards in time)
0025 %                   7.  'change stat'(change the ERP statistic that is being
0026 %                        visualized [t-score or standard error])
0027 %                   8.  'new time limits'(change the x-axis range on the
0028 %                        waveform x time axes)
0029 %                   9.  'new statistic limitsA'(change the y-axis range on the
0030 %                        ERP x time axis--i.e., Axis A)
0031 %                   10. 'new statistic limitsB' (change the y-axis range on the
0032 %                        t-score/stderr x time axis--i.e., Axis B)
0033 %                   11. 'change bin' (visualize a different bin)
0034 %                   12. 'update dashed lines' (redraws dashed lines
0035 %                        representing t-test time window and critical
0036 %                        t-scores)
0037 %                   13. 'redraw topo' (redraws ERP/t-score topography)
0038 %
0039 %
0040 % Optional Inputs:
0041 %   fig_id         - [integer] ID number of the figure window in which GUI is
0042 %                    displayed (or will be displayed).
0043 %   bin            - [integer] The ID number of the bin whose ERPs will be
0044 %                    visualized. Note, Kutaslab Bin 0 is ignored and is
0045 %                    assumed to contain cal pulses. Thus bin indexing starts
0046 %                    at 1.  Use headinfo.m to see the set of bins stored in
0047 %                    a GND or GRP variable
0048 %   t_test         - [integer] The ID number of the set of t-tests whose
0049 %                    results will be visualized. If specified, any optional
0050 %                    input arguments inconsistent with this test's parameters
0051 %                    (e.g., 'bin') will be ignored.  Note, the results of
0052 %                    t-tests performed on voltages averaged across
0053 %                    windows is not possible with this GUI; use sig_topo.m
0054 %                    or sig_raster.m instead. Use headinfo.m to see the
0055 %                    sets of t-test results stored in a GND or GRP variable
0056 %   show_wind      - [vector] Two numbers (in milliseconds) indicating
0057 %                    beginning and end time points to visualize (e.g., [20 800]);
0058 %   GNDorGRP       - MATLABmk GND structure variable containing the
0059 %                    ERP/t-score data and t-test results to visualize.
0060 %   critical_t     - [vector] One or two numbers indicating critical
0061 %                    t-score(s). Time points/channels with t-scores that
0062 %                    exceed the critical t-score(s) are significantly
0063 %                    different from the mean of the null hypothesis.
0064 %   alpha_or_q     - [number] Number between 0 and 1 indicating the family-
0065 %                    wise alpha or FDR q level of the critical t-scores.
0066 %   test_wind      - [vector] Pairs of numbers (in milliseconds) indicating
0067 %                    beginning and end time points of the t-test
0068 %                    time window(s) (e.g., [300 500]).  Multiple time windows
0069 %                    can be specified by using semicolons to separate pairs
0070 %                    of time points (e.g., [160 180; 300 500]).
0071 %   ydir           - [-1 or 1] If -1, negative voltage is plotted up.  If 1
0072 %                    positive voltage is plotted up. {default: -1}
0073 %   stat           - ['t', 'gfp', or 'stder'] Statistic shown in lower GUI axis.
0074 %                    If 't', ERPs will be shown in units of t-scores. If 'GFP',
0075 %                    ERPs will be visualized using global field power. If
0076 %                    'stder', the standard error of the ERPs will be showin
0077 %                    in units of microvolts.
0078 %   exclude_chans  - Cell array of channel labels to exclude from
0079 %                    visualization (e.g., {'A2','lle','rhe'}).  If only one
0080 %                    channel, a single string (e.g., 'A2') is acceptable.
0081 %                    You cannot use this option AND the 't_test' option.
0082 %   include_chans  - Cell array of channel labels to include in the
0083 %                    visualization (e.g., {'MiPf','MiCe','MiPa','MiOc'}). If
0084 %                    only one channel, a single string (e.g., 'MiCe') is
0085 %                    acceptable.  All other channels will be ignored. You
0086 %                    cannot use this option AND the 't_test' option.
0087 %
0088 %
0089 % Outputs:
0090 %   None
0091 %
0092 %
0093 % Author:
0094 % David Groppe
0095 % Kutaslab, 2/2010
0096 %
0097 %
0098 % Notes:
0099 % -If you try to use this function to visualize only channels that
0100 % have no scalp coordinates (e.g., the difference between left and right
0101 % hemisphere homologues), the function will automatically use sig_wave.m or
0102 % plot_wave.m instead.
0103 %
0104 % -If you try to use this function to visualize only two channels that
0105 % have scalp coordinates (e.g., MiPf & MiCe), the function will
0106 % automatically plot the channel locations instead of the scalp topographies.
0107 % This is because you can't interpolate a topography with only two
0108 % channels.  sig_wave.m is probably a better method than gui_pow.m for
0109 % visualizing test results at only two channels since when you click on the
0110 % waveforms the name of the channel corresponding to the waveform appears
0111 % (i.e., it's easier to determine which waveform corresponds to which channel).
0112 % plot_wave.m is also better than gui_erp.m for one or two channels of
0113 % data.
0114 %
0115 %
0116 % References:
0117 %    Lehmann D. & Skrandies, W. (1980) Reference-free identification of
0118 % components of checkerboard-evoked multichannel potential fields.
0119 % Electroencephalography and Clinical Neurophysiology. 48:609-621.
0120 
0121 
0122 
0123 %%%%%%%%%%%%%%%%%% NOTES %%%%%%%%%%%%%%%%%%%%%
0124 % -Much of the mechanics of this GUI is based on information that is stored
0125 % in the figure's 'userdata' field.  The fields of the 'userdata are as
0126 % follows:
0127 % >>dat=get(gcf,'userdata')
0128 % dat =
0129 %
0130 %            fig_id: 1<-The Figure ID # of the GUI
0131 %        psbl_tests: [1 2 3 4]<-The sets of t-tests stored in the GND or
0132 %                    GRP variable that can be visualized (mean time window
0133 %                    tests or tests based on channels not loaded cannot be
0134 %                    visualized)
0135 %           t_tests: [1x4 struct]<-GND/GRP.t_test info for the possible tests
0136 %     mltplcmp_crct: 'fdr'<-General method that was used to correct for multiple
0137 %                    comparisons ('fdr' or 'perm')
0138 %             alpha: 0.0532<-q or estimated alpha level of the currently
0139 %                    visualized test
0140 %            n_wind: 1<-# of time windows of the currently visualized test
0141 %        critical_t: [-9.4493 9.4493]<-critical t-scores of the currently
0142 %                    visualized test
0143 %         null_mean: 0<-mean of the null hypothesis of the currently
0144 %                    visualized test
0145 %               erp: [31x256x42 double]<-GND/GRP.grands
0146 %          t_scores: [31x256x42 double]<-GND/GRP.grands_t
0147 %             stder: [31x256x42 double]<-GND/GRP.grands_stder
0148 %         showing_t: [26x256 double]<- t-scores of the data in dat.showing.
0149 %                    These will differ from the values in dat.t_scores if
0150 %                    the mean of the null hypothesis of the test being
0151 %                    visualized is not 0
0152 %          showingB: [26x256 double]<-waveforms currently shown in lower
0153 %                    butterfly plot in GUI
0154 %          showingA: [26x256 double]<-waveforms currently shown in upper
0155 %                    butterfly plot in GUI
0156 %           bindesc: {1x42 cell}<-bin descriptors for dat.erp
0157 %             times: [1x256 double]<-time points (in ms) for dat.erp
0158 %         plt_times: [-100 920]<-start and stop end points for waveforms
0159 %                    currently shown
0160 %          chanlocs: [1x31 struct]<-GND/GRP.chanlocs
0161 %      loaded_chans: [3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
0162 %                    23 24 25 26 27 28]<-biggest possible set of channels
0163 %                    that can be visualized
0164 %     showing_chans: [3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
0165 %                    23 24 25 26 27 28]<-set of channels currently
0166 %                    visualized
0167 %           h_timeA: 1.0099<-handle of upper butterfly plot axis
0168 %          start_pt: 1<-first waveform time point to visualize
0169 %            end_pt: 256<-last waveform time point to visualize
0170 %            absmxA: 17.1658<-maximum value of absolute value of waveforms
0171 %                    being visualized for upper butterfly plot
0172 %         h_t0lineA: 3.0099<-handle for line marking time=0 in upper
0173 %                    butterfly plot
0174 %        h_showingA: [31x1 double]<-[26x1 double]<-handle for shown
0175 %                    waveforms in upper butterfly plot
0176 %           h_lineA: 35.0099<-handle for vertical line indicating which
0177 %                    topography is being visualized in upper butterfly plot
0178 %          h_wind1A: 36.0099<-handle for start points of test time
0179 %                    window(s) in upper butterfly plot
0180 %          h_wind2A: 37.0099<-handle for stop points of test time
0181 %                    window(s) in upper butterfly plot
0182 %      h_time_ylabA: 38.0099<-handle for y-axis label on the waveform axis
0183 %                    for upper butterfly plot
0184 %      h_time_title: 39.0099<-handle for title on the waveform axis
0185 %           h_timeB: 41.0099<-handle of lower butterfly plot axis
0186 %            absmxB: 14.5909<-maximum value of absolute value of waveforms
0187 %                    being visualized for lower butterfly plot
0188 %         h_t0lineB: 43.0099<-handle for line marking time=0 in lower
0189 %                    butterfly plot
0190 %        h_showingB: [31x1 double]<-handle for shown waveforms in lower
0191 %                    butterfly plot
0192 %           h_lineB: 75.0099<-handle for vertical line indicating which
0193 %                    topography is being visualized in lower butterfly plot
0194 %          h_wind1B: 76.0099<-handle for start points of test time
0195 %                    window(s) in lower butterfly plot
0196 %          h_wind2B: 77.0099<-handle for stop points of test time
0197 %                    window(s) in lower butterfly plot
0198 %           h_crit1: 78.0099<-handle for red horizontal dashed line indicating
0199 %                    critical t-score
0200 %           h_crit2: 80.0099<-handle for 2nd red horizontal dashed line indicating
0201 %                    critical t-score (used if two tailed test [i.e., two
0202 %                    critical t-scores])
0203 %           h_alph1: 79.0099<-handle for text box indicating alpha level.
0204 %           h_alph2: 81.0099<-handle for 2nd text box indicating alpha level.
0205 %                    (used if two tailed test [i.e., two critical
0206 %                    t-scores])
0207 %      h_time_ylabB: 83.0099<-handle for y-axis label on the waveform axis
0208 %                    for lower butterfly plot
0209 %           h_topoA: 85.0099<-handle of upper topography axis
0210 %           h_cbarA: 167.0099<-handle of topography colorbar legend for
0211 %                    upper axis
0212 %       h_topo_time: 171.0099<-handle of text box indicating the time point
0213 %                    whose topography is currently being visualized
0214 %           h_topoB: 173.0099<-handle of lower topography axis
0215 %           h_cbarB: 254.0099<-handle of topography colorbar legend for
0216 %                    lower axis
0217 %           h_back1: 257.0099<-handle of button that moves the vertical
0218 %                    waveform line (i.e., the point whose topography
0219 %                    is currently being visualized) back one time point
0220 %        h_forward1: 258.0099<-handle of button that moves the vertical
0221 %                    waveform line (i.e., the point whose topography
0222 %                    is currently being visualized) forward one time
0223 %                    point
0224 %            h_back: 259.0099<-handle of button that animates the vertical
0225 %                    waveform line (i.e., the point whose topography
0226 %                    is currently being visualized) back in time
0227 %         h_forward: 260.0099<-handle of button that animates the vertical
0228 %                    waveform line (i.e., the point whose topography
0229 %                    is currently being visualized) forward in time
0230 %            h_stop: 261.0099<-handle of button that stops animation of the
0231 %                    vertical waveform line (i.e., the point whose
0232 %                    topography is currently being visualized)
0233 %             h_bin: 264.0099<-handle of the scroll menu indicating which
0234 %                    bin is being visualized
0235 %           h_ptest: 266.0099<-handle of the scroll menu indicating which
0236 %                    set of t-tests are being visualized
0237 %        h_testwind: 268.0099<-handle of the text box indicating the
0238 %                    boundaries of the t-test time windows
0239 %         h_critval: 270.0099<-handle of the text box indicating the
0240 %                    critical t-score values of the set of t-tests
0241 %            h_stat: 272.0099<-handle of the scroll menu indicating which
0242 %                    statistic to plot (t-score of ERPs, standard
0243 %                    error of ERPs)
0244 %       h_timerange: 274.0099<-handle of the text box indicating the
0245 %                    minimum and maximum time values on the waveform axis
0246 %      h_statrangeA: 276.0099<-handle of the text box indicating the
0247 %                    minimum and maximum statistics values on the
0248 %                    upper butterfly plot (i.e., Axis A: ERPs)
0249 %      h_statrangeB: 278.0099<-handle of the text box indicating the
0250 %                    minimum and maximum statistics values on the
0251 %                    lower butterfly plot (i.e., Axis A: t-scores,
0252 %                    standard error)
0253 %          help_msg: [1x447 char]<-message displayed when help button is
0254 %                    pressed
0255 %         interrupt: 1<- field for topography animation
0256 
0257 
0258 %%%%%%%%% POSSIBLE FUTURE DEVELOPMENT %%%%%%%%%
0259 %
0260 %-Make it possible to animate more than one GUI at a time?
0261 %
0262 %-Make it possible to change topo color limits?  I don't know how do-able
0263 %this is since values that exceed topo color limits are impossible to
0264 %distinguish from values that are at the topo color limits.  Currently topo
0265 %is automatically scaled to +/- the absolute maxima of the waveforms
0266 %currently shown (parts of the waveform before/after the "showing time
0267 %window" are ignored).
0268 %
0269 %-Make it possible to right-click on axis to get them to pop-up in a new
0270 %figure?
0271 %
0272 %-Make color scheme same as EEGLAB GUIs (see variabl frm_col)?
0273 %
0274 %-Make time x erp axis title an optional input argument?
0275 %
0276 %-Make topo scale an option
0277 %
0278 %-Set topo color scale to green or empty when only one channel?, or delete
0279 % it?
0280 %
0281 %-Add Cohen's d as a possible statistic?
0282 %
0283 %-Possibly fix use of axes command (see yellow warnings) to speed up
0284 % animations?
0285 %
0286 %-Make frm_col=[1 1 1]*.702 the same light blue color as EEGLAB later
0287 % (currently it's gray)?
0288 
0289 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%%
0290 % 4/9/2010-GUI now can visualize standard error, has a permutation test menu,
0291 % can deal with tests on multiple time windows and tests with a non-zero
0292 % null mean.  Code was tidied up a bit as well.
0293 %
0294 % 4/12/2010-GUI now simultaneously visualizes ERPs and t-score of ERPs or
0295 % standard error of ERPs
0296 %
0297 % 5/5/2010-Revised to be compatible with FDR correction code. Revised to
0298 % accept FDR control of t-tests.
0299 %
0300 % 5/12/2010-Revised so that topographies update at same time (instead of
0301 % staggered)
0302 %
0303 % 10/4/2010-Add global field power as possible statistic with which to
0304 % visualize ERPs in the lower waveform plot.  GFP is the only waveform
0305 % option usable if there's only one participant in the bin being visualized
0306 %
0307 % 10/25/2010-Revised to be able to deal with GRP variables that don't
0308 % contain data from sufficient subjects for calculating t-scores
0309 %
0310 % 11/1/2010-Now checks to make sure GND or GRP variable contains at least
0311 % one ERP and returns an error if not
0312 %
0313 % 12/10/2010-'show_wind' option was ineffectual. Now it works.
0314 %
0315 % 12/17/2010-topoplotMK can't plot topographies if there are only two
0316 % channels of data.  Now, if there are only two channels with scalp
0317 % coordinates, only the electrode locations are shown. Code checks to make
0318 % sure requested bin exists
0319 %
0320 % 3/9/2011-Code now checks to make sure there are data in a bin before
0321 % trying to visualize them.
0322 %
0323 % 6/3/2011-Fixed minor bug with viewing GRP variables and made compatible
0324 % with cluster-based tests
0325 
0326 function gui_erp(cmnd_str,varargin)
0327 
0328 p=inputParser;
0329 p.addRequired('cmnd_str',@(x) ischar(x) || isstruct(x));
0330 p.addParamValue('fig_id',[],@(x) isempty(x) || (isnumeric(x) && (length(x)==1)));
0331 p.addParamValue('bin',1,@(x) isnumeric(x) && (length(x)==1));
0332 p.addParamValue('show_wind',[],@(x) isempty(x) || (isnumeric(x) && (length(x)==2)));
0333 p.addParamValue('GNDorGRP',[],@isstruct);
0334 p.addParamValue('critical_t',[],@(x) isempty(x) || (isnumeric(x) && (length(x)<=2)));
0335 p.addParamValue('test_wind',[],@(x) isempty(x) || (isnumeric(x) && (size(x,2)==2)));
0336 p.addParamValue('ydir',-1,@(x) isnumeric(x) && (length(x)==1));
0337 p.addParamValue('stat','t',@(x) ischar(x) || strcmpi(x,'t') || strcmpi(x,'stder'));
0338 p.addParamValue('exclude_chans',[],@(x) ischar(x) || iscell(x) || isempty(x));
0339 p.addParamValue('include_chans',[],@(x) ischar(x) || iscell(x) || isempty(x));
0340 p.addParamValue('alpha_or_q',[],@(x) isempty(x) || isnumeric(x));
0341 p.addParamValue('t_test',[],@(x) isempty(x) || (isnumeric(x) && (length(x)==1) && (x>0)));
0342 p.addParamValue('verblevel',2,@(x) isnumeric(x) && (length(x)==1));
0343 p.parse(cmnd_str,varargin{:});
0344 
0345 if ~verLessThan('matlab','8.1')
0346    warning('gui_erp.m does not yet work with your version of Matlab. Try an older version.');
0347    return;
0348 end
0349 
0350 if isstruct(cmnd_str)
0351     % Assume cmnd_str is really a GND or GRP variable and that the desired
0352     % command string is 'initialize'.  Call gui_erp with same
0353     % optional arguments
0354     gui_erp('initialize','GNDorGRP',cmnd_str,'fig_id',p.Results.fig_id, ...
0355         'bin',p.Results.bin, ...
0356         'critical_t',p.Results.critical_t, ...
0357         'test_wind',p.Results.test_wind, ...
0358         'show_wind',p.Results.show_wind, ...
0359         'ydir',p.Results.ydir, ...
0360         'stat',p.Results.stat, ...
0361         'exclude_chans',p.Results.exclude_chans, ...
0362         'include_chans',p.Results.include_chans, ...
0363         'alpha_or_q',p.Results.alpha_or_q, ...
0364         't_test',p.Results.t_test, ...
0365         'verblevel',p.Results.verblevel);
0366     return;
0367 end
0368 
0369 % Ensure passed command is legal
0370 psbl_cmnds{1}='initialize';
0371 psbl_cmnds{2}='time jump';
0372 psbl_cmnds{3}='back one';
0373 psbl_cmnds{4}='forward one';
0374 psbl_cmnds{5}='back';
0375 psbl_cmnds{6}='forward';
0376 psbl_cmnds{7}='change stat';
0377 psbl_cmnds{8}='new time limits';
0378 psbl_cmnds{9}='new statistic limitsA';
0379 psbl_cmnds{10}='change bin';
0380 psbl_cmnds{11}='new statistic limitsB';
0381 psbl_cmnds{12}='update dashed lines';
0382 psbl_cmnds{13}='update critical t';
0383 psbl_cmnds{14}='redraw topo';
0384 psbl_cmnds{15}='change test';
0385 if ~ismember(cmnd_str,psbl_cmnds),
0386     error('''%s'' is not a valid value of cmnd_str for gui_erp.m',cmnd_str); 
0387 end
0388 
0389 
0390 if ~strcmp(cmnd_str,'initialize')
0391     if isempty(p.Results.fig_id),
0392         fig_id=gcbf;
0393     else
0394         fig_id=p.Results.fig_id;
0395     end
0396     if ~strcmp(get(fig_id,'tag'),'gui_erp')
0397         % If the current figure does not have the right
0398         % tag, find the one that does.
0399         h_figs = get(0,'children');
0400         fig_id = findobj(h_figs,'flat',...
0401             'tag','gui_erp');
0402         if isempty(fig_id),
0403             % If gui_erp does not exist
0404             % initialize it. Then run the command string
0405             % that was originally requested.
0406             gui_erp('initialize');
0407             gui_erp(cmnd_str);
0408             return;
0409         end
0410     end
0411     
0412     % At this point we know that h_fig is the handle
0413     % to a figure containing the GUI of interest to
0414     % this function (it's possible that more than one of these
0415     % GUI figures is open).  Therefore we can use this figure
0416     % handle to cut down on the number of objects
0417     % that need to be searched for tag names as follows:
0418     dat=get(gcbf,'userdata');
0419 end
0420 
0421 % Manage VERBLEVEL
0422 if isempty(p.Results.verblevel),
0423     VERBLEVEL=2; %not global, just local
0424 else
0425     VERBLEVEL=p.Results.verblevel;
0426 end
0427 
0428 % INITIALIZE THE GUI SECTION.
0429 if strcmp(cmnd_str,'initialize')
0430     if isempty(p.Results.GNDorGRP.grands)
0431         error('There are no bins (i.e., ERPs) in this GND or GRP variable.  You need to add bins before you can visualize them with the ERP GUI.');
0432     end
0433     
0434     % Creates a new GUI (even if one already exists)
0435     if isempty(p.Results.fig_id),
0436         dat.fig_id=figure;
0437     else
0438         dat.fig_id=p.Results.fig_id;
0439         figure(dat.fig_id);
0440         clf
0441     end
0442     set(dat.fig_id,'name',['ERP GUI ' p.Results.GNDorGRP.exp_desc],'tag','gui_erp', ...
0443         'MenuBar','none','position',[139 42 690 700]);
0444     
0445     %% Manage t-test results
0446     n_t_tests=length(p.Results.GNDorGRP.t_tests);
0447     dat.psbl_tests=[];
0448     temp_bins=zeros(1,n_t_tests);
0449     for d=1:n_t_tests,
0450         if ~strcmpi(p.Results.GNDorGRP.t_tests(d).mean_wind,'yes'),
0451             dat.psbl_tests=[dat.psbl_tests d];
0452             temp_bins(d)=p.Results.GNDorGRP.t_tests(d).bin;
0453         end
0454     end
0455     temp_bins=temp_bins(dat.psbl_tests);
0456     n_psbl_tests=length(dat.psbl_tests);
0457     dat.t_tests=p.Results.GNDorGRP.t_tests(dat.psbl_tests);
0458     crnt_ttest=n_psbl_tests+1; %set of t-tests being visualized, default is None/Manual
0459     
0460     use_ttest=0;
0461     if isempty(p.Results.t_test),
0462         if isempty(p.Results.include_chans) && isempty(p.Results.exclude_chans) ...
0463                 && isempty(p.Results.critical_t) && isempty(p.Results.alpha_or_q) ...
0464                 && isempty(p.Results.test_wind),
0465             %search for set of t-tests that have been performed on bin being
0466             %visualized (if potentially incompatible optional input
0467             %arguments have NOT been specified)
0468             test_ids=find(p.Results.bin==temp_bins);
0469             if ~isempty(test_ids),
0470                 use_ttest=1;
0471                 crnt_ttest=test_ids(1); %in case there's more than one test
0472                 if VERBLEVEL>=2
0473                     fprintf('Plotting results of t-tests set %d.  Any input arguments (e.g., ''critical_t'') inconsistent with this test will be ignored.\n', ...
0474                         dat.psbl_tests(crnt_ttest));
0475                 end
0476             end
0477         end
0478     else
0479         if p.Results.t_test>n_t_tests,
0480             error('Argument ''t_test'' value of %d exceeds the number of test results stored in this GND/GRP variable (i.e., %d).', ...
0481                 p.Results.t_test,n_t_tests);
0482         elseif ~ismember(p.Results.t_test,dat.psbl_tests),
0483             error('t-test set %d in this GND/GRP variable was performed on mean amplitudes within one or more time windows.  You cannot visualize such tests with gui_erp.m.', ...
0484                 p.Results.t_test);
0485         else
0486             use_ttest=1;
0487             crnt_ttest=find(dat.psbl_tests==p.Results.t_test);
0488             if VERBLEVEL>=2
0489                 fprintf('Plotting results of t-test set %d.  Any input arguments (e.g., ''bin'') inconsistent with this test will be ignored.\n', ...
0490                     p.Results.t_test);
0491             end
0492         end
0493     end
0494     if use_ttest,
0495         % set all other optional arguments to be
0496         % consistent with t-test
0497         bin=dat.t_tests(crnt_ttest).bin;
0498         if isnan(dat.t_tests(crnt_ttest).estimated_alpha)
0499             dat.alpha=dat.t_tests(crnt_ttest).desired_alphaORq;
0500             dat.mltplcmp_crct='fdr';
0501         else
0502             dat.alpha=dat.t_tests(crnt_ttest).estimated_alpha;
0503             dat.mltplcmp_crct='perm';
0504         end
0505         test_wind=dat.t_tests(crnt_ttest).time_wind;
0506         dat.n_wind=size(test_wind,1);
0507         include_chans=dat.t_tests(crnt_ttest).include_chans;
0508         dat.critical_t=dat.t_tests(crnt_ttest).crit_t;
0509         dat.null_mean=dat.t_tests(crnt_ttest).null_mean;
0510     else
0511         % no t-test, use optional inputs or defaults
0512         bin=p.Results.bin;
0513         test_wind=p.Results.test_wind;
0514         include_chans=p.Results.include_chans;
0515         dat.alpha=p.Results.alpha_or_q;
0516         dat.critical_t=p.Results.critical_t;
0517         dat.null_mean=0;
0518     end
0519     
0520     
0521     %% Make sure user hasn't asked for a bin that doesn't exist or that
0522     % doesn't contain any data
0523     if bin>length(p.Results.GNDorGRP.bin_info)
0524         close(gcf);
0525         error('You asked to visualize Bin %d, but your GND/GRP variable only contains %d bins.', ...
0526             bin,length(p.Results.GNDorGRP.bin_info));
0527     elseif (isfield(p.Results.GNDorGRP,'sub_ct') && ~p.Results.GNDorGRP.sub_ct(bin)) || ...
0528             (isfield(p.Results.GNDorGRP.bin_info(bin),'n_subsA') && ( ~p.Results.GNDorGRP.bin_info(bin).n_subsA || ~p.Results.GNDorGRP.bin_info(bin).n_subsB))
0529         close(gcf);
0530         error('You asked to visualize Bin %d, but your GND/GRP variable doesn''t have any data in that bin.', ...
0531             bin);
0532     end
0533         
0534     %% Figure out which channels to ignore if any
0535     n_chan=length(p.Results.GNDorGRP.chanlocs);
0536     %Make sure exclude & include options were not both used.
0537     if ~isempty(include_chans) && ~isempty(p.Results.exclude_chans)
0538         if use_ttest,
0539             error('You cannot specify a set of t-tests to visualize and use the ''exclude_chans'' option.');
0540         else
0541             error('You cannot use BOTH ''include_chans'' and ''exclude_chans'' options.');
0542         end
0543     end
0544     if ischar(p.Results.exclude_chans),
0545         exclude_chans{1}=p.Results.exclude_chans;
0546     elseif isempty(p.Results.exclude_chans)
0547         exclude_chans=[];
0548     else
0549         exclude_chans=p.Results.exclude_chans;
0550     end
0551     if ischar(include_chans),
0552         temp_var=include_chans;
0553         clear include_chans;
0554         include_chans{1}=temp_var;
0555         clear temp_var;
0556     end
0557     if ~isempty(exclude_chans),
0558         ignore_chans=zeros(1,length(exclude_chans)); %preallocate mem
0559         ct=0;
0560         for x=1:length( exclude_chans),
0561             found=0;
0562             for c=1:n_chan,
0563                 if strcmpi(exclude_chans{x},p.Results.GNDorGRP.chanlocs(c).labels),
0564                     found=1;
0565                     ct=ct+1;
0566                     ignore_chans(ct)=c;
0567                 end
0568             end
0569             if ~found,
0570                 watchit(sprintf('I attempted to exclude %s.  However no such electrode was found in GND/GRP variable.', ...
0571                     exclude_chans{x}));
0572             end
0573         end
0574         ignore_chans=ignore_chans(1:ct);
0575         loaded_chans=setdiff(1:n_chan,ignore_chans);
0576     elseif ~isempty(include_chans),
0577         loaded_chans=zeros(1,length(include_chans)); %preallocate mem
0578         ct=0;
0579         for x=1:length(include_chans),
0580             found=0;
0581             for c=1:n_chan,
0582                 if strcmpi(include_chans{x},p.Results.GNDorGRP.chanlocs(c).labels),
0583                     found=1;
0584                     ct=ct+1;
0585                     loaded_chans(ct)=c;
0586                 end
0587             end
0588             if ~found,
0589                 watchit(sprintf('I attempted to include %s.  However no such electrode was found in GND/GRP variable.', ...
0590                      include_chans{x}));
0591             end
0592         end
0593         loaded_chans=loaded_chans(1:ct);
0594     else
0595         loaded_chans=1:n_chan;
0596     end
0597     if isempty(loaded_chans),
0598        error('No channels selected for visualization!'); 
0599     end
0600     %Check to see if any channels are missing coordinates
0601     yes_coord=zeros(1,n_chan);
0602     for c=loaded_chans,
0603         if ~isempty(p.Results.GNDorGRP.chanlocs(c).theta) && ~isnan(p.Results.GNDorGRP.chanlocs(c).theta)
0604             yes_coord(c)=1;
0605         end
0606     end
0607     if (sum(yes_coord)==0)
0608         if use_ttest
0609             %t-test results should be shown
0610             watchit(sprintf('None of the channels you wish to visualize have scalp coordinates.\nIt is pointless to use the ERP GUI.  Using sig_wave.m instead.'));
0611             close(gcf);
0612             sig_wave(p.Results.GNDorGRP,crnt_ttest,'ydir',p.Results.ydir, ...
0613                 'verblevel',p.Results.verblevel);
0614         else
0615             watchit('None of the channels you wish to visualize have scalp coordinates.  It is pointless to use the ERP GUI.  Using plot_wave.m instead.');
0616             close(gcf);
0617             if ~isempty(include_chans),
0618                 plot_wave(p.Results.GNDorGRP,bin,'ydir',p.Results.ydir, ...
0619                     'verblevel',p.Results.verblevel,'include_chans',include_chans);
0620             else
0621                 %Note, exclude_chans should be empty if all channels are to
0622                 %be shown
0623                 plot_wave(p.Results.GNDorGRP,bin,'ydir',p.Results.ydir, ...
0624                     'verblevel',p.Results.verblevel,'exclude_chans',exclude_chans);
0625             end
0626         end
0627         return
0628     elseif sum(yes_coord)<length(loaded_chans),
0629         watchit(sprintf('%d channels do not have scalp coordinates.  The waveforms for such channels will be visualized but they the will not be represented in the scalp topography.', ...
0630             length(loaded_chans)-sum(yes_coord)));
0631     end
0632         
0633     %remove any sets of t-tests peformed on channels that will not be
0634     %included in the GUI
0635     use_tests=[];
0636     for d=1:n_psbl_tests,
0637        if ~isempty(intersect(loaded_chans,dat.t_tests(d).used_chan_ids)),
0638           use_tests=[use_tests d]; 
0639        end
0640     end
0641     dat.psbl_tests=dat.psbl_tests(use_tests);
0642     dat.t_tests=dat.t_tests(use_tests);
0643     crnt_ttest=find(crnt_ttest==[use_tests n_psbl_tests+1]); % "n_psbl_tests+1" adds the manual/no test option
0644     n_psbl_tests=length(use_tests);
0645     
0646     %% attach data to figure
0647     dat.erp=p.Results.GNDorGRP.grands;
0648     dat.t_scores=p.Results.GNDorGRP.grands_t;
0649     dat.stder=p.Results.GNDorGRP.grands_stder;
0650     mn_erp_across_chans=mean(dat.erp,1);
0651     dat.gfp=squeeze(sqrt(mean( (dat.erp-repmat(mn_erp_across_chans,[n_chan 1 1])).^2,1)));
0652     %t-scores of data that will be shown
0653     if (crnt_ttest<=n_psbl_tests) && (dat.t_tests(crnt_ttest).null_mean),
0654         %The mean of the null hypothesis is non-zero
0655         dat.showing_t=squeeze( (p.Results.GNDorGRP.grands(loaded_chans,:,bin)- ...
0656             dat.t_tests(crnt_ttest).null_mean)./p.Results.GNDorGRP.grands_stder(loaded_chans,:,bin) ); 
0657     else
0658         dat.showing_t=squeeze(p.Results.GNDorGRP.grands_t(loaded_chans,:,bin)); 
0659     end
0660     %data that will be shown
0661     if isinf(dat.t_scores(1,1,bin)) || isnan(dat.t_scores(1,1,bin)) || strcmpi(p.Results.stat,'gfp'),
0662         %default to global field power if there's only one subject in the
0663         %bin (and standard deviation isn't defined). For within-subject
0664         %t-scores, stdev will bin Inf/-Inf when there's only one subject.
0665         %For between-subject t-scores, stdev will bin NaN when there are
0666         %insufficient subjects in one or both groups.
0667         dat.showingB=dat.gfp(:,bin)'; 
0668     elseif strcmpi(p.Results.stat,'t'),
0669         dat.showingB=dat.showing_t;
0670     else
0671         %standard error
0672         dat.showingB=squeeze(p.Results.GNDorGRP.grands_stder(loaded_chans,:,bin)); 
0673     end
0674     dat.showingA=squeeze(p.Results.GNDorGRP.grands(loaded_chans,:,bin)); %always show ERPs in top axis
0675     
0676     n_bin=length(p.Results.GNDorGRP.bin_info);
0677     dat.bindesc=cell(1,n_bin);
0678     for b=1:n_bin,
0679         dat.bindesc{b}=p.Results.GNDorGRP.bin_info(b).bindesc;
0680     end
0681     dat.times=p.Results.GNDorGRP.time_pts;
0682     if isempty(p.Results.show_wind),
0683         dat.plt_times=[p.Results.GNDorGRP.time_pts(1) p.Results.GNDorGRP.time_pts(end)];
0684     else
0685         dat.plt_times=p.Results.show_wind;
0686     end
0687     dat.chanlocs=p.Results.GNDorGRP.chanlocs;
0688     dat.loaded_chans=loaded_chans;
0689     dat.showing_chans=loaded_chans;
0690     critical_t=dat.critical_t;
0691     ydir=p.Results.ydir; 
0692     if length(critical_t)>2,
0693        critical_t=critical_t(1:2); 
0694     end
0695     dat.critical_t=critical_t;
0696     
0697     %
0698     %%%%%%%%%%%%%%%%%%%%%% AXIS A: Time x ERP Axes %%%%%%%%%%%%%%%%%%%%%%%%
0699     %
0700     
0701     frm_col=[1 1 1]*.702;
0702     uipanel(dat.fig_id,...
0703         'Units','normalized', ...
0704         'Position',[ 0 0.59 .72 0.42 ],...
0705         'shadowcolor','k', ...
0706         'highlightcolor',frm_col, ...
0707         'foregroundcolor',frm_col, ...
0708         'backgroundcolor',frm_col);
0709     
0710     dat.h_timeA=axes('position',[0.1 .62 .6 .33]);
0711     dat.start_pt=find_tpt(dat.plt_times(1),dat.times);
0712     dat.end_pt=find_tpt(dat.plt_times(2),dat.times);
0713     plotted_pts=dat.start_pt:dat.end_pt;
0714     plotted_times=dat.times(dat.start_pt:dat.end_pt);
0715     [dat.absmxA mx_tpt]=max(max(abs(dat.showingA(:,dat.start_pt:dat.end_pt))));
0716     stat_mx=max(max(dat.showingA(:,dat.start_pt:dat.end_pt)));
0717     stat_mn=min(min(dat.showingA(:,dat.start_pt:dat.end_pt)));
0718     stat_rng=stat_mx-stat_mn;
0719     stat_plt_rng=[stat_mn-stat_rng*.02 stat_mx+stat_rng*.02];
0720     stat_plt_rng=round(stat_plt_rng*100)/100;
0721     plot([p.Results.GNDorGRP.time_pts(1) p.Results.GNDorGRP.time_pts(end)],[0 0],'k'); % uV/t=0 line
0722     hold on;
0723     dat.h_t0lineA=plot([0 0],stat_plt_rng,'k'); % time=0 line
0724     set(dat.h_timeA,'ygrid','on');
0725     if ydir<0,
0726        set(dat.h_timeA,'ydir','reverse'); 
0727     end
0728     if size(dat.showingA,1)==1,
0729         %only one channel being plot
0730         dat.h_showingA=plot(dat.times,dat.showingA);
0731     else
0732         dat.h_showingA=plot(dat.times,dat.showingA');
0733     end
0734     axis tight;
0735     axis([plotted_times(1) plotted_times(end) stat_plt_rng]);
0736     vA=axis;
0737     dat.h_lineA=plot([1 1]*plotted_times(mx_tpt),vA(3:4),'k');
0738     set(dat.h_lineA,'linewidth',2);
0739     
0740     % Dashed vertical lines marking test window
0741     dat.n_wind=size(test_wind,1);
0742     if dat.n_wind        
0743         for nw=1:dat.n_wind,
0744             dat.h_wind1A(nw)=plot([1 1]*test_wind(nw,1),vA(3:4),'k--');
0745             set(dat.h_wind1A(nw),'linewidth',2);
0746             dat.h_wind2A(nw)=plot([1 1]*test_wind(nw,2),vA(3:4),'k--');
0747             set(dat.h_wind2A(nw),'linewidth',2);
0748         end
0749     else
0750         dat.h_wind1A=[];
0751         dat.h_wind2A=[];
0752     end
0753     
0754     %y-axis
0755     h=ylabel('\muV (ERP)');
0756     dat.h_time_ylabA=h;
0757     set(h,'fontsize',10,'fontunits','normalized');
0758     
0759     %Title
0760     new_title=['Bin ' int2str(bin) ': ' dat.bindesc{bin}];
0761     title_max_char=43; %prevents title from spilling over into topography axis
0762     if length(new_title)>title_max_char,
0763         new_title=new_title(1:title_max_char);
0764     end 
0765     dat.h_time_title=title(new_title);
0766     set(dat.h_time_title,'fontsize',10,'fontunits','normalized');
0767     bdf_code = [ 'tmppos = get(gca, ''currentpoint'');' ...
0768         'dat=get(gcbf, ''userdata'');' ...
0769         'new_tpt=find_tpt(tmppos(1,1),dat.times);' ...
0770         'set(dat.h_lineA,''XData'',[1 1]*dat.times(new_tpt));' ...
0771         'set(dat.h_lineB,''XData'',[1 1]*dat.times(new_tpt));' ...
0772         'set(dat.h_topo_time,''string'',num2str(dat.times(new_tpt)));' ...
0773         'set(dat.fig_id,''userdata'',dat);' ...
0774         'gui_erp(''redraw topo'');' ...
0775         'drawnow;' ...
0776         'clear latpoint dattmp tmppos;' ...
0777         ];
0778     set(dat.h_timeA,'ButtonDownFcn',bdf_code);
0779     set(dat.h_showingA,'ButtonDownFcn',bdf_code);
0780 
0781     
0782     %
0783     %%%%%%%%%%%%%%%%%%%%%% AXIS B: Time x t-score/stderr/GFP Axes %%%%%%%%%%%%%%%%%%%%%%%%
0784     %
0785     
0786     frm_col=[1 1 1]*.702; %background color (gray) differs from EEGLAB blue background color
0787     uipanel(dat.fig_id,...
0788         'Units','normalized', ...
0789         'Position',[ 0 0.175 .72 0.42 ],...
0790         'shadowcolor','k', ...
0791         'highlightcolor',frm_col, ...
0792         'foregroundcolor',frm_col, ...
0793         'backgroundcolor',frm_col);
0794    
0795     dat.h_timeB=axes('position',[0.1 .25 .6 .33]); 
0796     dat.absmxB=max(max(abs(dat.showingB(:,dat.start_pt:dat.end_pt))));
0797     stat_mx=max(max(dat.showingB(:,dat.start_pt:dat.end_pt)));
0798     stat_mn=min(min(dat.showingB(:,dat.start_pt:dat.end_pt)));
0799     stat_rng=stat_mx-stat_mn;
0800     stat_plt_rng=[stat_mn-stat_rng*.02 stat_mx+stat_rng*.02];
0801     stat_plt_rng=round(stat_plt_rng*100)/100;
0802     plot([p.Results.GNDorGRP.time_pts(1) p.Results.GNDorGRP.time_pts(end)],[0 0],'k'); % uV/t=0 line
0803     hold on;
0804     dat.h_t0lineB=plot([0 0],stat_plt_rng,'k'); % time=0 line
0805     set(dat.h_timeB,'ygrid','on');
0806     if ydir<0,
0807        set(dat.h_timeB,'ydir','reverse'); 
0808     end
0809     if size(dat.showingB,1)==1,
0810         %only one channel being plot
0811         dat.h_showingB=plot(dat.times,dat.showingB);
0812     else
0813         dat.h_showingB=plot(dat.times,dat.showingB');
0814     end
0815     axis tight;
0816     axis([plotted_times(1) plotted_times(end) stat_plt_rng]);
0817     vB=axis;
0818     dat.h_lineB=plot([1 1]*plotted_times(mx_tpt),vB(3:4),'k');
0819     set(dat.h_lineB,'linewidth',2);
0820     
0821     % Dashed vertical lines marking test window
0822     if dat.n_wind        
0823         for nw=1:dat.n_wind,
0824             dat.h_wind1B(nw)=plot([1 1]*test_wind(nw,1),vB(3:4),'k--');
0825             set(dat.h_wind1B(nw),'linewidth',2);
0826             dat.h_wind2B(nw)=plot([1 1]*test_wind(nw,2),vB(3:4),'k--');
0827             set(dat.h_wind2B(nw),'linewidth',2);
0828         end
0829     else
0830         dat.h_wind1B=[];
0831         dat.h_wind2B=[];
0832     end
0833     
0834     % Dashed lines marking critical t-score(s)
0835     dat.h_crit1=[];
0836     dat.h_crit2=[];
0837     dat.h_alph1=[];
0838     dat.h_alph2=[];
0839     if ~isempty(critical_t) && ~isempty(test_wind),
0840         if strcmpi(p.Results.stat,'t'),
0841             if isempty(dat.alpha),
0842                 watchit('You did not specify an alpha level via optional input argument ''alpha''.  Alpha level will not be displayed.');
0843             else
0844                 for nw=1:dat.n_wind,
0845                     dat.h_crit1(nw)=plot(test_wind(nw,:),[1 1]*critical_t(1),'r--');
0846                     set(dat.h_crit1(nw),'linewidth',3);
0847                     if nw==1,
0848                         tm_rng=double(p.Results.GNDorGRP.time_pts(end)-p.Results.GNDorGRP.time_pts(1));
0849                         if strcmpi(dat.mltplcmp_crct,'fdr')
0850                             dat.h_alph1=text(test_wind(nw,1)-tm_rng*.02,critical_t(1), ...
0851                                 ['q=' num2str(rnd_orderofmag(dat.alpha))]);
0852                         else
0853                             dat.h_alph1=text(test_wind(nw,1)-tm_rng*.02,critical_t(1), ...
0854                                 ['\alpha=' num2str(rnd_orderofmag(dat.alpha))]);
0855                         end
0856                         set(dat.h_alph1,'color','r','fontweight','normal','fontsize',12, ...
0857                             'horizontalalignment','right','backgroundcolor',[1 1 1], ...
0858                             'edgecolor',[1 1 1]*.3,'clipping','on','fontname','fixedwidth');
0859                     end
0860                 end
0861             end
0862             if length(critical_t)>1,
0863                 for nw=1:dat.n_wind,
0864                     dat.h_crit2(nw)=plot(test_wind(nw,:),[1 1]*critical_t(2),'r--');
0865                     set(dat.h_crit2(nw),'linewidth',3);
0866                     if nw==1,
0867                         if strcmpi(dat.mltplcmp_crct,'fdr')
0868                             dat.h_alph2=text(test_wind(nw,1)-tm_rng*.02,critical_t(2), ...
0869                                 ['q=' num2str(rnd_orderofmag(dat.alpha))]);
0870                         else
0871                             dat.h_alph2=text(test_wind(nw,1)-tm_rng*.02,critical_t(2), ...
0872                                 ['\alpha=' num2str(rnd_orderofmag(dat.alpha))]);
0873                         end
0874                         set(dat.h_alph2,'color','r','fontweight','normal','fontsize',12, ...
0875                             'horizontalalignment','right','backgroundcolor',[1 1 1], ...
0876                             'edgecolor',[1 1 1]*.3,'clipping','on','fontname','fixedwidth');
0877                     end
0878                 end
0879             end
0880         end
0881     end
0882     %x-axis label
0883     h=xlabel('Time (msec)');
0884     set(h,'fontsize',10,'fontunits','normalized');
0885 
0886     %y-axis label
0887     if strcmpi(p.Results.stat,'gfp') || isinf(dat.t_scores(1,1,bin)) || isnan(dat.t_scores(1,1,bin)),
0888         h=ylabel('\muV (GFP)');
0889     elseif strcmpi(p.Results.stat,'t'),
0890         h=ylabel('t-score');
0891     else
0892         h=ylabel('\muV (StdEr)');
0893     end
0894     dat.h_time_ylabB=h;
0895     set(h,'fontsize',10,'fontunits','normalized');
0896     
0897     bdf_code = [ 'tmppos = get(gca, ''currentpoint'');' ...
0898         'dat=get(gcbf, ''userdata'');' ...
0899         'new_tpt=find_tpt(tmppos(1,1),dat.times);' ...
0900         'set(dat.h_lineA,''XData'',[1 1]*dat.times(new_tpt));' ...
0901         'set(dat.h_lineB,''XData'',[1 1]*dat.times(new_tpt));' ...
0902         'set(dat.h_topo_time,''string'',num2str(dat.times(new_tpt)));' ...
0903         'set(dat.fig_id,''userdata'',dat);' ...
0904         'gui_erp(''redraw topo'');' ...
0905         'drawnow;' ...
0906         'clear latpoint dattmp tmppos;' ...
0907         ];
0908     set(dat.h_timeB,'ButtonDownFcn',bdf_code);
0909     set(dat.h_showingB,'ButtonDownFcn',bdf_code);
0910 
0911     %
0912     %%%%%%%%%%%%%%%%%%%%%% AXIS C: ERP Topography %%%%%%%%%%%%%%%%%%%%%%%%
0913     %
0914 
0915     % PANEL
0916     uipanel(dat.fig_id,...
0917         'Units','normalized', ...
0918         'Position',[ 0.719 0.59 0.281 0.42 ],...
0919         'shadowcolor','k', ...
0920         'highlightcolor',frm_col, ...
0921         'foregroundcolor',frm_col, ...
0922         'backgroundcolor',frm_col);   
0923     
0924     % TOPOGRAPHY
0925     dat.h_topoA=axes('position',[0.705 .68 .31 .24],'box','off'); 
0926   
0927     sig_chans=[];
0928     if ~isempty(test_wind) && ~isempty(critical_t),
0929         %if current time point is in a test window look for channels with
0930         %sig effects
0931         for nw=1:dat.n_wind,
0932             if (dat.times(plotted_pts(mx_tpt))>=test_wind(nw,1)) && (dat.times(plotted_pts(mx_tpt))<=test_wind(nw,2)),
0933                 if isnan(critical_t)
0934                     %cluster based test
0935                     mx_tpt_in_ms=plotted_times(mx_tpt);
0936                     mx_tpt_epoch_id=find(dat.times==mx_tpt_in_ms);
0937                     use_test_id=find(dat.psbl_tests==p.Results.t_test);
0938                     pval_tpt_id=find(dat.t_tests(use_test_id).used_tpt_ids==mx_tpt_epoch_id);
0939                     if ~isempty(pval_tpt_id)
0940                         sig_chans_temp=find(dat.t_tests(use_test_id).adj_pval(:,pval_tpt_id)<dat.t_tests(use_test_id).desired_alphaORq);
0941                         sig_chans_temp=dat.t_tests(use_test_id).used_chan_ids(sig_chans_temp); %convert sig channel indices into channel indices in the original GND/GRP variable
0942                         n_showing_chans=length(dat.showing_chans);
0943                         sig_chans=zeros(1,n_showing_chans);
0944                         for a=1:n_showing_chans,
0945                             if ismember(dat.showing_chans(a),sig_chans_temp)
0946                                 sig_chans(a)=1;
0947                             end
0948                         end
0949                         sig_chans=find(sig_chans);
0950                     else
0951                         sig_chans=[];
0952                     end
0953                 else
0954                     if length(critical_t)==2,
0955                         sig_chans=find(dat.showing_t(:,plotted_pts(mx_tpt))>max(critical_t));
0956                         sig_chans=[sig_chans; find(dat.showing_t(:,plotted_pts(mx_tpt))<min(critical_t))];
0957                     else
0958                         if critical_t>0,
0959                             sig_chans=find(dat.showing_t(:,plotted_pts(mx_tpt))>critical_t);
0960                         else
0961                             sig_chans=find(dat.showing_t(:,plotted_pts(mx_tpt))<critical_t);
0962                         end
0963                     end
0964                 end
0965                 break; %Visualized time point is in this window. Break out of for loop since there's no need to look at additional time windows
0966             end
0967         end
0968     end
0969     cbar_title_fontsize=14;
0970     if size(dat.showingA,1)<=2,
0971         %two or fewer channels, we can only plot electrode locations
0972         topoplotMK(dat.showingA(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
0973             'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
0974         cbar_title='Not Applicable';
0975         cbar_title_fontsize=10; %for some reason a fontsize of 14 cuts off last later for topoB axis
0976     else
0977         topoplotMK(dat.showingA(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
0978             'maplimits',[-1 1]*dat.absmxA,'emarker2',{sig_chans,'o',[1 1 1],4});
0979         set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702);
0980         cbar_title='\muV';
0981     end
0982 
0983     
0984     % COLOR BAR
0985     dat.h_cbarA=axes('position',[0.76 .945 .2 .015]);
0986     cbar(dat.h_cbarA);
0987     absmx=round(dat.absmxA*100)/100;
0988     set(gca,'xticklabel',[-absmx 0 absmx]);
0989     h_cbar_title=title(cbar_title);
0990     set(h_cbar_title,'fontsize',cbar_title_fontsize);
0991     
0992     
0993     %%% TIME POINT TEXT BOX
0994     %STATIC TEXT LABEL
0995     uicontrol(dat.fig_id,...
0996         'Units','normalized', ...
0997         'Position',[ 0.905 0.62 0.07 0.04 ],...
0998         'String','msec',...
0999         'fontsize',14, ...
1000         'fontunits','normalized', ...
1001         'Style','text');
1002     dat.h_topo_time=uicontrol(dat.fig_id,...
1003         'CallBack','gui_erp(''time jump'');',...
1004         'Units','normalized', ...
1005         'Position',[ 0.800 0.62 0.109 0.04 ], ... %war 0.805 war 0.1
1006         'String',num2str(plotted_times(mx_tpt)), ...
1007         'Style','edit', ... 
1008         'Enable','on', ...
1009         'ToolTipString','Time point to visualize topographicaly.', ...
1010         'fontsize',14, ...
1011         'fontunits','normalized', ...
1012         'horizontalalignment','center', ...
1013         'BackGroundColor','w', ...
1014         'Tag','topo_time');
1015     
1016     
1017     %
1018     %%%%%%%%%%%%%%%%%%%%%% AXIS D: t-Score/Standard Error/GFP Topography %%%%%%%%%%%%%%%%%%%%%%%%
1019     %
1020     
1021     
1022     % PANEL
1023     uipanel(dat.fig_id,...
1024         'Units','normalized', ...
1025         'Position',[ 0.719 0.175 0.281 0.42 ],...
1026         'shadowcolor','k', ...
1027         'highlightcolor',frm_col, ...
1028         'foregroundcolor',frm_col, ...
1029         'backgroundcolor',frm_col);
1030     
1031     % TOPOGRAPHY
1032     dat.h_topoB=axes('position',[0.705 .27 .31 .24],'box','off');
1033     
1034     cbar_title_fontsize=14;
1035     if strcmpi(p.Results.stat,'gfp') || isinf(dat.t_scores(1,1,bin)) || isnan(dat.t_scores(1,1,bin))
1036         topoplotMK([],dat.chanlocs(dat.showing_chans), ...
1037             'style','blank','plain_blank',1);
1038         cbar_title='Not Applicable';
1039         cbar_title_fontsize=10;
1040     else
1041         if size(dat.showingB,1)<=2,
1042             %two or fewer channels, we can only plot electrode locations
1043             topoplotMK(dat.showingB(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
1044                 'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
1045             cbar_title='Not Applicable';
1046             cbar_title_fontsize=10;
1047         else
1048             topoplotMK(dat.showingB(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
1049                 'maplimits',[-1 1]*dat.absmxB,'emarker2',{sig_chans,'o',[1 1 1],4});
1050             set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702);
1051             if strcmpi(p.Results.stat,'t'),
1052                 cbar_title='t-score';
1053             else
1054                 cbar_title='\muV'; %ERPs and stder are in units of uV
1055             end
1056         end
1057     end
1058     
1059     % COLOR BAR
1060     dat.h_cbarB=axes('position',[0.76 .535 .2 .015]);
1061     cbar(dat.h_cbarB);
1062     absmx=round(dat.absmxB*100)/100;
1063     set(gca,'xticklabel',[-absmx 0 absmx]);
1064     h_cbar_title=title(cbar_title);
1065     set(h_cbar_title,'fontsize',cbar_title_fontsize);
1066     
1067     % GO BACK 1 TIME POINT
1068     dat.h_back1=uicontrol(dat.fig_id,...
1069         'CallBack','gui_erp(''back one'');',...
1070         'Units','normalized', ...
1071         'Position',[ 0.80 0.232 0.055 0.04 ],...
1072         'String','<',...
1073         'fontsize',14, ...
1074         'fontunits','normalized', ...
1075         'Tag','back1', ...
1076         'ToolTipString','Show topography at preceding time point.', ...
1077         'Style','pushbutton');
1078     
1079     % GO FORWARD 1 TIME POINT
1080     dat.h_forward1=uicontrol(dat.fig_id,...
1081         'CallBack','gui_erp(''forward one'');',...
1082         'Units','normalized', ...
1083         'Position',[ 0.865 0.232 0.055 0.04 ],...
1084         'String','>', ...
1085         'fontsize',14, ...
1086         'fontunits','normalized', ...
1087         'Tag','forward1', ...
1088         'ToolTipString','Show topography at subsequent time point.', ...
1089         'Style','pushbutton');
1090     
1091     % ANIMATE BACKWARDS IN TIME
1092     dat.h_back=uicontrol(dat.fig_id,...
1093         'CallBack','gui_erp(''back'');',...
1094         'Units','normalized', ...
1095         'Position',[ 0.735 0.232 0.055 0.04 ],...
1096         'String','<<',...
1097         'fontsize',14, ...
1098         'fontunits','normalized', ...
1099         'Tag','back', ...
1100         'interruptible','on', ...
1101         'ToolTipString','Animate topography going backwards in time.', ...
1102         'Style','pushbutton');
1103     
1104     % ANIMATE FORWARDS IN TIME
1105     dat.h_forward=uicontrol(dat.fig_id,...
1106         'CallBack','gui_erp(''forward'');',...
1107         'Units','normalized', ...
1108         'Position',[ 0.93 0.232 0.055 0.04 ],...
1109         'String','>>',...
1110         'fontsize',14, ...
1111         'fontunits','normalized', ...
1112         'Tag','back', ...
1113         'interruptible','on', ...
1114         'ToolTipString','Animate topography going forwards in time.', ...
1115         'Style','pushbutton');
1116     
1117     % STOP ANIMATION
1118     dat.h_stop=uicontrol(dat.fig_id,...
1119         'CallBack','dat=get(gcbf,''userdata''); dat.interrupt=1; set(dat.fig_id,''userdata'',dat);',...
1120         'Units','normalized', ...
1121         'Position',[ 0.745 0.179 0.23 0.05 ],...
1122         'String','Stop Animation',...
1123         'fontsize',14, ...
1124         'fontunits','normalized', ...
1125         'Tag','stop', ...
1126         'enable','off', ...
1127         'ToolTipString','Stop topography animation.', ...
1128         'Style','pushbutton');
1129    
1130 
1131     
1132     %
1133     %%%%%%%%%%%%%%%%%%%%%% AXIS E: PLOTTING OPTIONS %%%%%%%%%%%%%%%%%%%%%%%%
1134     %
1135     
1136     % PANEL
1137     uipanel(dat.fig_id,...
1138         'Units','normalized', ...
1139         'Position',[ 0 0 1 0.178 ],...
1140         'shadowcolor','k', ...
1141         'highlightcolor',frm_col, ...
1142         'foregroundcolor',frm_col, ...
1143         'backgroundcolor',frm_col);
1144       
1145         
1146     %%%% WHICH BIN TO PLOT
1147     bdesc_str=cell(1,n_bin);
1148     for a=1:n_bin,
1149         bdesc_str{a}=sprintf('Bin %d: %s',a,dat.bindesc{a});
1150     end 
1151     % Text
1152     uicontrol(dat.fig_id,...
1153         'Units','normalized', ...
1154         'Position',[ 0.01 0.09 0.24 0.075 ],...
1155         'String','Current Bin',...
1156         'fontsize',12, ...
1157         'ToolTipString','Bin to plot in Time x ERP/t-score axis.', ...
1158         'Style','text');
1159     % Create bin menu
1160     dat.h_bin=uicontrol(dat.fig_id,...
1161         'CallBack','gui_erp(''change bin'');',...
1162         'Units','normalized', ...
1163         'Position',[ 0.015 0.095 0.23 0.04 ],...
1164         'fontsize',12, ...
1165         'String',bdesc_str, ...
1166         'Value',bin, ...
1167         'Style','popup', ...
1168         'ToolTipString','Bin to plot in Time x ERP/t-score axis.', ...
1169         'tag','bin');
1170     
1171     
1172     %%%% WHICH T-TESTS RESULTS TO PLOT
1173     ttest_str=cell(1,n_psbl_tests+1);
1174     for a=1:n_psbl_tests,
1175         ttest_str{a}=sprintf('Test %d: Bin %d, Times ',dat.psbl_tests(a),dat.t_tests(a).bin);
1176         n_ttst_wind=size(dat.t_tests(a).time_wind,1);
1177         for b=1:n_ttst_wind,
1178             ttest_str{a}=[ttest_str{a} int2str(dat.t_tests(a).time_wind(b,1)) '-' int2str(dat.t_tests(a).time_wind(b,2)) ','];
1179         end
1180         if length(dat.t_tests(a).crit_t)==2,
1181             ttest_str{a}=[ttest_str{a} ' Two-Tailed,'];
1182         elseif dat.t_tests(a).crit_t<0,
1183             ttest_str{a}=[ttest_str{a} ' Lower-Tailed,'];
1184         else
1185             ttest_str{a}=[ttest_str{a} ' Upper-Tailed,'];
1186         end
1187         
1188         if isnan(dat.t_tests(a).estimated_alpha)
1189             ttest_str{a}=[ttest_str{a} sprintf(' q=%.4f',dat.t_tests(a).desired_alphaORq)];
1190             ttest_str{a}=[ttest_str{a} sprintf(', method=%s',dat.t_tests(a).mult_comp_method)];
1191         else
1192             ttest_str{a}=[ttest_str{a} sprintf(' alpha=%.4f',dat.t_tests(a).estimated_alpha)];
1193         end
1194         
1195         if dat.t_tests(a).null_mean,
1196             ttest_str{a}=[ttest_str{a} sprintf(', Null Mean=%.4f',dat.t_tests(a).null_mean)];
1197         end
1198     end
1199     ttest_str{n_psbl_tests+1}='None/Manual';
1200     
1201     % Text
1202     uicontrol(dat.fig_id,...
1203         'Units','normalized', ...
1204         'Position',[ 0.255 0.09 0.24 0.075 ],...
1205         'String','Current Test Result',...
1206         'fontsize',12, ...
1207         'ToolTipString','Set of t-test results to visualize.', ...
1208         'Style','text');
1209     % Create sortvar browse button
1210     dat.h_ptest=uicontrol(dat.fig_id,...
1211         'CallBack','gui_erp(''change test'');',...
1212         'Units','normalized', ...
1213         'Position',[ 0.26 0.095 0.23 0.04 ],...
1214         'fontsize',12, ...
1215         'String',ttest_str, ...
1216         'Value',crnt_ttest, ...
1217         'Style','popup', ...
1218         'ToolTipString','Set of t-test results to visualize.', ...
1219         'tag','bin');
1220 
1221     %%% Time window in which hypotheses tests were done
1222     %Text
1223     uicontrol(dat.fig_id,...
1224         'Units','normalized', ...
1225         'Position',[ 0.505 0.09 0.26 0.075 ],... %hier das msste weter zum Boden reichen, damit es das andere Fenster inschliet
1226         'String','Test Window(s) [Min Max]:',...
1227         'fontsize',12, ...
1228         'Style','text');
1229     %Edit Box
1230     wind_edges=[];
1231     for nw=1:dat.n_wind,
1232         if nw==dat.n_wind,
1233             wind_edges=[wind_edges num2str(test_wind(nw,:))];
1234         else
1235             wind_edges=[wind_edges num2str(test_wind(nw,:)) '; ']; 
1236         end
1237     end
1238     dat.h_testwind=uicontrol(dat.fig_id,...
1239         'CallBack','gui_erp(''update dashed lines'');',...
1240         'Units','normalized', ...
1241         'fontsize',10, ...
1242         'BackGroundColor','w', ...
1243         'Position',[ 0.525 0.095 0.22 0.04 ],...
1244         'String',wind_edges, ...
1245         'Style','edit', ...
1246         'ToolTipString','Miniumum and maximum time of time window in which hypothesis tests were done.', ...
1247         'tag','timerange'); 
1248     
1249     
1250     %%% Critical t-score here
1251     %Text
1252     uicontrol(dat.fig_id,...
1253         'Units','normalized', ...
1254         'Position',[ 0.76 0.09 0.23 0.075 ],...
1255         'String','Critical t-score(s)',...
1256         'fontsize',12, ...
1257         'Style','text');
1258     %Edit Box
1259     dat.h_critval=uicontrol(dat.fig_id,...
1260         'CallBack','gui_erp(''update critical t'');',...
1261         'Units','normalized', ...
1262         'fontsize',10, ...
1263         'BackGroundColor','w', ...
1264         'Position',[ 0.79 0.095 0.19 0.04 ],...
1265         'String',num2str(critical_t), ...
1266         'Style','edit', ...
1267         'ToolTipString','Critical t-score(s) for reliable deviation from 0 voltage.', ...
1268         'tag','timerange');
1269     
1270     %%%% WHICH STAT TO PLOT
1271     stats={'t-score of ERPs','standard error of ERPs','global field power of ERPs'};
1272     % Text
1273     uicontrol(dat.fig_id,...
1274         'Units','normalized', ...
1275         'Position',[ 0.005 0.221 0.09 0.03 ],... % Hhe war 0.183
1276         'String','Statistic:',...
1277         'fontsize',12, ...
1278         'ToolTipString','Select what to plot in secondary waveform axis: ERP t-scores, standard error of the mean, or global field power.', ...
1279         'Style','text');
1280     
1281     if strcmpi(p.Results.stat,'gfp') || isinf(dat.t_scores(1,1,bin)) || isnan(dat.t_scores(1,1,bin))
1282         %isinf(dat.t_scores(1,1,bin)) will
1283         %equal 1 if there are insufficient subjects to calculate t-scores
1284         ini_val=3;
1285     elseif strcmpi(p.Results.stat,'t'),
1286         ini_val=1;
1287     elseif strcmpi(p.Results.stat,'stder'),
1288         ini_val=2;
1289     else
1290         error('Unrecognized value of input argument ''stat''.');
1291     end
1292     dat.h_stat=uicontrol(dat.fig_id,...
1293         'CallBack','gui_erp(''change stat'');',...
1294         'Units','normalized', ...
1295         'Position',[ 0.005 0.19 0.16 0.03 ],...
1296         'fontsize',12, ...
1297         'value',ini_val, ...
1298         'String',stats, ...
1299         'Style','popup', ...
1300         'ToolTipString','Plot ERP t-scores or standard error of the mean in secondary axis?', ...
1301         'tag','stat');
1302     
1303     %%% TIME RANGE
1304     %Text
1305     uicontrol(dat.fig_id,...
1306         'Units','normalized', ...
1307         'Position',[ 0.01 0.005 0.24 0.075 ],...
1308         'String','Time Range [Min Max]',...
1309         'fontsize',12, ...
1310         'Style','text');
1311     %Edit Box
1312     dat.h_timerange=uicontrol(dat.fig_id,...
1313         'CallBack','gui_erp(''new time limits'');',...
1314         'Units','normalized', ...
1315         'fontsize',10, ...
1316         'BackGroundColor','w', ...
1317         'Position',[ 0.025 0.007 0.21 0.04 ],...
1318         'String',num2str(dat.plt_times), ...
1319         'Style','edit', ...
1320         'ToolTipString','Miniumum and maximum time on Time x ERP axis.', ...
1321         'tag','timerange');   
1322     
1323     %%% ERP uV RANGE BOX (AXIS A)
1324     %Text
1325     uicontrol(dat.fig_id,...
1326         'Units','normalized', ...
1327         'Position',[ 0.26 0.005 0.26 0.075 ],...
1328         'String','ERP Range [Min Max]     ',...
1329         'HorizontalAlignment','center', ...
1330         'fontsize',12, ...
1331         'Style','text');
1332     %Edit Box
1333     dat.h_statrangeA=uicontrol(dat.fig_id,...
1334         'CallBack','gui_erp(''new statistic limitsA'');',...
1335         'Units','normalized', ...
1336         'fontsize',10, ...
1337         'BackGroundColor','w', ...
1338         'Position',[ 0.28 0.007 0.16 0.04 ],...
1339         'String',num2str(round(vA(3:4)*100)/100), ...
1340         'ToolTipString','Miniumum and maximum voltage on Time x ERP axis.', ...
1341         'Style','edit', ...
1342         'tag','statrange'); 
1343     
1344     %%% t-score/StdErr RANGE BOX (AXIS B)
1345     %Text
1346     uicontrol(dat.fig_id,...
1347         'Units','normalized', ...
1348         'Position',[ 0.50 0.005 0.26 0.075 ],...
1349         'String',' t/StdErr Range [Min Max]',...
1350         'HorizontalAlignment','center', ...
1351         'fontsize',12, ...
1352         'Style','text');
1353     %Edit Box
1354     dat.h_statrangeB=uicontrol(dat.fig_id,...
1355         'CallBack','gui_erp(''new statistic limitsB'');',...
1356         'Units','normalized', ...
1357         'fontsize',10, ...
1358         'BackGroundColor','w', ...
1359         'Position',[ 0.54 0.007 0.16 0.04 ],...
1360         'String',num2str(round(vB(3:4)*100)/100), ...
1361         'ToolTipString','Miniumum and maximum t-scores/voltage on Time x t-Scores/Standard Error/GFP axis.', ...
1362         'Style','edit', ...
1363         'tag','statrange');   
1364     
1365     % Button to close GUI
1366     uicontrol(dat.fig_id,...
1367         'CallBack','close(gcbf);',...
1368         'Units','normalized', ...
1369         'Position',[ 0.77 0.012 0.11 0.05 ],...
1370         'String','Close',...
1371         'fontsize',12, ...
1372         'Tag','close', ...
1373         'backgroundcolor','m', ...
1374         'ToolTipString','Close GUI', ...
1375         'Style','pushbutton');
1376     
1377     dat.help_msg=sprintf(['Hold mouse cursor over a GUI control for an explanation of what it does.\n\n', ...
1378         'Time x waveform axis visualizes ERPs, t-score of ERPs, standard error of ERPs, or ERP global field power simultaneously at multiple electrodes.  ' ... 
1379         'Each colored line corresponds to a different electrode.\n\nClick on Time x uV/t-score/stderr/GFP axis to visualize the scalp topography\n', ...
1380         'at that point in time.\n\nClick on electrodes in scalp topography to see electrode name.\n\n', ...
1381         'This GUI was produced by gui_erp.m']);
1382     
1383     % Button for help
1384     uicontrol(dat.fig_id,...
1385         'CallBack','dat=get(gcbf,''userdata''); helpdlg(dat.help_msg,''ERP GUI Help'');', ...
1386         'Units','normalized', ...
1387         'Position',[ 0.885 0.012 0.11 0.05 ],...
1388         'String','Help',...
1389         'fontsize',12, ...
1390         'Tag','help', ...
1391         'ToolTipString','Click for help', ...
1392         'Style','pushbutton');
1393     dat.interrupt=1;
1394     set(dat.fig_id,'userdata',dat);
1395 elseif strcmpi(cmnd_str,'time jump'),
1396     new_tpt=find_tpt(str2num(get(dat.h_topo_time,'string')), ...
1397         dat.times);
1398     if new_tpt<dat.start_pt,
1399         errordlg('That time is too early.');
1400     elseif new_tpt>dat.end_pt,
1401         errordlg('That time is too late.');
1402     else
1403         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1404         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1405         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1406         set(dat.fig_id,'userdata',dat);
1407         redraw_topos(dat,0);
1408         drawnow;
1409     end    
1410 elseif strcmpi(cmnd_str,'back one'),
1411     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1412         dat.times);
1413     new_tpt=current_tpt-1;
1414     if new_tpt<dat.start_pt,
1415         errordlg('Can''t go back any further.');
1416     else
1417         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1418         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1419         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1420         set(dat.fig_id,'userdata',dat);
1421         redraw_topos(dat,0);
1422         drawnow;
1423     end
1424 elseif strcmpi(cmnd_str,'forward one'),
1425     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1426         dat.times);
1427     new_tpt=current_tpt+1;
1428     if new_tpt>dat.end_pt,
1429         errordlg('Can''t go forward any further.');
1430     else
1431         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1432         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1433         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1434         set(dat.fig_id,'userdata',dat);
1435         redraw_topos(dat,0);
1436         drawnow;
1437     end
1438 elseif strcmpi(cmnd_str,'back'),
1439     interrupt=0;
1440     dat.interrupt=0;
1441     set(dat.h_stop,'enable','on');
1442     set(dat.h_forward,'enable','off');
1443     set(dat.h_back,'enable','off');
1444     set(dat.h_forward1,'enable','off');
1445     set(dat.h_back1,'enable','off');
1446     set(dat.fig_id,'userdata',dat);
1447     drawnow;
1448     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1449         dat.times);
1450     new_tpt=current_tpt-1;
1451     loop_ct=0;
1452     while (new_tpt>=dat.start_pt) && (interrupt~=1),
1453         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1454         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1455         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1456         drawnow('update');
1457         redraw_topos(dat,0);
1458         drawnow;
1459         new_tpt=new_tpt-1;
1460         loop_ct=loop_ct+1;
1461         if loop_ct==5,
1462             loop_ct=0;
1463             new_tmp=get(dat.fig_id,'userdata');
1464             interrupt=new_tmp.interrupt;
1465         end
1466     end
1467     drawnow;
1468     dat.interrupt=0;
1469     new_tpt=new_tpt+1; %undo step that broke loop
1470     set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1471     set(dat.h_stop,'enable','off');
1472     set(dat.h_forward,'enable','on');
1473     set(dat.h_back,'enable','on');
1474     set(dat.h_forward1,'enable','on');
1475     set(dat.h_back1,'enable','on');
1476     set(dat.fig_id,'userdata',dat);
1477 elseif strcmpi(cmnd_str,'forward'),
1478     interrupt=0;
1479     dat.interrupt=0;
1480     set(dat.h_stop,'enable','on');
1481     set(dat.h_forward,'enable','off');
1482     set(dat.h_back,'enable','off');
1483     set(dat.h_forward1,'enable','off');
1484     set(dat.h_back1,'enable','off');
1485     set(dat.fig_id,'userdata',dat);
1486     drawnow;
1487     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1488         dat.times);
1489     new_tpt=current_tpt+1;
1490     loop_ct=0;
1491     while (new_tpt<=dat.end_pt) && (interrupt~=1),
1492         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1493         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1494         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1495         drawnow('update');
1496         redraw_topos(dat,0);
1497         drawnow;
1498         new_tpt=new_tpt+1;
1499         loop_ct=loop_ct+1;
1500         if loop_ct==5,
1501             loop_ct=0;
1502             new_tmp=get(dat.fig_id,'userdata');
1503             interrupt=new_tmp.interrupt;
1504         end
1505     end
1506     drawnow;
1507     dat.interrupt=0;
1508     new_tpt=new_tpt-1; %undo step that broke loop
1509     set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1510     set(dat.h_stop,'enable','off');
1511     set(dat.h_forward,'enable','on');
1512     set(dat.h_back,'enable','on');
1513     set(dat.h_forward1,'enable','on');
1514     set(dat.h_back1,'enable','on');
1515     set(dat.fig_id,'userdata',dat);
1516 elseif strcmpi(cmnd_str,'change stat'),
1517     stat=get(dat.h_stat,'value');
1518     current_bin=get(dat.h_bin,'value');
1519     if (stat~=3),
1520         %make sure there are sufficients subjects for that bin
1521         if isinf(dat.t_scores(1,1,current_bin)) || isnan(dat.t_scores(1,1,current_bin)) 
1522             stat=3;
1523             set(dat.h_stat,'value',stat);
1524             if (stat==2)
1525                 warndlg('Only one participant contributed to this bin. Thus the grand average standard error cannot be esimated.','gui_erp Warning');
1526             else
1527                 warndlg('Only one participant contributed to this bin. Thus the grand average t-scores cannot be derived.','gui_erp Warning');
1528             end
1529         end
1530     end
1531         
1532     if (stat==1),
1533         % t-scores
1534         if ~isempty(dat.h_crit1),
1535             delete(dat.h_crit1);
1536         end
1537         if ~isempty(dat.h_crit2),
1538             delete(dat.h_crit2);
1539         end
1540         dat.h_crit1=[];
1541         dat.h_crit2=[];
1542         
1543         if ~isempty(dat.h_alph1),
1544             delete(dat.h_alph1);
1545         end
1546         if ~isempty(dat.h_alph2),
1547             delete(dat.h_alph2);
1548         end
1549         dat.h_alph1=[];
1550         dat.h_alph2=[];
1551     end
1552     
1553     %redraw time plot
1554     draw_waveforms(dat,0);
1555     gui_erp('update dashed lines'); %also redraws topo
1556 elseif strcmpi(cmnd_str,'change bin'),
1557     new_bin=get(dat.h_bin,'value');
1558     crnt_ttest_id=get(dat.h_ptest,'value');
1559     n_t_tests=length(dat.t_tests);
1560     if (crnt_ttest_id==(n_t_tests+1)) || (dat.t_tests(crnt_ttest_id).bin~=new_bin),
1561         % If currently selected set of t-tests is for a different bin
1562         % than is currently visualized search for a t-test set on
1563         % this bin. Use the first test if more than one test has
1564         % been performed on the bin.  If no test is
1565         % found, set to None/Manual.
1566         crnt_ttest_id=n_t_tests+1;
1567         for a=1:n_t_tests,
1568             if dat.t_tests(a).bin==new_bin;
1569                 crnt_ttest_id=a;
1570                 break;
1571             end
1572         end
1573         set(dat.h_ptest,'value',crnt_ttest_id);
1574         dat=update_test(dat);
1575     end
1576     draw_waveforms(dat,1);
1577     gui_erp('update dashed lines'); %also redraws topo plot
1578 elseif strcmpi(cmnd_str,'new time limits'),
1579     %time limits
1580     plotted_times=dat.times(dat.start_pt:dat.end_pt);
1581     tme_lim=str2num(get(dat.h_timerange,'string'));
1582     if length(tme_lim)~=2,
1583         errordlg('Enter exactly two values for time range (desired Min and Max values).','gui_erp Error');
1584         set(dat.h_timerange,'string',num2str([plotted_times(1) plotted_times(end)])); %reset time limits
1585     elseif tme_lim(2)<tme_lim(1),
1586         errordlg('The second time range value must be greater than the first (enter desired Min then Max).','gui_erp Error');
1587         set(dat.h_timerange,'string',num2str([plotted_times(1) plotted_times(end)])); %reset time limits
1588     elseif (tme_lim(1)<dat.times(1)) || (tme_lim(2)>dat.times(end))
1589         errordlg(sprintf('Time range values must be between %d and %d msec.',dat.times(1),dat.times(end)),'gui_erp Error');
1590         set(dat.h_timerange,'string',num2str([plotted_times(1) plotted_times(end)])); %reset time limits
1591     else
1592         %change time limits
1593         set(dat.h_timeA,'xlim',tme_lim);
1594         set(dat.h_timeB,'xlim',tme_lim);
1595         dat.start_pt=find_tpt(tme_lim(1),dat.times);
1596         dat.end_pt=find_tpt(tme_lim(2),dat.times);
1597         
1598         %update max abs val
1599         dat.absmxA=max(max(abs(dat.showingA(:,dat.start_pt:dat.end_pt))));
1600         dat.absmxB=max(max(abs(dat.showingB(:,dat.start_pt:dat.end_pt))));
1601         current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1602             dat.times);
1603         if (current_tpt>dat.end_pt) || (current_tpt<dat.start_pt)
1604            % Time point whose topography is currently being visualized
1605            % falls outside of time range.  Reset current time point to peak
1606            % t-score latency within visualized time window.
1607            [dummy, mx_tpt]=max(max(abs(dat.showing_t(:,dat.start_pt:dat.end_pt))));
1608            showing_pts=dat.start_pt:dat.end_pt;
1609            set(dat.h_lineA,'xdata',[1 1]*dat.times(showing_pts(mx_tpt)));
1610            set(dat.h_lineB,'xdata',[1 1]*dat.times(showing_pts(mx_tpt)));
1611            
1612            set(dat.h_topo_time,'string',num2str(dat.times(showing_pts(mx_tpt))));
1613         end
1614         
1615         redraw_topos(dat,1);
1616         set(dat.fig_id,'userdata',dat);
1617     end
1618 elseif strcmpi(cmnd_str,'new statistic limitsA'),
1619     %statistic limits
1620     plotted_limits=get(dat.h_timeA,'ylim');
1621     new_lim=str2num(get(dat.h_statrangeA,'string'));
1622     if length(new_lim)~=2,
1623         errordlg('Enter exactly two values for statistic range (desired Min and Max values).','gui_erp Error');
1624         set(dat.h_statrangeA,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1625     elseif new_lim(2)<new_lim(1),
1626         errordlg('The second statistic range value must be greater than the first (enter desired Min then Max).','gui_erp Error');
1627         set(dat.h_statrangeA,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1628     else
1629         %change time line limits
1630         set(dat.h_timeA,'ylim',new_lim);
1631         set(dat.h_t0lineA,'YData',new_lim); %adjust time=0 line
1632         set(dat.h_lineA,'YData',new_lim); %adjust current time pt line
1633         
1634         %change dashed time line limits (hypothesis test window)
1635         if ~isempty(dat.h_wind1A)
1636            set(dat.h_wind1A,'YData',new_lim); 
1637            set(dat.h_wind2A,'YData',new_lim);
1638         end
1639     end
1640 elseif strcmpi(cmnd_str,'new statistic limitsB'),
1641     %statistic limits
1642     plotted_limits=get(dat.h_timeB,'ylim');
1643     new_lim=str2num(get(dat.h_statrangeB,'string'));
1644     if length(new_lim)~=2,
1645         errordlg('Enter exactly two values for statistic range (desired Min and Max values).','gui_erp Error');
1646         set(dat.h_statrangeB,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1647     elseif new_lim(2)<new_lim(1),
1648         errordlg('The second statistic range value must be greater than the first (enter desired Min then Max).','gui_erp Error');
1649         set(dat.h_statrangeB,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1650     else
1651         %change time line limits
1652         set(dat.h_timeB,'ylim',new_lim);
1653         set(dat.h_t0lineB,'YData',new_lim); %adjust time=0 line
1654         set(dat.h_lineB,'YData',new_lim); %adjust current time pt line
1655         
1656         %change dashed time line limits (hypothesis test window)
1657         if ~isempty(dat.h_wind1B)
1658             set(dat.h_wind1B,'YData',new_lim);
1659             set(dat.h_wind2B,'YData',new_lim);
1660         end
1661     end
1662 elseif strcmpi(cmnd_str,'redraw topo'),
1663     redraw_topos(dat,1); %we need this command string for buttdownfcn calls
1664 elseif strcmpi(cmnd_str,'update dashed lines'),
1665     test_wind=str2num(get(dat.h_testwind,'string'));
1666     
1667     if (size(test_wind,2)>2) || (isempty(test_wind) && ~isempty(get(dat.h_testwind,'string')))
1668         errordlg('Enter pairs of values for time window range (desired Min and Max values) or leave box blank. Separate multiple pairs with semicolons.', ...
1669             'gui_erp Error');
1670     elseif length(dat.critical_t)>2,
1671         errordlg('Enter one (for one-tailed test) or two (for two-tailed test) value(s) for critical t-scores or leave box blank.', ...
1672             'gui_erp Error');
1673     else
1674         if isempty(test_wind),
1675             test_wind=[NaN NaN];
1676         end
1677         
1678         % remove existing critical value markers & test time window markers
1679         if ~isempty(dat.h_crit1),
1680             delete(dat.h_crit1);
1681         end
1682         if ~isempty(dat.h_crit2),
1683             delete(dat.h_crit2);
1684         end
1685         if ~isempty(dat.h_alph1),
1686             delete(dat.h_alph1);
1687         end
1688         if ~isempty(dat.h_alph2),
1689             delete(dat.h_alph2);
1690         end
1691         if ~isempty(dat.h_wind1A),
1692             delete(dat.h_wind1A);
1693         end
1694         if ~isempty(dat.h_wind2A),
1695             delete(dat.h_wind2A);
1696         end
1697         if ~isempty(dat.h_wind1B),
1698             delete(dat.h_wind1B);
1699         end
1700         if ~isempty(dat.h_wind2B),
1701             delete(dat.h_wind2B);
1702         end
1703         dat.h_wind1A=[];
1704         dat.h_wind2A=[];
1705         dat.h_wind1B=[];
1706         dat.h_wind2B=[];
1707         dat.h_crit1=[];
1708         dat.h_crit2=[];
1709         dat.h_alph1=[];
1710         dat.h_alph2=[];
1711         dat.n_wind=size(test_wind,1);
1712         
1713         if ~sum(isnan(test_wind))
1714             y_rngA=get(dat.h_timeA,'YLim');
1715             y_rngB=get(dat.h_timeB,'YLim');
1716             
1717             axes(dat.h_timeA); %warning message says that I should give axes an output to speed it up, but axes doesn't produce an output
1718             for nw=1:dat.n_wind,
1719                 dat.h_wind1A(nw)=plot([1 1]*test_wind(nw,1),y_rngA,'k--');
1720                 set(dat.h_wind1A(nw),'linewidth',2);
1721                 dat.h_wind2A(nw)=plot([1 1]*test_wind(nw,2),y_rngA,'k--');
1722                 set(dat.h_wind2A(nw),'linewidth',2);
1723             end
1724             
1725             axes(dat.h_timeB);
1726             for nw=1:dat.n_wind,
1727                 dat.h_wind1B(nw)=plot([1 1]*test_wind(nw,1),y_rngB,'k--');
1728                 set(dat.h_wind1B(nw),'linewidth',2);
1729                 dat.h_wind2B(nw)=plot([1 1]*test_wind(nw,2),y_rngB,'k--');
1730                 set(dat.h_wind2B(nw),'linewidth',2);
1731             end
1732             
1733             %Find out if visualized units are microvolts or t-scores
1734             stat=get(dat.h_stat,'value');
1735             
1736             % if stat=1, t-scores, else data are in microvolts (for stderr)
1737             if ~sum(isnan(dat.critical_t)) && (stat==1) && ~isempty(dat.critical_t)
1738                 for nw=1:dat.n_wind,
1739                     dat.h_crit1(nw)=plot(test_wind(nw,:),[1 1]*dat.critical_t(1),'r--');
1740                     set(dat.h_crit1(nw),'linewidth',3);
1741                     if nw==1,
1742                         %write alpha value on figure
1743                         tm_rng=double(dat.times(end)-dat.times(1));
1744                         if strcmpi(dat.mltplcmp_crct,'fdr')
1745                             dat.h_alph1=text(test_wind(nw,1)-tm_rng*.02,dat.critical_t(1), ...
1746                                 ['q=' num2str(rnd_orderofmag(dat.alpha))]);
1747                         else
1748                             dat.h_alph1=text(test_wind(nw,1)-tm_rng*.02,dat.critical_t(1), ...
1749                                 ['\alpha=' num2str(rnd_orderofmag(dat.alpha))]);
1750                         end
1751                         set(dat.h_alph1,'color','r','fontweight','normal','fontsize',12, ...
1752                             'horizontalalignment','right','backgroundcolor',[1 1 1], ...
1753                             'edgecolor',[1 1 1]*.3,'clipping','on','fontname','fixedwidth');
1754                     end
1755                     if length(dat.critical_t)>1,
1756                         dat.h_crit2(nw)=plot(test_wind(nw,:),[1 1]*dat.critical_t(2),'r--');
1757                         set(dat.h_crit2(nw),'linewidth',3);
1758                         if nw==1,
1759                             %write alpha value on figure
1760                             if strcmpi(dat.mltplcmp_crct,'fdr')
1761                                 dat.h_alph2=text(test_wind(1)-tm_rng*.02,dat.critical_t(2), ...
1762                                     ['q=' num2str(rnd_orderofmag(dat.alpha))]);
1763                             else
1764                                 dat.h_alph2=text(test_wind(1)-tm_rng*.02,dat.critical_t(2), ...
1765                                     ['\alpha=' num2str(rnd_orderofmag(dat.alpha))]);
1766                             end
1767                             set(dat.h_alph2,'color','r','fontweight','normal','fontsize',12, ...
1768                                 'horizontalalignment','right','backgroundcolor',[1 1 1], ...
1769                                 'edgecolor',[1 1 1]*.3,'clipping','on','fontname','fixedwidth');
1770                         end
1771                     end
1772                 end
1773             end
1774         end
1775         set(dat.fig_id,'userdata',dat);
1776         redraw_topos(dat,1);
1777     end
1778 elseif strcmpi(cmnd_str,'update critical t'),
1779     critical_t=str2num(get(dat.h_critval,'string'));
1780     if ~isequal(sort(critical_t),sort(dat.critical_t)),
1781         new_alpha=[];
1782         while isempty(new_alpha),
1783             new_alpha=inputdlg({'Enter alpha level for new critical t-score(s)'}, ...
1784                 'Set New Alpha Level',1,{num2str(dat.alpha)});
1785             new_alpha=str2num(new_alpha{1});
1786             if isempty(new_alpha),
1787                 errrodlg('You must enter a numeric value for the new alpha level','gui_erp Error');
1788             end
1789         end
1790         dat.alpha=new_alpha;
1791         dat.critical_t=critical_t;
1792     end
1793     set(dat.fig_id,'userdata',dat);
1794     gui_erp('update dashed lines');
1795 elseif strcmpi(cmnd_str,'change test'),
1796     [dat, redraw_wform]=update_test(dat);    
1797     if redraw_wform,
1798         draw_waveforms(dat,1);
1799     end
1800     gui_erp('update dashed lines'); %also redraws topo
1801 else
1802     errordlg(sprintf('Command "%s" not recognized by gui_erp.m',cmnd_str), ...
1803         'gui_erp Error');
1804 end
1805 
1806 
1807 function draw_waveforms(dat,redrawA)
1808 % Redraws the channel waveforms (i.e., ERPs, t-scores of ERPs, standard error of ERPs, or GFP of ERPs)
1809 % and the color bar the topography.
1810 %
1811 % Used by: 'change bin', 'change stat', and 'change test'
1812 %
1813 % Input:
1814 %   dat     - The userdata of the GUI
1815 %   redrawA - If non-zero, axis A (the ERP waveformss) will also be redrawn
1816 
1817 new_bin=get(dat.h_bin,'value');
1818 stat=get(dat.h_stat,'value');
1819 crnt_ttest_id=get(dat.h_ptest,'value');
1820 n_t_tests=length(dat.t_tests);
1821 if crnt_ttest_id==n_t_tests+1,
1822     %no t-test results visualized
1823     dat.showing_chans=dat.loaded_chans;
1824 else
1825     dat.showing_chans=intersect(dat.t_tests(crnt_ttest_id).used_chan_ids, ...
1826         dat.loaded_chans);
1827 end
1828 
1829 %redraw Axis B time plot
1830 axes(dat.h_timeB);
1831 delete(dat.h_showingB);
1832 delete(dat.h_lineB);
1833 plotted_times=dat.times(dat.start_pt:dat.end_pt);
1834 %store t-scores for waveform that will be shown
1835 if (crnt_ttest_id<=n_t_tests) && dat.t_tests(crnt_ttest_id).null_mean
1836     %The mean of the null hypothesis is non-zero
1837     dat.showing_t=squeeze( (dat.erp(dat.showing_chans,:,new_bin)- ...
1838         dat.t_tests(crnt_ttest_id).null_mean)./dat.stder(dat.showing_chans,:,new_bin) );
1839 else
1840     dat.showing_t=squeeze(dat.t_scores(dat.showing_chans,:,new_bin));
1841 end
1842 
1843 if (stat==1),
1844     dat.showingB=dat.showing_t;
1845     cbar_title='t-score';
1846     ylab='t-score';
1847 elseif (stat==2),
1848     dat.showingB=squeeze(dat.stder(dat.showing_chans,:,new_bin));
1849     cbar_title='\muV';
1850     ylab='\muV (StdEr)';
1851 elseif (stat==3),
1852     dat.showingB=dat.gfp(:,new_bin)';
1853     cbar_title='\muV';
1854     ylab='\muV (GFP)';
1855 end
1856 if size(dat.showingB,1)==1,
1857     %only one channel being plot
1858     dat.h_showingB=plot(dat.times,dat.showingB);
1859     cbar_title='Not Applicable';
1860     cbar_title_fontsize=10;
1861 else
1862     dat.h_showingB=plot(dat.times,dat.showingB');
1863     cbar_title_fontsize=14;
1864 end
1865 set(dat.h_time_ylabB,'string',ylab);
1866 
1867 stat_mx=max(max(dat.showingB));
1868 stat_mn=min(min(dat.showingB));
1869 %Make sure new bin has data in it
1870 if isnan(stat_mx) || isnan(stat_mn)
1871    error('Bin %d does not appear to have any data in it.',new_bin); 
1872 end
1873 stat_rng=stat_mx-stat_mn;
1874 stat_plt_rng=[stat_mn-stat_rng*.02 stat_mx+stat_rng*.02];
1875 stat_plt_rng=round(stat_plt_rng*100)/100;
1876 axis([plotted_times(1) plotted_times(end) stat_plt_rng]);
1877 set(dat.h_statrangeB,'string',num2str(stat_plt_rng));
1878 set(dat.h_t0lineB,'YData',stat_plt_rng); %adjust time=0 line
1879 current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1880     dat.times);
1881 h_lineB=plot([1 1]*dat.times(current_tpt),stat_plt_rng,'k');
1882 set(h_lineB,'linewidth',2);
1883 dat.h_lineB=h_lineB;
1884 
1885 bdf_code = [ 'tmppos = get(gca, ''currentpoint'');' ...
1886     'dat=get(gcbf, ''userdata'');' ...
1887     'new_tpt=find_tpt(tmppos(1,1),dat.times);' ...
1888     'set(dat.h_lineA,''XData'',[1 1]*dat.times(new_tpt));' ...
1889     'set(dat.h_lineB,''XData'',[1 1]*dat.times(new_tpt));' ...
1890     'set(dat.h_topo_time,''string'',num2str(dat.times(new_tpt)));' ...
1891     'set(dat.fig_id,''userdata'',dat);' ...
1892     'gui_erp(''redraw topo'');' ...
1893     'drawnow;' ...
1894     'clear latpoint dattmp tmppos;' ...
1895     ];
1896 set(dat.h_showingB,'ButtonDownFcn',bdf_code);
1897 set(dat.h_timeB,'ButtonDownFcn',bdf_code);
1898 
1899 %redraw topo color bar for Axis B
1900 dat.absmxB=max(max(abs(dat.showingB(:,dat.start_pt:dat.end_pt))));
1901 axes(dat.h_cbarB);
1902 cla;
1903 cbar(dat.h_cbarB);
1904 absmx=round(dat.absmxB*100)/100;
1905 set(gca,'xticklabel',[-absmx 0 absmx]);
1906 h_cbar_title=title(cbar_title);
1907 set(h_cbar_title,'fontsize',cbar_title_fontsize);
1908 
1909 if redrawA,
1910     %redraw Axis A time plot
1911     new_title=['Bin ' int2str(new_bin) ': ' dat.bindesc{new_bin}];
1912     title_max_char=43;
1913     if length(new_title)>title_max_char,
1914         new_title=new_title(1:title_max_char);
1915     end
1916     set(dat.h_time_title,'string',new_title);
1917     
1918     
1919     %redraw Axis A time plot
1920     axes(dat.h_timeA);
1921     delete(dat.h_showingA);
1922     delete(dat.h_lineA);
1923     plotted_times=dat.times(dat.start_pt:dat.end_pt);
1924     %store t-scores for waveform that will be shown
1925     if (crnt_ttest_id<=n_t_tests) && dat.t_tests(crnt_ttest_id).null_mean
1926         %The mean of the null hypothesis is non-zero
1927         dat.showing_t=squeeze( (dat.erp(dat.showing_chans,:,new_bin)- ...
1928             dat.t_tests(crnt_ttest_id).null_mean)./dat.stder(dat.showing_chans,:,new_bin) );
1929     else
1930         dat.showing_t=squeeze(dat.t_scores(dat.showing_chans,:,new_bin));
1931     end
1932     
1933     dat.showingA=squeeze(dat.erp(dat.showing_chans,:,new_bin));
1934     cbar_title='\muV';
1935     ylab='\muV (ERP)';
1936     if size(dat.showingA,1)==1,
1937         %only one channel being plot
1938         dat.h_showingA=plot(dat.times,dat.showingA);
1939         cbar_title='Not Applicable';
1940         cbar_title_fontsize=10;
1941     else
1942         dat.h_showingA=plot(dat.times,dat.showingA');
1943     end
1944     set(dat.h_time_ylabA,'string',ylab);
1945     
1946     stat_mx=max(max(dat.showingA));
1947     stat_mn=min(min(dat.showingA));
1948     stat_rng=stat_mx-stat_mn;
1949     stat_plt_rng=[stat_mn-stat_rng*.02 stat_mx+stat_rng*.02];
1950     stat_plt_rng=round(stat_plt_rng*100)/100;
1951     axis([plotted_times(1) plotted_times(end) stat_plt_rng]);
1952     set(dat.h_statrangeA,'string',num2str(stat_plt_rng));
1953     set(dat.h_t0lineA,'YData',stat_plt_rng); %adjust time=0 line
1954     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1955         dat.times);
1956     h_lineA=plot([1 1]*dat.times(current_tpt),stat_plt_rng,'k');
1957     set(h_lineA,'linewidth',2);
1958     dat.h_lineA=h_lineA;
1959     
1960     bdf_code = [ 'tmppos = get(gca, ''currentpoint'');' ...
1961         'dat=get(gcbf, ''userdata'');' ...
1962         'new_tpt=find_tpt(tmppos(1,1),dat.times);' ...
1963         'set(dat.h_lineA,''XData'',[1 1]*dat.times(new_tpt));' ...
1964         'set(dat.h_lineB,''XData'',[1 1]*dat.times(new_tpt));' ...
1965         'set(dat.h_topo_time,''string'',num2str(dat.times(new_tpt)));' ...
1966         'set(dat.fig_id,''userdata'',dat);' ...
1967         'gui_erp(''redraw topo'');' ...
1968         'drawnow;' ...
1969         'clear latpoint dattmp tmppos;' ...
1970         ];
1971     set(dat.h_timeA,'ButtonDownFcn',bdf_code);
1972     set(dat.h_showingA,'ButtonDownFcn',bdf_code);
1973     
1974     %redraw topo color bar for Axis A
1975     dat.absmxA=max(max(abs(dat.showingA(:,dat.start_pt:dat.end_pt))));
1976     axes(dat.h_cbarA);
1977     cla;
1978     cbar(dat.h_cbarA);
1979     absmx=round(dat.absmxA*100)/100;
1980     set(gca,'xticklabel',[-absmx 0 absmx]);
1981     h_cbar_title=title(cbar_title);
1982     set(h_cbar_title,'fontsize',cbar_title_fontsize);
1983 end
1984 
1985 set(dat.fig_id,'userdata',dat);
1986 
1987 %note, redraw_topos is called by update dashed lines
1988 
1989 
1990 
1991 function [dat, redraw_wform]=update_test(dat)
1992 % Updates the t-test results paramters (e.g., critical t-score)
1993 % Used by: 'change test' & 'change bin'
1994 %
1995 % Inputs:
1996 %   dat - The userdata of the GUI
1997 %
1998 % Outputs:
1999 %   dat            - The revised of the GUI
2000 %   redraw_wform   - 1 if the waveforms in the GUI need to be redrawn
2001 %                    (e.g., if the bin being visualized has changed),
2002 %                    otherwise it's 0
2003 %
2004 
2005 crnt_ttest=get(dat.h_ptest,'value');
2006 
2007 n_psbl_tests=length(dat.psbl_tests);
2008 crnt_bin=get(dat.h_bin,'value');
2009 redraw_wform=0;
2010 if n_psbl_tests<crnt_ttest,
2011     %manual/no test, remove all test info/visualizations
2012     set(dat.h_testwind,'string',[]); %get rid of test wind
2013     dat.alpha=[];
2014     dat.critical_t=[];
2015     dat.mltplcmp_crct=[];
2016     set(dat.h_critval,'string',[]);
2017     
2018     if ~isequal(dat.loaded_chans,dat.showing_chans)
2019         redraw_wform=1;
2020     end    
2021 else
2022     ptest_bin=dat.t_tests(crnt_ttest).bin;
2023     if ptest_bin~=crnt_bin,
2024         redraw_wform=1;
2025         set(dat.h_bin,'value',ptest_bin);
2026     end
2027     
2028     %If the mean of the null hypothesis of the previously shown test
2029     %results differs from that of the new test results, redraw the waveform
2030     crnt_null_mean=dat.null_mean;
2031     if crnt_null_mean~=dat.t_tests(crnt_ttest).null_mean,
2032         redraw_wform=1;
2033     end
2034     dat.null_mean=dat.t_tests(crnt_ttest).null_mean;
2035     
2036     %test time window(s)
2037     test_wind=dat.t_tests(crnt_ttest).time_wind;
2038     dat.n_wind=size(test_wind,1);
2039     wind_edges=[];
2040     for nw=1:dat.n_wind,
2041         if nw==dat.n_wind,
2042             wind_edges=[wind_edges num2str(test_wind(nw,:))];
2043         else
2044             wind_edges=[wind_edges num2str(test_wind(nw,:)) '; '];
2045         end
2046     end
2047     set(dat.h_testwind,'string',wind_edges);
2048     
2049     % if test was performed including channels that are not currently
2050     % loaded, throw a warning
2051     if ~isempty(setdiff(dat.t_tests(crnt_ttest).used_chan_ids, ...
2052             dat.loaded_chans)),
2053         warndlg('The set of t-tests you selected includes some channels that were excluded when you created this GUI.  Keep this in mind when interpreting the visualization.','gui_erp Warning');
2054     end
2055     
2056     % if test was performed on a set of channels that differs from what
2057     % is currently shown, replot the data with the 'change bin' command
2058     if ~isequal(dat.t_tests(crnt_ttest).used_chan_ids,dat.showing_chans)
2059         redraw_wform=1;
2060     end
2061     
2062     % set critical values and alpha level
2063     if isnan(dat.t_tests(crnt_ttest).estimated_alpha)
2064         dat.alpha=dat.t_tests(crnt_ttest).desired_alphaORq;
2065         dat.mltplcmp_crct='fdr';
2066     else
2067         dat.alpha=dat.t_tests(crnt_ttest).estimated_alpha;
2068         dat.mltplcmp_crct='perm';
2069     end
2070     dat.critical_t=dat.t_tests(crnt_ttest).crit_t;
2071     set(dat.h_critval,'string',num2str(dat.critical_t));
2072 end
2073 set(dat.fig_id,'userdata',dat);
2074 
2075 
2076 function redraw_topos(dat,redraw_cbars)
2077 
2078 current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
2079     dat.times);
2080 
2081 test_wind=str2num(get(dat.h_testwind,'string'));
2082 critical_t=str2num(get(dat.h_critval,'string'));
2083 
2084 if ~isempty(test_wind) && ~isempty(critical_t),
2085     %if current time point is in a test window look for channels with
2086     %sig effects
2087     crnt_ttest=get(dat.h_ptest,'value');
2088     current_tpt_in_ms=dat.times(current_tpt);
2089     n_showing=length(dat.showing_chans);
2090     sig_chans=zeros(1,n_showing);    
2091     for nw=1:dat.n_wind,
2092         if (current_tpt_in_ms>=test_wind(nw,1)) && (current_tpt_in_ms<=test_wind(nw,2)),
2093             if isnan(critical_t)
2094                 %cluster based test
2095                 pval_tpt_id=find(dat.t_tests(crnt_ttest).used_tpt_ids==current_tpt);
2096                 if ~isempty(pval_tpt_id)
2097                     %if there's a significant electrode
2098                     sig_chans_temp=find(dat.t_tests(crnt_ttest).adj_pval(:,pval_tpt_id)<dat.t_tests(crnt_ttest).desired_alphaORq);
2099                     sig_chans_temp=dat.t_tests(crnt_ttest).used_chan_ids(sig_chans_temp); %convert sig channel indices into channel indices in the original GND/GRP variable
2100                     sig_chans=zeros(1,n_showing);
2101                     for a=1:n_showing,
2102                         if ismember(dat.showing_chans(a),sig_chans_temp)
2103                             sig_chans(a)=1;
2104                         end
2105                     end
2106                 end
2107             else
2108                 if length(critical_t)==2,
2109                     sig_chans(dat.showing_t(:,current_tpt)>max(critical_t))=1;
2110                     sig_chans(dat.showing_t(:,current_tpt)<min(critical_t))=1;
2111                 else
2112                     if critical_t>0,
2113                         sig_chans(dat.showing_t(:,current_tpt)>critical_t)=1;
2114                     else
2115                         sig_chans(dat.showing_t(:,current_tpt)<critical_t)=1;
2116                     end
2117                 end
2118             end
2119             break; %Visualized time point is in this window. Break out of for loop since there's no need to look at additional time windows
2120         end
2121     end
2122     sig_chans=find(sig_chans>0);
2123 else
2124     sig_chans=[];
2125 end
2126 
2127 % ERP TOPO
2128 set(dat.fig_id,'CurrentAxes',dat.h_topoA); %<-changes current axis without updating GUI graphics
2129 %which is why I use this instead of axes(dat.h_topoA)
2130 if size(dat.showingA,1)<=2,
2131     %two or fewer channels, we can only plot electrode locations
2132     topoplotMK(dat.showingA(:,current_tpt),dat.chanlocs(dat.showing_chans), ...
2133         'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
2134     
2135     if get(dat.h_stat,'value')==3,
2136         %GFP topo
2137         if redraw_cbars,
2138             %statistic has been changed, need to erase old topo
2139             set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2140             topoplotMK([],dat.chanlocs(dat.showing_chans), ...
2141                 'style','blank','plain_blank',1);
2142         end
2143     else
2144         % t-score/stderr
2145         set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2146         topoplotMK(dat.showingB(:,current_tpt),dat.chanlocs(dat.showing_chans), ...
2147             'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
2148     end
2149 else
2150     topoplotMK(dat.showingA(:,current_tpt),dat.chanlocs(dat.showing_chans),'maplimits', ...
2151         [-1 1]*dat.absmxA,'emarker2',{sig_chans,'o',[1 1 1],4});
2152     set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702); %make topoplot background color match that of GUI
2153     
2154     if get(dat.h_stat,'value')==3,
2155         %GFP topo
2156         if redraw_cbars,
2157             %statistic has been changed, need to erase old topo
2158             set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2159             topoplotMK([],dat.chanlocs(dat.showing_chans), ...
2160                 'style','blank','plain_blank',1);
2161             set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702); %make topoplot background color match that of GUI
2162         end
2163     else
2164         % t-score/stderr
2165         set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2166         topoplotMK(dat.showingB(:,current_tpt),dat.chanlocs(dat.showing_chans),'maplimits', ...
2167             [-1 1]*dat.absmxB,'emarker2',{sig_chans,'o',[1 1 1],4});
2168         set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702); %make topoplot background color match that of GUI
2169     end
2170 end
2171 
2172 if (redraw_cbars)
2173     % ERP COLOR BAR TICK LABELS
2174     %axes(dat.h_cbarA);
2175     set(dat.fig_id,'CurrentAxes',dat.h_cbarA);
2176     absmx=round(dat.absmxA*100)/100;
2177     set(gca,'xticklabel',[-absmx 0 absmx]);
2178     
2179     % t-Score/StdErr/GFP COLOR BAR TICK LABELS
2180     %axes(dat.h_cbarB);
2181     set(dat.fig_id,'CurrentAxes',dat.h_cbarB);
2182     absmx=round(dat.absmxB*100)/100;
2183     set(gca,'xticklabel',[-absmx 0 absmx]);
2184 end
2185 drawnow
2186

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005