Home > matlabmk > gui_svar_cdf.m

gui_svar_cdf

PURPOSE ^

gui_svar_cdf() - Creates a GUI for visualizing and exploring the

SYNOPSIS ^

function gui_svar_cdf(command_str,eim_fname,sortvar_name,bins,jitter)

DESCRIPTION ^

 gui_svar_cdf() - Creates a GUI for visualizing and exploring the
                  cumulative distribution of an ERPimage sorting variable.
              
 Usage:
  >> gui_svar_cdf(command_str,eim_fname,sortvar_name,bins,jitter)

 Required Inputs:
   command_str - One of two strings:
        'initialize' = Creates a new GUI and plots the cumulative
                       distribution of the sorting variable.  Requires the
                       four other arguments.
        'new_lim'    = Updates the limits on the range of sorting variable
                       values shown in the GUI.  No additional inputs are
                       used for this command_str.

 Inputs Required for Initialization
   eim_fname    - The name of a file of concatenated single trials used for
                  creating ERPimages.  eim files are produced via the function
                  compile_erpimage.m.
   sortvar_name - The name of the sorting variable whose cumalitive
                  distribution will be plot (e.g., 'rtmsec').
   bins         - A string that specifies a vector of bins.  Only trials that
                  fall into these bins will be used.  Specifying a non-numeric 
                  string like 'All' will result in all bins being used.

 Optional Input
   jitter - [0 | 1] If 1, the GUI will be created up and to the right of
            the current figure (to ensure that the new GUI doesn't obscure
            existing GUIs).  If no figure is already open, a new figure will be
            created (in addition to the cdf GUI).  If 0, the GUI is created at 
            the default new figure location. {default=0} 

 Notes: 
 -For an example, open a GUI with gui_erpimage.m and press the button
 'Sorting Var Distribution.'

 Author: 
 David Groppe
 Kutaslab, 10/2009

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function gui_svar_cdf(command_str,eim_fname,sortvar_name,bins,jitter)
0002 % gui_svar_cdf() - Creates a GUI for visualizing and exploring the
0003 %                  cumulative distribution of an ERPimage sorting variable.
0004 %
0005 % Usage:
0006 %  >> gui_svar_cdf(command_str,eim_fname,sortvar_name,bins,jitter)
0007 %
0008 % Required Inputs:
0009 %   command_str - One of two strings:
0010 %        'initialize' = Creates a new GUI and plots the cumulative
0011 %                       distribution of the sorting variable.  Requires the
0012 %                       four other arguments.
0013 %        'new_lim'    = Updates the limits on the range of sorting variable
0014 %                       values shown in the GUI.  No additional inputs are
0015 %                       used for this command_str.
0016 %
0017 % Inputs Required for Initialization
0018 %   eim_fname    - The name of a file of concatenated single trials used for
0019 %                  creating ERPimages.  eim files are produced via the function
0020 %                  compile_erpimage.m.
0021 %   sortvar_name - The name of the sorting variable whose cumalitive
0022 %                  distribution will be plot (e.g., 'rtmsec').
0023 %   bins         - A string that specifies a vector of bins.  Only trials that
0024 %                  fall into these bins will be used.  Specifying a non-numeric
0025 %                  string like 'All' will result in all bins being used.
0026 %
0027 % Optional Input
0028 %   jitter - [0 | 1] If 1, the GUI will be created up and to the right of
0029 %            the current figure (to ensure that the new GUI doesn't obscure
0030 %            existing GUIs).  If no figure is already open, a new figure will be
0031 %            created (in addition to the cdf GUI).  If 0, the GUI is created at
0032 %            the default new figure location. {default=0}
0033 %
0034 % Notes:
0035 % -For an example, open a GUI with gui_erpimage.m and press the button
0036 % 'Sorting Var Distribution.'
0037 %
0038 % Author:
0039 % David Groppe
0040 % Kutaslab, 10/2009
0041 
0042 %Possible Future Improvements:
0043 %-Make the cdf plot clickable to change boundaries on the range of
0044 %highlighted values.
0045 %
0046 
0047 if nargin==0,
0048     help gui_svar_cdf
0049     error('Function gui_svar_cdf requires at least one argment.');
0050 end
0051 
0052 if nargin<5,
0053     jitter=0;
0054 end
0055 
0056 if ~strcmpi(command_str,'initialize')
0057     h_fig = gcf;
0058     if ~strcmp(get(h_fig,'tag'),'gui_svar_cdf')
0059         % If the current figure does not have the right
0060         % tag, find one that does.
0061         h_figs = get(0,'children');
0062         h_fig = findobj(h_figs,'flat',...
0063             'tag','gui_svar_cdf');
0064         if isempty(h_fig)
0065             % If gui_svar_cdf does not exist
0066             % initialize it. Then run the command string
0067             % that was originally requested.
0068             if nargin<4 || nargin>5,
0069                 help gui_svar_cdf
0070                 error('You need to provide gui_svar_cdf four or five arguments when the GUI window is not already created.');
0071             else
0072                 gui_svar_cdf('initialize',eim_fname,sortvar_name,bins,jitter);
0073                 gui_svar_cdf(command_str,eim_fname,sortvar_name,bins,jitter);
0074                 return;
0075             end
0076         end
0077     end
0078     tmp=get(h_fig,'userdata');
0079 end
0080 
0081 if strcmpi(command_str,'initialize'),
0082     if nargin<4 || nargin>5,
0083         help gui_svar_cdf
0084         error('You need to provide gui_svar_cdf four or five arguments when the GUI window is not already created.');
0085     else
0086         %load eim_file
0087         load(eim_fname,'-MAT');
0088         
0089         %Extract data from specified bins and sorting variable from epoch info
0090         n_ep=length(ep_info);
0091         srt_val=zeros(1,n_ep)*NaN;
0092         
0093         if strcmpi(sortvar_name,'rtmsec'),
0094             sortby_rt=1;
0095         else
0096             sortby_rt=0;
0097         end
0098         
0099         %make bins numeric (it will be empty if a string like 'All')
0100         bins=str2num(bins);
0101         
0102         %note, srt_val indicates which epochs are to be ignored with NaN values
0103         for a=1:n_ep,
0104             %%%%%%  use a subset of bins  %%%%%%
0105             if ~isempty(bins),
0106                 got_it=0;
0107                 if sortby_rt,
0108                     %make sure the RTs are the same for all bins
0109                     %preallocate memory
0110                     rt_ids=zeros(1,length(bins));
0111                     rt_ids_ct=0;
0112                     for d=bins,
0113                         rt_id=find(rtbins{a}==d);
0114                         if ~isempty(rt_id),
0115                             rt_ids_ct=rt_ids_ct+1;
0116                             rt_ids(rt_ids_ct)=rt_id;
0117                         end
0118                     end
0119                     if rt_ids_ct>0,
0120                         uni_rt=unique(rtmsec{a}(rt_ids(1:rt_ids_ct)));
0121                         if length(uni_rt)>1,
0122                             error(sprintf(['Different bins have different RTs for epoch %d.\n' ...
0123                                 'You must specify bins that have the same RTs.'],a));
0124                         else
0125                             srt_val(a)=uni_rt; %get the RT
0126                             got_it=1;
0127                         end
0128                     end
0129                 else %non-RT sorting variable
0130                     for b=bins,
0131                         for c=1:length(ep_info(a).eventtype),
0132                             if strcmpi(ep_info(a).eventtype{c},['bin' int2str(b)]), %does this trial fall in a desired bin?
0133                                 cmnd=['srt_val(a)=ep_info(a).event' sortvar_name ';'];
0134                                 eval(cmnd);
0135                                 got_it=1;
0136                                 break; %break c loop
0137                             end
0138                         end
0139                         if got_it,
0140                             break; %break b loop
0141                         end
0142                     end
0143                 end
0144                 if ~got_it, %epoch not a member of desired bin
0145                     srt_val(a)=NaN;
0146                 end
0147                 
0148                 %%%%%%  no bins specified, use all bins  %%%%%%
0149             else
0150                 if sortby_rt,
0151                     uni_rt=unique(rtmsec{a});
0152                     if length(uni_rt)>1,
0153                         error(sprintf(['Different bins have different RTs for epoch %d.\n' ...
0154                             'You must specify bins that have the same RTs.'],a));
0155                     else
0156                         srt_val(a)=uni_rt;
0157                     end
0158                 else
0159                     cmnd=['srt_val(a)=ep_info(a).event' sortvar_name ';'];
0160                     eval(cmnd);
0161                 end
0162             end
0163         end
0164         
0165         %pad sortvar_name with some spaces so end of xlabel doesn't get cutoff
0166         sortvar_name=[sortvar_name '   '];
0167            
0168         use_id=find(~isnan(srt_val));
0169         srt_val=srt_val(use_id);
0170         uni=unique(srt_val);
0171         n_uni=length(uni);
0172         cumu=zeros(1,n_uni);
0173         for a=1:n_uni,
0174             cumu(a)=mean(srt_val<=uni(a));
0175         end
0176         col=[1 1 1]*.7;
0177         if jitter,
0178             pos=get(gcf,'position');
0179             h_fig = figure('name','Sort Var Cumulative Distribution GUI','tag','gui_erpimage');
0180             set(h_fig,'color',col,'position',[pos(1:2)*1.1 pos(3:4)]);
0181         else
0182             h_fig = figure('name','Sort Var Cumulative Distribution GUI','tag','gui_erpimage');
0183             set(h_fig,'color',col);
0184         end
0185         
0186         set(gcf,'tag','gui_svar_cdf');
0187         %frame
0188         h_panel=uipanel(h_fig,...
0189             'Units','normalized', ...
0190             'Position',[ .1 .3 .85 .65],...
0191             'shadowcolor','k', ...
0192             'bordertype','line', ...
0193             'borderwidth',1, ...
0194             'highlightcolor',col, ...
0195             'foregroundcolor',col, ...
0196             'backgroundcolor',col);
0197         %bordertype and borderwidth do nothing as far as I can tell
0198         
0199         tmp.h_ax=axes('position',[.1 .3 .85 .65],'box','on');
0200         set(tmp.h_ax,'color',col,'fontname','helvetica');
0201         
0202         %frame
0203         tmp.h_frame_1 = uicontrol(h_fig,...
0204             'Units','normalized', ...
0205             'Position',[ 0 0 1 0.2 ],...
0206             'backgroundcolor',col, ...
0207             'Style','frame');
0208         
0209         plot(uni,cumu,'.-'); hold on;
0210         v=axis;
0211         plot([v(1) uni(1)],[0 cumu(1)],'.-');
0212         lq=prctile(srt_val,25);
0213         uq=prctile(srt_val,75);
0214         tmp.mn=lq;
0215         tmp.mx=uq;
0216         tmp.h_shade=ShadePlotForEmphasis([lq uq],'r',.5);
0217         set(gca,'xgrid','on','ygrid','on');
0218         hy=ylabel('Proportion of Observations<=x Value   ');
0219         set(hy,'fontsize',12,'fontweight','bold');
0220         hx=xlabel(sortvar_name);
0221         set(hx,'fontsize',12,'fontweight','bold');
0222         
0223         %x-min text box
0224         uicontrol(h_fig,...
0225             'Units','normalized', ...
0226             'Position',[ 0.055 0.11 0.16 0.07 ],...
0227             'String','Min x-Value:',...
0228             'fontsize',12, ...
0229             'tooltipstring',sprintf('Minimum value of %s to consider.',sortvar_name), ...
0230             'Style','text');
0231         tmp.h_mn = uicontrol(h_fig,...
0232             'CallBack','[dmy h_fig]=gcbo; tmp=get(h_fig,''userdata''); gui_svar_cdf(''new_lim'');',...
0233             'Units','normalized', ...
0234             'Position',[ 0.215 0.125 0.14 0.07 ],...
0235             'String',num2str(lq),...
0236             'fontsize',12, ...
0237             'tooltipstring',sprintf('Minimum value of %s to consider.',sortvar_name), ...
0238             'backgroundcolor','w', ...
0239             'Style','edit');
0240         
0241         
0242         %x-max text box
0243         uicontrol(h_fig,...
0244             'Units','normalized', ...
0245             'Position',[ 0.405 0.11 0.16 0.07 ],...
0246             'String','Max x-Value:',...
0247             'fontsize',12, ...
0248             'tooltipstring',sprintf('Maximum value of %s to consider.',sortvar_name), ...
0249             'Style','text');
0250         tmp.h_mx = uicontrol(h_fig,...
0251             'CallBack','[dmy h_fig]=gcbo; tmp=get(h_fig,''userdata''); gui_svar_cdf(''new_lim'');',...
0252             'Units','normalized', ...
0253             'Position',[ 0.565 0.125 0.14 0.07 ],...
0254             'String',num2str(uq),...
0255             'fontsize',12, ...
0256             'tooltipstring',sprintf('Maximum value of %s to consider.',sortvar_name), ...
0257             'backgroundcolor','w', ...
0258             'Style','edit');
0259         
0260         
0261         %Report # of trials
0262         tmp.h_ntrials = uicontrol(h_fig,...
0263             'Units','normalized', ...
0264             'Position',[ 0.05 0.05 0.7 0.07 ],...
0265             'String',sprintf('Number of trials between x-min and x-max (inclusive): %d',sum( (srt_val>=lq).*(srt_val<=uq) )),...
0266             'fontsize',12, ...
0267             'Style','text');
0268         
0269         %Report proportion of trials
0270         tmp.h_prop = uicontrol(h_fig,...
0271             'Units','normalized', ...
0272             'Position',[ 0.017 0.002 0.8 0.07 ],...
0273             'String',sprintf('Proportion of trials between x-min and x-max (inclusive): %.2f',mean( (srt_val>=lq).*(srt_val<=uq) )),...
0274             'fontsize',12, ...
0275             'horizontalalignment','center', ...
0276             'Style','text');
0277         
0278         
0279         
0280         % Button to close GUI
0281         uicontrol(h_fig,...
0282             'CallBack','close(gcf);',...
0283             'Units','normalized', ...
0284             'Position',[ 0.8 0.02 0.17 0.16 ],...
0285             'String','Close',...
0286             'fontsize',14, ...
0287             'Tag','cancel', ...
0288             'ToolTipString','Close GUI', ...
0289             'Style','pushbutton');
0290         
0291         
0292         %store data with figure
0293         tmp.srt_val=srt_val;
0294         tmp.bins=bins;
0295         tmp.eim_fname=eim_fname;
0296         tmp.sortvar_name=sortvar_name;
0297         set(gcf,'userdata',tmp);
0298     end
0299 elseif strcmpi(command_str,'new_lim')
0300     mx=str2double(get(tmp.h_mx,'string'));
0301     mn=str2double(get(tmp.h_mn,'string'));
0302     if mn>mx,
0303         errordlg('x-min value needs to be less or equal to than x-max value. Resetting max and min to previous values.');
0304         set(tmp.h_mx,'string',num2str(tmp.mx));
0305         set(tmp.h_mn,'string',num2str(tmp.mn));
0306     else
0307         tmp.mn=mn;
0308         tmp.mx=mx;
0309         set(tmp.h_shade,'xdata',[mn mn mx mx]');
0310         nt_str=sprintf('Number of trials between x-min and x-max (inclusive): %d',sum( (tmp.srt_val>=mn).*(tmp.srt_val<=mx) ));
0311         set(tmp.h_ntrials,'string',nt_str);
0312         prop_str=sprintf('Proportion of trials between x-min and x-max (inclusive): %.2f',mean( (tmp.srt_val>=mn).*(tmp.srt_val<=mx) ));
0313         set(tmp.h_prop,'string',prop_str);
0314         set(gcf,'userdata',tmp);
0315     end
0316 else
0317     help gui_svar_cdf
0318     error('gui_svar_cdf does not recognize the command "%s".',command_str);
0319 end

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