Home > matlabmk > gui_erpORIG.m

gui_erpORIG

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 % 3/26/2013-Function would throw an error when trying to visualize a test
0327 % with FDR control that didn't produce any significant results.  Should be
0328 % fixed now.
0329 
0330 function gui_erp(cmnd_str,varargin)
0331 
0332 p=inputParser;
0333 p.addRequired('cmnd_str',@(x) ischar(x) || isstruct(x));
0334 p.addParamValue('fig_id',[],@(x) isempty(x) || (isnumeric(x) && (length(x)==1)));
0335 p.addParamValue('bin',1,@(x) isnumeric(x) && (length(x)==1));
0336 p.addParamValue('show_wind',[],@(x) isempty(x) || (isnumeric(x) && (length(x)==2)));
0337 p.addParamValue('GNDorGRP',[],@isstruct);
0338 p.addParamValue('critical_t',[],@(x) isempty(x) || (isnumeric(x) && (length(x)<=2)));
0339 p.addParamValue('test_wind',[],@(x) isempty(x) || (isnumeric(x) && (size(x,2)==2)));
0340 p.addParamValue('ydir',-1,@(x) isnumeric(x) && (length(x)==1));
0341 p.addParamValue('stat','t',@(x) ischar(x) || strcmpi(x,'t') || strcmpi(x,'stder'));
0342 p.addParamValue('exclude_chans',[],@(x) ischar(x) || iscell(x) || isempty(x));
0343 p.addParamValue('include_chans',[],@(x) ischar(x) || iscell(x) || isempty(x));
0344 p.addParamValue('alpha_or_q',[],@(x) isempty(x) || isnumeric(x));
0345 p.addParamValue('t_test',[],@(x) isempty(x) || (isnumeric(x) && (length(x)==1) && (x>0)));
0346 p.addParamValue('verblevel',2,@(x) isnumeric(x) && (length(x)==1));
0347 p.parse(cmnd_str,varargin{:});
0348 
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=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 ~strcmpi(dat.mltplcmp_crct,'fdr') && (length(critical_t)==1) && isnan(critical_t) % critical_t is always the scalar NaN for cluster based permutation tests
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                         % I don't think one critical t value can be NaN and not
0956                         % the other, but just in case
0957                         if isnan(critical_t(2))
0958                             sig_chans=[];
0959                         else
0960                             sig_chans=find(dat.showing_t(:,plotted_pts(mx_tpt))>max(critical_t));
0961                         end
0962                         if ~isnan(critical_t(1))
0963                             sig_chans=[sig_chans; find(dat.showing_t(:,plotted_pts(mx_tpt))<min(critical_t))];
0964                         end
0965                     else
0966                         if isnan(critical_t) %FDR control con produce NaN critical_t's if not test are significant
0967                             sig_chans=[];
0968                         elseif critical_t>0,
0969                             sig_chans=find(dat.showing_t(:,plotted_pts(mx_tpt))>critical_t);
0970                         else
0971                             sig_chans=find(dat.showing_t(:,plotted_pts(mx_tpt))<critical_t);
0972                         end
0973                     end
0974                 end
0975                 break; %Visualized time point is in this window. Break out of for loop since there's no need to look at additional time windows
0976             end
0977         end
0978     end
0979     cbar_title_fontsize=14;
0980     if size(dat.showingA,1)<=2,
0981         %two or fewer channels, we can only plot electrode locations
0982         topoplotMK(dat.showingA(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
0983             'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
0984         cbar_title='Not Applicable';
0985         cbar_title_fontsize=10; %for some reason a fontsize of 14 cuts off last later for topoB axis
0986     else
0987         topoplotMK(dat.showingA(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
0988             'maplimits',[-1 1]*dat.absmxA,'emarker2',{sig_chans,'o',[1 1 1],4});
0989         set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702);
0990         cbar_title='\muV';
0991     end
0992 
0993     
0994     % COLOR BAR
0995     dat.h_cbarA=axes('position',[0.76 .945 .2 .015]);
0996     cbar(dat.h_cbarA);
0997     absmx=round(dat.absmxA*100)/100;
0998     set(gca,'xticklabel',[-absmx 0 absmx]);
0999     h_cbar_title=title(cbar_title);
1000     set(h_cbar_title,'fontsize',cbar_title_fontsize);
1001     
1002     
1003     %%% TIME POINT TEXT BOX
1004     %STATIC TEXT LABEL
1005     uicontrol(dat.fig_id,...
1006         'Units','normalized', ...
1007         'Position',[ 0.905 0.62 0.07 0.04 ],...
1008         'String','msec',...
1009         'fontsize',14, ...
1010         'fontunits','normalized', ...
1011         'Style','text');
1012     dat.h_topo_time=uicontrol(dat.fig_id,...
1013         'CallBack','gui_erp(''time jump'');',...
1014         'Units','normalized', ...
1015         'Position',[ 0.805 0.62 0.1 0.05 ], ...
1016         'String',num2str(plotted_times(mx_tpt)), ...
1017         'Style','edit', ...
1018         'Enable','on', ...
1019         'ToolTipString','Time point to visualize topographicaly.', ...
1020         'fontsize',14, ...
1021         'fontunits','normalized', ...
1022         'horizontalalignment','center', ...
1023         'BackGroundColor','w', ...
1024         'Tag','topo_time');
1025     
1026     
1027     %
1028     %%%%%%%%%%%%%%%%%%%%%% AXIS D: t-Score/Standard Error/GFP Topography %%%%%%%%%%%%%%%%%%%%%%%%
1029     %
1030     
1031     
1032     % PANEL
1033     uipanel(dat.fig_id,...
1034         'Units','normalized', ...
1035         'Position',[ 0.719 0.175 0.281 0.42 ],...
1036         'shadowcolor','k', ...
1037         'highlightcolor',frm_col, ...
1038         'foregroundcolor',frm_col, ...
1039         'backgroundcolor',frm_col);
1040     
1041     % TOPOGRAPHY
1042     dat.h_topoB=axes('position',[0.705 .27 .31 .24],'box','off');
1043     
1044     cbar_title_fontsize=14;
1045     if strcmpi(p.Results.stat,'gfp') || isinf(dat.t_scores(1,1,bin)) || isnan(dat.t_scores(1,1,bin))
1046         topoplotMK([],dat.chanlocs(dat.showing_chans), ...
1047             'style','blank','plain_blank',1);
1048         cbar_title='Not Applicable';
1049         cbar_title_fontsize=10;
1050     else
1051         if size(dat.showingB,1)<=2,
1052             %two or fewer channels, we can only plot electrode locations
1053             topoplotMK(dat.showingB(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
1054                 'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
1055             cbar_title='Not Applicable';
1056             cbar_title_fontsize=10;
1057         else
1058             topoplotMK(dat.showingB(:,plotted_pts(mx_tpt)),dat.chanlocs(dat.showing_chans), ...
1059                 'maplimits',[-1 1]*dat.absmxB,'emarker2',{sig_chans,'o',[1 1 1],4});
1060             set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702);
1061             if strcmpi(p.Results.stat,'t'),
1062                 cbar_title='t-score';
1063             else
1064                 cbar_title='\muV'; %ERPs and stder are in units of uV
1065             end
1066         end
1067     end
1068     
1069     % COLOR BAR
1070     dat.h_cbarB=axes('position',[0.76 .535 .2 .015]);
1071     cbar(dat.h_cbarB);
1072     absmx=round(dat.absmxB*100)/100;
1073     set(gca,'xticklabel',[-absmx 0 absmx]);
1074     h_cbar_title=title(cbar_title);
1075     set(h_cbar_title,'fontsize',cbar_title_fontsize);
1076     
1077     % GO BACK 1 TIME POINT
1078     dat.h_back1=uicontrol(dat.fig_id,...
1079         'CallBack','gui_erp(''back one'');',...
1080         'Units','normalized', ...
1081         'Position',[ 0.80 0.232 0.055 0.04 ],...
1082         'String','<',...
1083         'fontsize',14, ...
1084         'fontunits','normalized', ...
1085         'Tag','back1', ...
1086         'ToolTipString','Show topography at preceding time point.', ...
1087         'Style','pushbutton');
1088     
1089     % GO FORWARD 1 TIME POINT
1090     dat.h_forward1=uicontrol(dat.fig_id,...
1091         'CallBack','gui_erp(''forward one'');',...
1092         'Units','normalized', ...
1093         'Position',[ 0.865 0.232 0.055 0.04 ],...
1094         'String','>', ...
1095         'fontsize',14, ...
1096         'fontunits','normalized', ...
1097         'Tag','forward1', ...
1098         'ToolTipString','Show topography at subsequent time point.', ...
1099         'Style','pushbutton');
1100     
1101     % ANIMATE BACKWARDS IN TIME
1102     dat.h_back=uicontrol(dat.fig_id,...
1103         'CallBack','gui_erp(''back'');',...
1104         'Units','normalized', ...
1105         'Position',[ 0.735 0.232 0.055 0.04 ],...
1106         'String','<<',...
1107         'fontsize',14, ...
1108         'fontunits','normalized', ...
1109         'Tag','back', ...
1110         'interruptible','on', ...
1111         'ToolTipString','Animate topography going backwards in time.', ...
1112         'Style','pushbutton');
1113     
1114     % ANIMATE FORWARDS IN TIME
1115     dat.h_forward=uicontrol(dat.fig_id,...
1116         'CallBack','gui_erp(''forward'');',...
1117         'Units','normalized', ...
1118         'Position',[ 0.93 0.232 0.055 0.04 ],...
1119         'String','>>',...
1120         'fontsize',14, ...
1121         'fontunits','normalized', ...
1122         'Tag','back', ...
1123         'interruptible','on', ...
1124         'ToolTipString','Animate topography going forwards in time.', ...
1125         'Style','pushbutton');
1126     
1127     % STOP ANIMATION
1128     dat.h_stop=uicontrol(dat.fig_id,...
1129         'CallBack','dat=get(gcbf,''userdata''); dat.interrupt=1; set(dat.fig_id,''userdata'',dat);',...
1130         'Units','normalized', ...
1131         'Position',[ 0.745 0.179 0.23 0.05 ],...
1132         'String','Stop Animation',...
1133         'fontsize',14, ...
1134         'fontunits','normalized', ...
1135         'Tag','stop', ...
1136         'enable','off', ...
1137         'ToolTipString','Stop topography animation.', ...
1138         'Style','pushbutton');
1139    
1140 
1141     
1142     %
1143     %%%%%%%%%%%%%%%%%%%%%% AXIS E: PLOTTING OPTIONS %%%%%%%%%%%%%%%%%%%%%%%%
1144     %
1145     
1146     % PANEL
1147     uipanel(dat.fig_id,...
1148         'Units','normalized', ...
1149         'Position',[ 0 0 1 0.178 ],...
1150         'shadowcolor','k', ...
1151         'highlightcolor',frm_col, ...
1152         'foregroundcolor',frm_col, ...
1153         'backgroundcolor',frm_col);
1154       
1155         
1156     %%%% WHICH BIN TO PLOT
1157     bdesc_str=cell(1,n_bin);
1158     for a=1:n_bin,
1159         bdesc_str{a}=sprintf('Bin %d: %s',a,dat.bindesc{a});
1160     end 
1161     % Text
1162     uicontrol(dat.fig_id,...
1163         'Units','normalized', ...
1164         'Position',[ 0.01 0.115 0.2 0.05 ],...
1165         'String','Current Bin',...
1166         'fontsize',12, ...
1167         'ToolTipString','Bin to plot in Time x ERP/t-score axis.', ...
1168         'Style','text');
1169     % Create bin menu
1170     dat.h_bin=uicontrol(dat.fig_id,...
1171         'CallBack','gui_erp(''change bin'');',...
1172         'Units','normalized', ...
1173         'Position',[ 0.01 0.08 0.24 0.05 ],...
1174         'fontsize',12, ...
1175         'String',bdesc_str, ...
1176         'Value',bin, ...
1177         'Style','popup', ...
1178         'ToolTipString','Bin to plot in Time x ERP/t-score axis.', ...
1179         'tag','bin');
1180     
1181     
1182     %%%% WHICH T-TESTS RESULTS TO PLOT
1183     ttest_str=cell(1,n_psbl_tests+1);
1184     for a=1:n_psbl_tests,
1185         ttest_str{a}=sprintf('Test %d: Bin %d, Times ',dat.psbl_tests(a),dat.t_tests(a).bin);
1186         n_ttst_wind=size(dat.t_tests(a).time_wind,1);
1187         for b=1:n_ttst_wind,
1188             ttest_str{a}=[ttest_str{a} int2str(dat.t_tests(a).time_wind(b,1)) '-' int2str(dat.t_tests(a).time_wind(b,2)) ','];
1189         end
1190         if length(dat.t_tests(a).crit_t)==2,
1191             ttest_str{a}=[ttest_str{a} ' Two-Tailed,'];
1192         elseif dat.t_tests(a).crit_t<0,
1193             ttest_str{a}=[ttest_str{a} ' Lower-Tailed,'];
1194         else
1195             ttest_str{a}=[ttest_str{a} ' Upper-Tailed,'];
1196         end
1197         
1198         if isnan(dat.t_tests(a).estimated_alpha)
1199             ttest_str{a}=[ttest_str{a} sprintf(' q=%.4f',dat.t_tests(a).desired_alphaORq)];
1200             ttest_str{a}=[ttest_str{a} sprintf(', method=%s',dat.t_tests(a).mult_comp_method)];
1201         else
1202             ttest_str{a}=[ttest_str{a} sprintf(' alpha=%.4f',dat.t_tests(a).estimated_alpha)];
1203         end
1204         
1205         if dat.t_tests(a).null_mean,
1206             ttest_str{a}=[ttest_str{a} sprintf(', Null Mean=%.4f',dat.t_tests(a).null_mean)];
1207         end
1208     end
1209     ttest_str{n_psbl_tests+1}='None/Manual';
1210     
1211     % Text
1212     uicontrol(dat.fig_id,...
1213         'Units','normalized', ...
1214         'Position',[ 0.275 0.115 0.2 0.05 ],...
1215         'String','Current Test Result',...
1216         'fontsize',12, ...
1217         'ToolTipString','Set of t-test results to visualize.', ...
1218         'Style','text');
1219     % Create sortvar browse button
1220     dat.h_ptest=uicontrol(dat.fig_id,...
1221         'CallBack','gui_erp(''change test'');',...
1222         'Units','normalized', ...
1223         'Position',[ 0.26 0.075 0.24 0.055 ],...
1224         'fontsize',12, ...
1225         'String',ttest_str, ...
1226         'Value',crnt_ttest, ...
1227         'Style','popup', ...
1228         'ToolTipString','Set of t-test results to visualize.', ...
1229         'tag','bin');
1230 
1231     %%% Time window in which hypotheses tests were done
1232     %Text
1233     uicontrol(dat.fig_id,...
1234         'Units','normalized', ...
1235         'Position',[ 0.505 0.115 0.26 0.05 ],...
1236         'String','Test Window(s) [Min Max]:',...
1237         'fontsize',12, ...
1238         'Style','text');
1239     %Edit Box
1240     wind_edges=[];
1241     for nw=1:dat.n_wind,
1242         if nw==dat.n_wind,
1243             wind_edges=[wind_edges num2str(test_wind(nw,:))];
1244         else
1245             wind_edges=[wind_edges num2str(test_wind(nw,:)) '; ']; 
1246         end
1247     end
1248     dat.h_testwind=uicontrol(dat.fig_id,...
1249         'CallBack','gui_erp(''update dashed lines'');',...
1250         'Units','normalized', ...
1251         'fontsize',12, ...
1252         'BackGroundColor','w', ...
1253         'Position',[ 0.525 0.095 0.22 0.04 ],...
1254         'String',wind_edges, ...
1255         'Style','edit', ...
1256         'ToolTipString','Miniumum and maximum time of time window in which hypothesis tests were done.', ...
1257         'tag','timerange'); 
1258     
1259     
1260     %%% Critical t-score here
1261     %Text
1262     uicontrol(dat.fig_id,...
1263         'Units','normalized', ...
1264         'Position',[ 0.76 0.115 0.23 0.05 ],...
1265         'String','Critical t-score(s)',...
1266         'fontsize',12, ...
1267         'Style','text');
1268     %Edit Box
1269     dat.h_critval=uicontrol(dat.fig_id,...
1270         'CallBack','gui_erp(''update critical t'');',...
1271         'Units','normalized', ...
1272         'fontsize',12, ...
1273         'BackGroundColor','w', ...
1274         'Position',[ 0.79 0.09 0.16 0.045 ],...
1275         'String',num2str(critical_t), ...
1276         'Style','edit', ...
1277         'ToolTipString','Critical t-score(s) for reliable deviation from 0 voltage.', ...
1278         'tag','timerange');
1279     
1280     %%%% WHICH STAT TO PLOT
1281     stats={'t-score of ERPs','standard error of ERPs','global field power of ERPs'};
1282     % Text
1283     uicontrol(dat.fig_id,...
1284         'Units','normalized', ...
1285         'Position',[ 0.01 0.183 0.09 0.03 ],...
1286         'String','Statistic:',...
1287         'fontsize',12, ...
1288         'ToolTipString','Select what to plot in secondary waveform axis: ERP t-scores, standard error of the mean, or global field power.', ...
1289         'Style','text');
1290     
1291     if strcmpi(p.Results.stat,'gfp') || isinf(dat.t_scores(1,1,bin)) || isnan(dat.t_scores(1,1,bin))
1292         %isinf(dat.t_scores(1,1,bin)) will
1293         %equal 1 if there are insufficient subjects to calculate t-scores
1294         ini_val=3;
1295     elseif strcmpi(p.Results.stat,'t'),
1296         ini_val=1;
1297     elseif strcmpi(p.Results.stat,'stder'),
1298         ini_val=2;
1299     else
1300         error('Unrecognized value of input argument ''stat''.');
1301     end
1302     dat.h_stat=uicontrol(dat.fig_id,...
1303         'CallBack','gui_erp(''change stat'');',...
1304         'Units','normalized', ...
1305         'Position',[ 0.095 0.165 0.16 0.05 ],...
1306         'fontsize',12, ...
1307         'value',ini_val, ...
1308         'String',stats, ...
1309         'Style','popup', ...
1310         'ToolTipString','Plot ERP t-scores or standard error of the mean in secondary axis?', ...
1311         'tag','stat');
1312     
1313     %%% TIME RANGE
1314     %Text
1315     uicontrol(dat.fig_id,...
1316         'Units','normalized', ...
1317         'Position',[ 0.01 0.035 0.24 0.05 ],...
1318         'String','Time Range [Min Max]',...
1319         'fontsize',12, ...
1320         'Style','text');
1321     %Edit Box
1322     dat.h_timerange=uicontrol(dat.fig_id,...
1323         'CallBack','gui_erp(''new time limits'');',...
1324         'Units','normalized', ...
1325         'fontsize',12, ...
1326         'BackGroundColor','w', ...
1327         'Position',[ 0.06 0.013 0.11 0.045 ],...
1328         'String',num2str(dat.plt_times), ...
1329         'Style','edit', ...
1330         'ToolTipString','Miniumum and maximum time on Time x ERP axis.', ...
1331         'tag','timerange');   
1332     
1333     %%% ERP uV RANGE BOX (AXIS A)
1334     %Text
1335     uicontrol(dat.fig_id,...
1336         'Units','normalized', ...
1337         'Position',[ 0.26 0.035 0.26 0.05 ],...
1338         'String','ERP Range [Min Max]',...
1339         'HorizontalAlignment','left', ...
1340         'fontsize',12, ...
1341         'Style','text');
1342     %Edit Box
1343     dat.h_statrangeA=uicontrol(dat.fig_id,...
1344         'CallBack','gui_erp(''new statistic limitsA'');',...
1345         'Units','normalized', ...
1346         'fontsize',12, ...
1347         'BackGroundColor','w', ...
1348         'Position',[ 0.28 0.013 0.16 0.045 ],...
1349         'String',num2str(round(vA(3:4)*100)/100), ...
1350         'ToolTipString','Miniumum and maximum voltage on Time x ERP axis.', ...
1351         'Style','edit', ...
1352         'tag','statrange'); 
1353     
1354     %%% t-score/StdErr RANGE BOX (AXIS B)
1355     %Text
1356     uicontrol(dat.fig_id,...
1357         'Units','normalized', ...
1358         'Position',[ 0.50 0.035 0.26 0.05 ],...
1359         'String','t/StdErr Range [Min Max]',...
1360         'HorizontalAlignment','left', ...
1361         'fontsize',12, ...
1362         'Style','text');
1363     %Edit Box
1364     dat.h_statrangeB=uicontrol(dat.fig_id,...
1365         'CallBack','gui_erp(''new statistic limitsB'');',...
1366         'Units','normalized', ...
1367         'fontsize',12, ...
1368         'BackGroundColor','w', ...
1369         'Position',[ 0.54 0.013 0.16 0.045 ],...
1370         'String',num2str(round(vB(3:4)*100)/100), ...
1371         'ToolTipString','Miniumum and maximum t-scores/voltage on Time x t-Scores/Standard Error/GFP axis.', ...
1372         'Style','edit', ...
1373         'tag','statrange');   
1374     
1375     % Button to close GUI
1376     uicontrol(dat.fig_id,...
1377         'CallBack','close(gcbf);',...
1378         'Units','normalized', ...
1379         'Position',[ 0.75 0.012 0.11 0.05 ],...
1380         'String','Close',...
1381         'fontsize',12, ...
1382         'Tag','close', ...
1383         'backgroundcolor','m', ...
1384         'ToolTipString','Close GUI', ...
1385         'Style','pushbutton');
1386     
1387     dat.help_msg=sprintf(['Hold mouse cursor over a GUI control for an explanation of what it does.\n\n', ...
1388         'Time x waveform axis visualizes ERPs, t-score of ERPs, standard error of ERPs, or ERP global field power simultaneously at multiple electrodes.  ' ... 
1389         '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', ...
1390         'at that point in time.\n\nClick on electrodes in scalp topography to see electrode name.\n\n', ...
1391         'This GUI was produced by gui_erp.m']);
1392     
1393     % Button for help
1394     uicontrol(dat.fig_id,...
1395         'CallBack','dat=get(gcbf,''userdata''); helpdlg(dat.help_msg,''ERP GUI Help'');', ...
1396         'Units','normalized', ...
1397         'Position',[ 0.87 0.01 0.11 0.05 ],...
1398         'String','Help',...
1399         'fontsize',12, ...
1400         'Tag','help', ...
1401         'ToolTipString','Click for help', ...
1402         'Style','pushbutton');
1403     dat.interrupt=1;
1404     set(dat.fig_id,'userdata',dat);
1405 elseif strcmpi(cmnd_str,'time jump'),
1406     new_tpt=find_tpt(str2num(get(dat.h_topo_time,'string')), ...
1407         dat.times);
1408     if new_tpt<dat.start_pt,
1409         errordlg('That time is too early.');
1410     elseif new_tpt>dat.end_pt,
1411         errordlg('That time is too late.');
1412     else
1413         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1414         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1415         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1416         set(dat.fig_id,'userdata',dat);
1417         redraw_topos(dat,0);
1418         drawnow;
1419     end    
1420 elseif strcmpi(cmnd_str,'back one'),
1421     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1422         dat.times);
1423     new_tpt=current_tpt-1;
1424     if new_tpt<dat.start_pt,
1425         errordlg('Can''t go back any further.');
1426     else
1427         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1428         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1429         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1430         set(dat.fig_id,'userdata',dat);
1431         redraw_topos(dat,0);
1432         drawnow;
1433     end
1434 elseif strcmpi(cmnd_str,'forward one'),
1435     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1436         dat.times);
1437     new_tpt=current_tpt+1;
1438     if new_tpt>dat.end_pt,
1439         errordlg('Can''t go forward any further.');
1440     else
1441         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1442         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1443         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1444         set(dat.fig_id,'userdata',dat);
1445         redraw_topos(dat,0);
1446         drawnow;
1447     end
1448 elseif strcmpi(cmnd_str,'back'),
1449     interrupt=0;
1450     dat.interrupt=0;
1451     set(dat.h_stop,'enable','on');
1452     set(dat.h_forward,'enable','off');
1453     set(dat.h_back,'enable','off');
1454     set(dat.h_forward1,'enable','off');
1455     set(dat.h_back1,'enable','off');
1456     set(dat.fig_id,'userdata',dat);
1457     drawnow;
1458     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1459         dat.times);
1460     new_tpt=current_tpt-1;
1461     loop_ct=0;
1462     while (new_tpt>=dat.start_pt) && (interrupt~=1),
1463         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1464         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1465         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1466         drawnow('update');
1467         redraw_topos(dat,0);
1468         drawnow;
1469         new_tpt=new_tpt-1;
1470         loop_ct=loop_ct+1;
1471         if loop_ct==5,
1472             loop_ct=0;
1473             new_tmp=get(dat.fig_id,'userdata');
1474             interrupt=new_tmp.interrupt;
1475         end
1476     end
1477     drawnow;
1478     dat.interrupt=0;
1479     new_tpt=new_tpt+1; %undo step that broke loop
1480     set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1481     set(dat.h_stop,'enable','off');
1482     set(dat.h_forward,'enable','on');
1483     set(dat.h_back,'enable','on');
1484     set(dat.h_forward1,'enable','on');
1485     set(dat.h_back1,'enable','on');
1486     set(dat.fig_id,'userdata',dat);
1487 elseif strcmpi(cmnd_str,'forward'),
1488     interrupt=0;
1489     dat.interrupt=0;
1490     set(dat.h_stop,'enable','on');
1491     set(dat.h_forward,'enable','off');
1492     set(dat.h_back,'enable','off');
1493     set(dat.h_forward1,'enable','off');
1494     set(dat.h_back1,'enable','off');
1495     set(dat.fig_id,'userdata',dat);
1496     drawnow;
1497     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1498         dat.times);
1499     new_tpt=current_tpt+1;
1500     loop_ct=0;
1501     while (new_tpt<=dat.end_pt) && (interrupt~=1),
1502         set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1503         set(dat.h_lineA,'XData',[1 1]*dat.times(new_tpt));
1504         set(dat.h_lineB,'XData',[1 1]*dat.times(new_tpt));
1505         drawnow('update');
1506         redraw_topos(dat,0);
1507         drawnow;
1508         new_tpt=new_tpt+1;
1509         loop_ct=loop_ct+1;
1510         if loop_ct==5,
1511             loop_ct=0;
1512             new_tmp=get(dat.fig_id,'userdata');
1513             interrupt=new_tmp.interrupt;
1514         end
1515     end
1516     drawnow;
1517     dat.interrupt=0;
1518     new_tpt=new_tpt-1; %undo step that broke loop
1519     set(dat.h_topo_time,'string',num2str(dat.times(new_tpt)));
1520     set(dat.h_stop,'enable','off');
1521     set(dat.h_forward,'enable','on');
1522     set(dat.h_back,'enable','on');
1523     set(dat.h_forward1,'enable','on');
1524     set(dat.h_back1,'enable','on');
1525     set(dat.fig_id,'userdata',dat);
1526 elseif strcmpi(cmnd_str,'change stat'),
1527     stat=get(dat.h_stat,'value');
1528     current_bin=get(dat.h_bin,'value');
1529     if (stat~=3),
1530         %make sure there are sufficients subjects for that bin
1531         if isinf(dat.t_scores(1,1,current_bin)) || isnan(dat.t_scores(1,1,current_bin)) 
1532             stat=3;
1533             set(dat.h_stat,'value',stat);
1534             if (stat==2)
1535                 warndlg('Only one participant contributed to this bin. Thus the grand average standard error cannot be esimated.','gui_erp Warning');
1536             else
1537                 warndlg('Only one participant contributed to this bin. Thus the grand average t-scores cannot be derived.','gui_erp Warning');
1538             end
1539         end
1540     end
1541         
1542     if (stat==1),
1543         % t-scores
1544         if ~isempty(dat.h_crit1),
1545             delete(dat.h_crit1);
1546         end
1547         if ~isempty(dat.h_crit2),
1548             delete(dat.h_crit2);
1549         end
1550         dat.h_crit1=[];
1551         dat.h_crit2=[];
1552         
1553         if ~isempty(dat.h_alph1),
1554             delete(dat.h_alph1);
1555         end
1556         if ~isempty(dat.h_alph2),
1557             delete(dat.h_alph2);
1558         end
1559         dat.h_alph1=[];
1560         dat.h_alph2=[];
1561     end
1562     
1563     %redraw time plot
1564     draw_waveforms(dat,0);
1565     gui_erp('update dashed lines'); %also redraws topo
1566 elseif strcmpi(cmnd_str,'change bin'),
1567     new_bin=get(dat.h_bin,'value');
1568     crnt_ttest_id=get(dat.h_ptest,'value');
1569     n_t_tests=length(dat.t_tests);
1570     if (crnt_ttest_id==(n_t_tests+1)) || (dat.t_tests(crnt_ttest_id).bin~=new_bin),
1571         % If currently selected set of t-tests is for a different bin
1572         % than is currently visualized search for a t-test set on
1573         % this bin. Use the first test if more than one test has
1574         % been performed on the bin.  If no test is
1575         % found, set to None/Manual.
1576         crnt_ttest_id=n_t_tests+1;
1577         for a=1:n_t_tests,
1578             if dat.t_tests(a).bin==new_bin;
1579                 crnt_ttest_id=a;
1580                 break;
1581             end
1582         end
1583         set(dat.h_ptest,'value',crnt_ttest_id);
1584         dat=update_test(dat);
1585     end
1586     draw_waveforms(dat,1);
1587     gui_erp('update dashed lines'); %also redraws topo plot
1588 elseif strcmpi(cmnd_str,'new time limits'),
1589     %time limits
1590     plotted_times=dat.times(dat.start_pt:dat.end_pt);
1591     tme_lim=str2num(get(dat.h_timerange,'string'));
1592     if length(tme_lim)~=2,
1593         errordlg('Enter exactly two values for time range (desired Min and Max values).','gui_erp Error');
1594         set(dat.h_timerange,'string',num2str([plotted_times(1) plotted_times(end)])); %reset time limits
1595     elseif tme_lim(2)<tme_lim(1),
1596         errordlg('The second time range value must be greater than the first (enter desired Min then Max).','gui_erp Error');
1597         set(dat.h_timerange,'string',num2str([plotted_times(1) plotted_times(end)])); %reset time limits
1598     elseif (tme_lim(1)<dat.times(1)) || (tme_lim(2)>dat.times(end))
1599         errordlg(sprintf('Time range values must be between %d and %d msec.',dat.times(1),dat.times(end)),'gui_erp Error');
1600         set(dat.h_timerange,'string',num2str([plotted_times(1) plotted_times(end)])); %reset time limits
1601     else
1602         %change time limits
1603         set(dat.h_timeA,'xlim',tme_lim);
1604         set(dat.h_timeB,'xlim',tme_lim);
1605         dat.start_pt=find_tpt(tme_lim(1),dat.times);
1606         dat.end_pt=find_tpt(tme_lim(2),dat.times);
1607         
1608         %update max abs val
1609         dat.absmxA=max(max(abs(dat.showingA(:,dat.start_pt:dat.end_pt))));
1610         dat.absmxB=max(max(abs(dat.showingB(:,dat.start_pt:dat.end_pt))));
1611         current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1612             dat.times);
1613         if (current_tpt>dat.end_pt) || (current_tpt<dat.start_pt)
1614            % Time point whose topography is currently being visualized
1615            % falls outside of time range.  Reset current time point to peak
1616            % t-score latency within visualized time window.
1617            [dummy, mx_tpt]=max(max(abs(dat.showing_t(:,dat.start_pt:dat.end_pt))));
1618            showing_pts=dat.start_pt:dat.end_pt;
1619            set(dat.h_lineA,'xdata',[1 1]*dat.times(showing_pts(mx_tpt)));
1620            set(dat.h_lineB,'xdata',[1 1]*dat.times(showing_pts(mx_tpt)));
1621            
1622            set(dat.h_topo_time,'string',num2str(dat.times(showing_pts(mx_tpt))));
1623         end
1624         
1625         redraw_topos(dat,1);
1626         set(dat.fig_id,'userdata',dat);
1627     end
1628 elseif strcmpi(cmnd_str,'new statistic limitsA'),
1629     %statistic limits
1630     plotted_limits=get(dat.h_timeA,'ylim');
1631     new_lim=str2num(get(dat.h_statrangeA,'string'));
1632     if length(new_lim)~=2,
1633         errordlg('Enter exactly two values for statistic range (desired Min and Max values).','gui_erp Error');
1634         set(dat.h_statrangeA,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1635     elseif new_lim(2)<new_lim(1),
1636         errordlg('The second statistic range value must be greater than the first (enter desired Min then Max).','gui_erp Error');
1637         set(dat.h_statrangeA,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1638     else
1639         %change time line limits
1640         set(dat.h_timeA,'ylim',new_lim);
1641         set(dat.h_t0lineA,'YData',new_lim); %adjust time=0 line
1642         set(dat.h_lineA,'YData',new_lim); %adjust current time pt line
1643         
1644         %change dashed time line limits (hypothesis test window)
1645         if ~isempty(dat.h_wind1A)
1646            set(dat.h_wind1A,'YData',new_lim); 
1647            set(dat.h_wind2A,'YData',new_lim);
1648         end
1649     end
1650 elseif strcmpi(cmnd_str,'new statistic limitsB'),
1651     %statistic limits
1652     plotted_limits=get(dat.h_timeB,'ylim');
1653     new_lim=str2num(get(dat.h_statrangeB,'string'));
1654     if length(new_lim)~=2,
1655         errordlg('Enter exactly two values for statistic range (desired Min and Max values).','gui_erp Error');
1656         set(dat.h_statrangeB,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1657     elseif new_lim(2)<new_lim(1),
1658         errordlg('The second statistic range value must be greater than the first (enter desired Min then Max).','gui_erp Error');
1659         set(dat.h_statrangeB,'string',num2str([plotted_limits(1) plotted_limits(end)])); %reset statistic limits
1660     else
1661         %change time line limits
1662         set(dat.h_timeB,'ylim',new_lim);
1663         set(dat.h_t0lineB,'YData',new_lim); %adjust time=0 line
1664         set(dat.h_lineB,'YData',new_lim); %adjust current time pt line
1665         
1666         %change dashed time line limits (hypothesis test window)
1667         if ~isempty(dat.h_wind1B)
1668             set(dat.h_wind1B,'YData',new_lim);
1669             set(dat.h_wind2B,'YData',new_lim);
1670         end
1671     end
1672 elseif strcmpi(cmnd_str,'redraw topo'),
1673     redraw_topos(dat,1); %we need this command string for buttdownfcn calls
1674 elseif strcmpi(cmnd_str,'update dashed lines'),
1675     test_wind=str2num(get(dat.h_testwind,'string'));
1676     
1677     if (size(test_wind,2)>2) || (isempty(test_wind) && ~isempty(get(dat.h_testwind,'string')))
1678         errordlg('Enter pairs of values for time window range (desired Min and Max values) or leave box blank. Separate multiple pairs with semicolons.', ...
1679             'gui_erp Error');
1680     elseif length(dat.critical_t)>2,
1681         errordlg('Enter one (for one-tailed test) or two (for two-tailed test) value(s) for critical t-scores or leave box blank.', ...
1682             'gui_erp Error');
1683     else
1684         if isempty(test_wind),
1685             test_wind=[NaN NaN];
1686         end
1687         
1688         % remove existing critical value markers & test time window markers
1689         if ~isempty(dat.h_crit1),
1690             delete(dat.h_crit1);
1691         end
1692         if ~isempty(dat.h_crit2),
1693             delete(dat.h_crit2);
1694         end
1695         if ~isempty(dat.h_alph1),
1696             delete(dat.h_alph1);
1697         end
1698         if ~isempty(dat.h_alph2),
1699             delete(dat.h_alph2);
1700         end
1701         if ~isempty(dat.h_wind1A),
1702             delete(dat.h_wind1A);
1703         end
1704         if ~isempty(dat.h_wind2A),
1705             delete(dat.h_wind2A);
1706         end
1707         if ~isempty(dat.h_wind1B),
1708             delete(dat.h_wind1B);
1709         end
1710         if ~isempty(dat.h_wind2B),
1711             delete(dat.h_wind2B);
1712         end
1713         dat.h_wind1A=[];
1714         dat.h_wind2A=[];
1715         dat.h_wind1B=[];
1716         dat.h_wind2B=[];
1717         dat.h_crit1=[];
1718         dat.h_crit2=[];
1719         dat.h_alph1=[];
1720         dat.h_alph2=[];
1721         dat.n_wind=size(test_wind,1);
1722         
1723         if ~sum(isnan(test_wind))
1724             y_rngA=get(dat.h_timeA,'YLim');
1725             y_rngB=get(dat.h_timeB,'YLim');
1726             
1727             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
1728             for nw=1:dat.n_wind,
1729                 dat.h_wind1A(nw)=plot([1 1]*test_wind(nw,1),y_rngA,'k--');
1730                 set(dat.h_wind1A(nw),'linewidth',2);
1731                 dat.h_wind2A(nw)=plot([1 1]*test_wind(nw,2),y_rngA,'k--');
1732                 set(dat.h_wind2A(nw),'linewidth',2);
1733             end
1734             
1735             axes(dat.h_timeB);
1736             for nw=1:dat.n_wind,
1737                 dat.h_wind1B(nw)=plot([1 1]*test_wind(nw,1),y_rngB,'k--');
1738                 set(dat.h_wind1B(nw),'linewidth',2);
1739                 dat.h_wind2B(nw)=plot([1 1]*test_wind(nw,2),y_rngB,'k--');
1740                 set(dat.h_wind2B(nw),'linewidth',2);
1741             end
1742             
1743             %Find out if visualized units are microvolts or t-scores
1744             stat=get(dat.h_stat,'value');
1745             
1746             % if stat=1, t-scores, else data are in microvolts (for stderr)
1747             if ~sum(isnan(dat.critical_t)) && (stat==1) && ~isempty(dat.critical_t)
1748                 for nw=1:dat.n_wind,
1749                     dat.h_crit1(nw)=plot(test_wind(nw,:),[1 1]*dat.critical_t(1),'r--');
1750                     set(dat.h_crit1(nw),'linewidth',3);
1751                     if nw==1,
1752                         %write alpha value on figure
1753                         tm_rng=dat.times(end)-dat.times(1);
1754                         if strcmpi(dat.mltplcmp_crct,'fdr')
1755                             dat.h_alph1=text(test_wind(nw,1)-tm_rng*.02,dat.critical_t(1), ...
1756                                 ['q=' num2str(rnd_orderofmag(dat.alpha))]);
1757                         else
1758                             dat.h_alph1=text(test_wind(nw,1)-tm_rng*.02,dat.critical_t(1), ...
1759                                 ['\alpha=' num2str(rnd_orderofmag(dat.alpha))]);
1760                         end
1761                         set(dat.h_alph1,'color','r','fontweight','normal','fontsize',12, ...
1762                             'horizontalalignment','right','backgroundcolor',[1 1 1], ...
1763                             'edgecolor',[1 1 1]*.3,'clipping','on','fontname','fixedwidth');
1764                     end
1765                     if length(dat.critical_t)>1,
1766                         dat.h_crit2(nw)=plot(test_wind(nw,:),[1 1]*dat.critical_t(2),'r--');
1767                         set(dat.h_crit2(nw),'linewidth',3);
1768                         if nw==1,
1769                             %write alpha value on figure
1770                             if strcmpi(dat.mltplcmp_crct,'fdr')
1771                                 dat.h_alph2=text(test_wind(1)-tm_rng*.02,dat.critical_t(2), ...
1772                                     ['q=' num2str(rnd_orderofmag(dat.alpha))]);
1773                             else
1774                                 dat.h_alph2=text(test_wind(1)-tm_rng*.02,dat.critical_t(2), ...
1775                                     ['\alpha=' num2str(rnd_orderofmag(dat.alpha))]);
1776                             end
1777                             set(dat.h_alph2,'color','r','fontweight','normal','fontsize',12, ...
1778                                 'horizontalalignment','right','backgroundcolor',[1 1 1], ...
1779                                 'edgecolor',[1 1 1]*.3,'clipping','on','fontname','fixedwidth');
1780                         end
1781                     end
1782                 end
1783             end
1784         end
1785         set(dat.fig_id,'userdata',dat);
1786         redraw_topos(dat,1);
1787     end
1788 elseif strcmpi(cmnd_str,'update critical t'),
1789     critical_t=str2num(get(dat.h_critval,'string'));
1790     if ~isequal(sort(critical_t),sort(dat.critical_t)),
1791         new_alpha=[];
1792         while isempty(new_alpha),
1793             new_alpha=inputdlg({'Enter alpha level for new critical t-score(s)'}, ...
1794                 'Set New Alpha Level',1,{num2str(dat.alpha)});
1795             new_alpha=str2num(new_alpha{1});
1796             if isempty(new_alpha),
1797                 errrodlg('You must enter a numeric value for the new alpha level','gui_erp Error');
1798             end
1799         end
1800         dat.alpha=new_alpha;
1801         dat.critical_t=critical_t;
1802     end
1803     set(dat.fig_id,'userdata',dat);
1804     gui_erp('update dashed lines');
1805 elseif strcmpi(cmnd_str,'change test'),
1806     [dat, redraw_wform]=update_test(dat);    
1807     if redraw_wform,
1808         draw_waveforms(dat,1);
1809     end
1810     gui_erp('update dashed lines'); %also redraws topo
1811 else
1812     errordlg(sprintf('Command "%s" not recognized by gui_erp.m',cmnd_str), ...
1813         'gui_erp Error');
1814 end
1815 
1816 
1817 function draw_waveforms(dat,redrawA)
1818 % Redraws the channel waveforms (i.e., ERPs, t-scores of ERPs, standard error of ERPs, or GFP of ERPs)
1819 % and the color bar the topography.
1820 %
1821 % Used by: 'change bin', 'change stat', and 'change test'
1822 %
1823 % Input:
1824 %   dat     - The userdata of the GUI
1825 %   redrawA - If non-zero, axis A (the ERP waveformss) will also be redrawn
1826 
1827 new_bin=get(dat.h_bin,'value');
1828 stat=get(dat.h_stat,'value');
1829 crnt_ttest_id=get(dat.h_ptest,'value');
1830 n_t_tests=length(dat.t_tests);
1831 if crnt_ttest_id==n_t_tests+1,
1832     %no t-test results visualized
1833     dat.showing_chans=dat.loaded_chans;
1834 else
1835     dat.showing_chans=intersect(dat.t_tests(crnt_ttest_id).used_chan_ids, ...
1836         dat.loaded_chans);
1837 end
1838 
1839 %redraw Axis B time plot
1840 axes(dat.h_timeB);
1841 delete(dat.h_showingB);
1842 delete(dat.h_lineB);
1843 plotted_times=dat.times(dat.start_pt:dat.end_pt);
1844 %store t-scores for waveform that will be shown
1845 if (crnt_ttest_id<=n_t_tests) && dat.t_tests(crnt_ttest_id).null_mean
1846     %The mean of the null hypothesis is non-zero
1847     dat.showing_t=squeeze( (dat.erp(dat.showing_chans,:,new_bin)- ...
1848         dat.t_tests(crnt_ttest_id).null_mean)./dat.stder(dat.showing_chans,:,new_bin) );
1849 else
1850     dat.showing_t=squeeze(dat.t_scores(dat.showing_chans,:,new_bin));
1851 end
1852 
1853 if (stat==1),
1854     dat.showingB=dat.showing_t;
1855     cbar_title='t-score';
1856     ylab='t-score';
1857 elseif (stat==2),
1858     dat.showingB=squeeze(dat.stder(dat.showing_chans,:,new_bin));
1859     cbar_title='\muV';
1860     ylab='\muV (StdEr)';
1861 elseif (stat==3),
1862     dat.showingB=dat.gfp(:,new_bin)';
1863     cbar_title='\muV';
1864     ylab='\muV (GFP)';
1865 end
1866 if size(dat.showingB,1)==1,
1867     %only one channel being plot
1868     dat.h_showingB=plot(dat.times,dat.showingB);
1869     cbar_title='Not Applicable';
1870     cbar_title_fontsize=10;
1871 else
1872     dat.h_showingB=plot(dat.times,dat.showingB');
1873     cbar_title_fontsize=14;
1874 end
1875 set(dat.h_time_ylabB,'string',ylab);
1876 
1877 stat_mx=max(max(dat.showingB));
1878 stat_mn=min(min(dat.showingB));
1879 %Make sure new bin has data in it
1880 if isnan(stat_mx) || isnan(stat_mn)
1881    error('Bin %d does not appear to have any data in it.',new_bin); 
1882 end
1883 stat_rng=stat_mx-stat_mn;
1884 stat_plt_rng=[stat_mn-stat_rng*.02 stat_mx+stat_rng*.02];
1885 stat_plt_rng=round(stat_plt_rng*100)/100;
1886 axis([plotted_times(1) plotted_times(end) stat_plt_rng]);
1887 set(dat.h_statrangeB,'string',num2str(stat_plt_rng));
1888 set(dat.h_t0lineB,'YData',stat_plt_rng); %adjust time=0 line
1889 current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1890     dat.times);
1891 h_lineB=plot([1 1]*dat.times(current_tpt),stat_plt_rng,'k');
1892 set(h_lineB,'linewidth',2);
1893 dat.h_lineB=h_lineB;
1894 
1895 bdf_code = [ 'tmppos = get(gca, ''currentpoint'');' ...
1896     'dat=get(gcbf, ''userdata'');' ...
1897     'new_tpt=find_tpt(tmppos(1,1),dat.times);' ...
1898     'set(dat.h_lineA,''XData'',[1 1]*dat.times(new_tpt));' ...
1899     'set(dat.h_lineB,''XData'',[1 1]*dat.times(new_tpt));' ...
1900     'set(dat.h_topo_time,''string'',num2str(dat.times(new_tpt)));' ...
1901     'set(dat.fig_id,''userdata'',dat);' ...
1902     'gui_erp(''redraw topo'');' ...
1903     'drawnow;' ...
1904     'clear latpoint dattmp tmppos;' ...
1905     ];
1906 set(dat.h_showingB,'ButtonDownFcn',bdf_code);
1907 set(dat.h_timeB,'ButtonDownFcn',bdf_code);
1908 
1909 %redraw topo color bar for Axis B
1910 dat.absmxB=max(max(abs(dat.showingB(:,dat.start_pt:dat.end_pt))));
1911 axes(dat.h_cbarB);
1912 cla;
1913 cbar(dat.h_cbarB);
1914 absmx=round(dat.absmxB*100)/100;
1915 set(gca,'xticklabel',[-absmx 0 absmx]);
1916 h_cbar_title=title(cbar_title);
1917 set(h_cbar_title,'fontsize',cbar_title_fontsize);
1918 
1919 if redrawA,
1920     %redraw Axis A time plot
1921     new_title=['Bin ' int2str(new_bin) ': ' dat.bindesc{new_bin}];
1922     title_max_char=43;
1923     if length(new_title)>title_max_char,
1924         new_title=new_title(1:title_max_char);
1925     end
1926     set(dat.h_time_title,'string',new_title);
1927     
1928     
1929     %redraw Axis A time plot
1930     axes(dat.h_timeA);
1931     delete(dat.h_showingA);
1932     delete(dat.h_lineA);
1933     plotted_times=dat.times(dat.start_pt:dat.end_pt);
1934     %store t-scores for waveform that will be shown
1935     if (crnt_ttest_id<=n_t_tests) && dat.t_tests(crnt_ttest_id).null_mean
1936         %The mean of the null hypothesis is non-zero
1937         dat.showing_t=squeeze( (dat.erp(dat.showing_chans,:,new_bin)- ...
1938             dat.t_tests(crnt_ttest_id).null_mean)./dat.stder(dat.showing_chans,:,new_bin) );
1939     else
1940         dat.showing_t=squeeze(dat.t_scores(dat.showing_chans,:,new_bin));
1941     end
1942     
1943     dat.showingA=squeeze(dat.erp(dat.showing_chans,:,new_bin));
1944     cbar_title='\muV';
1945     ylab='\muV (ERP)';
1946     if size(dat.showingA,1)==1,
1947         %only one channel being plot
1948         dat.h_showingA=plot(dat.times,dat.showingA);
1949         cbar_title='Not Applicable';
1950         cbar_title_fontsize=10;
1951     else
1952         dat.h_showingA=plot(dat.times,dat.showingA');
1953     end
1954     set(dat.h_time_ylabA,'string',ylab);
1955     
1956     stat_mx=max(max(dat.showingA));
1957     stat_mn=min(min(dat.showingA));
1958     stat_rng=stat_mx-stat_mn;
1959     stat_plt_rng=[stat_mn-stat_rng*.02 stat_mx+stat_rng*.02];
1960     stat_plt_rng=round(stat_plt_rng*100)/100;
1961     axis([plotted_times(1) plotted_times(end) stat_plt_rng]);
1962     set(dat.h_statrangeA,'string',num2str(stat_plt_rng));
1963     set(dat.h_t0lineA,'YData',stat_plt_rng); %adjust time=0 line
1964     current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
1965         dat.times);
1966     h_lineA=plot([1 1]*dat.times(current_tpt),stat_plt_rng,'k');
1967     set(h_lineA,'linewidth',2);
1968     dat.h_lineA=h_lineA;
1969     
1970     bdf_code = [ 'tmppos = get(gca, ''currentpoint'');' ...
1971         'dat=get(gcbf, ''userdata'');' ...
1972         'new_tpt=find_tpt(tmppos(1,1),dat.times);' ...
1973         'set(dat.h_lineA,''XData'',[1 1]*dat.times(new_tpt));' ...
1974         'set(dat.h_lineB,''XData'',[1 1]*dat.times(new_tpt));' ...
1975         'set(dat.h_topo_time,''string'',num2str(dat.times(new_tpt)));' ...
1976         'set(dat.fig_id,''userdata'',dat);' ...
1977         'gui_erp(''redraw topo'');' ...
1978         'drawnow;' ...
1979         'clear latpoint dattmp tmppos;' ...
1980         ];
1981     set(dat.h_timeA,'ButtonDownFcn',bdf_code);
1982     set(dat.h_showingA,'ButtonDownFcn',bdf_code);
1983     
1984     %redraw topo color bar for Axis A
1985     dat.absmxA=max(max(abs(dat.showingA(:,dat.start_pt:dat.end_pt))));
1986     axes(dat.h_cbarA);
1987     cla;
1988     cbar(dat.h_cbarA);
1989     absmx=round(dat.absmxA*100)/100;
1990     set(gca,'xticklabel',[-absmx 0 absmx]);
1991     h_cbar_title=title(cbar_title);
1992     set(h_cbar_title,'fontsize',cbar_title_fontsize);
1993 end
1994 
1995 set(dat.fig_id,'userdata',dat);
1996 
1997 %note, redraw_topos is called by update dashed lines
1998 
1999 
2000 
2001 function [dat, redraw_wform]=update_test(dat)
2002 % Updates the t-test results paramters (e.g., critical t-score)
2003 % Used by: 'change test' & 'change bin'
2004 %
2005 % Inputs:
2006 %   dat - The userdata of the GUI
2007 %
2008 % Outputs:
2009 %   dat            - The revised of the GUI
2010 %   redraw_wform   - 1 if the waveforms in the GUI need to be redrawn
2011 %                    (e.g., if the bin being visualized has changed),
2012 %                    otherwise it's 0
2013 %
2014 
2015 crnt_ttest=get(dat.h_ptest,'value');
2016 
2017 n_psbl_tests=length(dat.psbl_tests);
2018 crnt_bin=get(dat.h_bin,'value');
2019 redraw_wform=0;
2020 if n_psbl_tests<crnt_ttest,
2021     %manual/no test, remove all test info/visualizations
2022     set(dat.h_testwind,'string',[]); %get rid of test wind
2023     dat.alpha=[];
2024     dat.critical_t=[];
2025     dat.mltplcmp_crct=[];
2026     set(dat.h_critval,'string',[]);
2027     
2028     if ~isequal(dat.loaded_chans,dat.showing_chans)
2029         redraw_wform=1;
2030     end    
2031 else
2032     ptest_bin=dat.t_tests(crnt_ttest).bin;
2033     if ptest_bin~=crnt_bin,
2034         redraw_wform=1;
2035         set(dat.h_bin,'value',ptest_bin);
2036     end
2037     
2038     %If the mean of the null hypothesis of the previously shown test
2039     %results differs from that of the new test results, redraw the waveform
2040     crnt_null_mean=dat.null_mean;
2041     if crnt_null_mean~=dat.t_tests(crnt_ttest).null_mean,
2042         redraw_wform=1;
2043     end
2044     dat.null_mean=dat.t_tests(crnt_ttest).null_mean;
2045     
2046     %test time window(s)
2047     test_wind=dat.t_tests(crnt_ttest).time_wind;
2048     dat.n_wind=size(test_wind,1);
2049     wind_edges=[];
2050     for nw=1:dat.n_wind,
2051         if nw==dat.n_wind,
2052             wind_edges=[wind_edges num2str(test_wind(nw,:))];
2053         else
2054             wind_edges=[wind_edges num2str(test_wind(nw,:)) '; '];
2055         end
2056     end
2057     set(dat.h_testwind,'string',wind_edges);
2058     
2059     % if test was performed including channels that are not currently
2060     % loaded, throw a warning
2061     if ~isempty(setdiff(dat.t_tests(crnt_ttest).used_chan_ids, ...
2062             dat.loaded_chans)),
2063         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');
2064     end
2065     
2066     % if test was performed on a set of channels that differs from what
2067     % is currently shown, replot the data with the 'change bin' command
2068     if ~isequal(dat.t_tests(crnt_ttest).used_chan_ids,dat.showing_chans)
2069         redraw_wform=1;
2070     end
2071     
2072     % set critical values and alpha level
2073     if isnan(dat.t_tests(crnt_ttest).estimated_alpha)
2074         dat.alpha=dat.t_tests(crnt_ttest).desired_alphaORq;
2075         dat.mltplcmp_crct='fdr';
2076     else
2077         dat.alpha=dat.t_tests(crnt_ttest).estimated_alpha;
2078         dat.mltplcmp_crct='perm';
2079     end
2080     dat.critical_t=dat.t_tests(crnt_ttest).crit_t;
2081     set(dat.h_critval,'string',num2str(dat.critical_t));
2082 end
2083 set(dat.fig_id,'userdata',dat);
2084 
2085 
2086 function redraw_topos(dat,redraw_cbars)
2087 
2088 current_tpt=find_tpt(str2double(get(dat.h_topo_time,'string')), ...
2089     dat.times);
2090 
2091 test_wind=str2num(get(dat.h_testwind,'string'));
2092 critical_t=str2num(get(dat.h_critval,'string'));
2093 
2094 if ~isempty(test_wind) && ~isempty(critical_t),
2095     %if current time point is in a test window look for channels with
2096     %sig effects
2097     crnt_ttest=get(dat.h_ptest,'value');
2098     current_tpt_in_ms=dat.times(current_tpt);
2099     n_showing=length(dat.showing_chans);
2100     sig_chans=zeros(1,n_showing);    
2101     for nw=1:dat.n_wind,
2102         if (current_tpt_in_ms>=test_wind(nw,1)) && (current_tpt_in_ms<=test_wind(nw,2)),
2103             %if isnan(critical_t) YODA
2104             if ~strcmpi(dat.mltplcmp_crct,'fdr') && (length(critical_t)==1) && isnan(critical_t) % critical_t is always the scalar NaN for cluster based permutation tests
2105                 %cluster based test
2106                 pval_tpt_id=find(dat.t_tests(crnt_ttest).used_tpt_ids==current_tpt);
2107                 if ~isempty(pval_tpt_id)
2108                     %if there's a significant electrode
2109                     sig_chans_temp=find(dat.t_tests(crnt_ttest).adj_pval(:,pval_tpt_id)<dat.t_tests(crnt_ttest).desired_alphaORq);
2110                     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
2111                     sig_chans=zeros(1,n_showing);
2112                     for a=1:n_showing,
2113                         if ismember(dat.showing_chans(a),sig_chans_temp)
2114                             sig_chans(a)=1;
2115                         end
2116                     end
2117                 end
2118             else
2119                 if length(critical_t)==2,
2120                     % I don't think one critical t value can be NaN and not
2121                     % the other, but just in case
2122                     if isnan(critical_t(2))
2123                         sig_chans=[];
2124                     else
2125                         sig_chans(dat.showing_t(:,current_tpt)>max(critical_t))=1;
2126                     end
2127                     if ~isnan(critical_t(1))
2128                         sig_chans(dat.showing_t(:,current_tpt)<min(critical_t))=1;
2129                     end
2130                 else
2131                     if isnan(critical_t) %FDR control con produce NaN critical_t's if not test are significant
2132                         sig_chans=[];
2133                     elseif critical_t>0,
2134                         sig_chans(dat.showing_t(:,current_tpt)>critical_t)=1;
2135                     else
2136                         sig_chans(dat.showing_t(:,current_tpt)<critical_t)=1;
2137                     end
2138                 end
2139             end
2140             break; %Visualized time point is in this window. Break out of for loop since there's no need to look at additional time windows
2141         end
2142     end
2143     sig_chans=find(sig_chans>0);
2144 else
2145     sig_chans=[];
2146 end
2147 
2148 % ERP TOPO
2149 set(dat.fig_id,'CurrentAxes',dat.h_topoA); %<-changes current axis without updating GUI graphics
2150 %which is why I use this instead of axes(dat.h_topoA)
2151 if size(dat.showingA,1)<=2,
2152     %two or fewer channels, we can only plot electrode locations
2153     topoplotMK(dat.showingA(:,current_tpt),dat.chanlocs(dat.showing_chans), ...
2154         'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
2155     
2156     if get(dat.h_stat,'value')==3,
2157         %GFP topo
2158         if redraw_cbars,
2159             %statistic has been changed, need to erase old topo
2160             set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2161             topoplotMK([],dat.chanlocs(dat.showing_chans), ...
2162                 'style','blank','plain_blank',1);
2163         end
2164     else
2165         % t-score/stderr
2166         set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2167         topoplotMK(dat.showingB(:,current_tpt),dat.chanlocs(dat.showing_chans), ...
2168             'style','blank','plain_blank',1,'emarker2',{sig_chans,'o',[1 1 1],4});
2169     end
2170 else
2171     topoplotMK(dat.showingA(:,current_tpt),dat.chanlocs(dat.showing_chans),'maplimits', ...
2172         [-1 1]*dat.absmxA,'emarker2',{sig_chans,'o',[1 1 1],4});
2173     set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702); %make topoplot background color match that of GUI
2174     
2175     if get(dat.h_stat,'value')==3,
2176         %GFP topo
2177         if redraw_cbars,
2178             %statistic has been changed, need to erase old topo
2179             set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2180             topoplotMK([],dat.chanlocs(dat.showing_chans), ...
2181                 'style','blank','plain_blank',1);
2182             set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702); %make topoplot background color match that of GUI
2183         end
2184     else
2185         % t-score/stderr
2186         set(dat.fig_id,'CurrentAxes',dat.h_topoB);
2187         topoplotMK(dat.showingB(:,current_tpt),dat.chanlocs(dat.showing_chans),'maplimits', ...
2188             [-1 1]*dat.absmxB,'emarker2',{sig_chans,'o',[1 1 1],4});
2189         set(findobj(gca,'type','patch'),'facecolor',[1 1 1]*.702); %make topoplot background color match that of GUI
2190     end
2191 end
2192 
2193 if (redraw_cbars)
2194     % ERP COLOR BAR TICK LABELS
2195     %axes(dat.h_cbarA);
2196     set(dat.fig_id,'CurrentAxes',dat.h_cbarA);
2197     absmx=round(dat.absmxA*100)/100;
2198     set(gca,'xticklabel',[-absmx 0 absmx]);
2199     
2200     % t-Score/StdErr/GFP COLOR BAR TICK LABELS
2201     %axes(dat.h_cbarB);
2202     set(dat.fig_id,'CurrentAxes',dat.h_cbarB);
2203     absmx=round(dat.absmxB*100)/100;
2204     set(gca,'xticklabel',[-absmx 0 absmx]);
2205 end
2206 drawnow
2207

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