⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rri_input_contrast_ui.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 4 页
字号:
function DeleteContrast()

   g = get(findobj(gcf,'Tag','GroupListBox'),'value');
   CurrContrast = getappdata(gcf,'CurrContrasts');

   if ~isempty(CurrContrast) & length(CurrContrast) >= g
      curr_contrast = CurrContrast{g};
   else
      curr_contrast = [];
   end

   contrast_hdls = getappdata(gcf,'Contrast_hlist');

   row_idx = get(gcbo,'UserData');
   contrast_idx = str2num(get(contrast_hdls(row_idx,1),'String'));

   mask = ones(1,length(curr_contrast));  mask(contrast_idx) = 0;
   idx = find(mask == 1);
   curr_contrast = curr_contrast(idx);

   CurrContrast{g} = curr_contrast;
   setappdata(gcf,'CurrContrasts',CurrContrast);

   DisplayContrasts;
   UpdateSlider;

   return;						% DeleteContrast


%----------------------------------------------------------------------------
function AddContrast()

   conditions = getappdata(gcbf,'Conditions');

   if isempty(conditions),
      msg = sprintf('Cannot add contrasts without loading conditions first.');
      set(findobj(gcbf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end;

   g = get(findobj(gcf,'Tag','GroupListBox'),'value');
   CurrContrast = getappdata(gcf,'CurrContrasts');

   if ~isempty(CurrContrast) & length(CurrContrast) >= g
      curr_contrast = CurrContrast{g};
   else
      curr_contrast = [];
   end

   old_contrast = curr_contrast;		% save, in case roll back

   rows = getappdata(gcf,'NumRows');
   a_hdls = getappdata(gcf,'AddRowHdls');

   contrast_idx = str2num(get(a_hdls(1),'String'));
   contrast_name = get(a_hdls(2),'String');
   contrast_value = get(a_hdls(4),'String');

   num_contrast = length(curr_contrast)+1;

%   curr_contrast(num_contrast).name = '';
%   curr_contrast(num_contrast).value = [];

   curr_contrast(num_contrast).name = contrast_name;
   curr_contrast(num_contrast).value = contrast_value;

   CurrContrast{g} = curr_contrast;
   setappdata(gcf,'CurrContrasts',CurrContrast);

   UpdateContrastName2;
   err = UpdateContrastValue2;

   if err
      CurrContrast{g} = old_contrast;
      setappdata(gcf,'CurrContrasts',CurrContrast);	% roll back
      return;
   end

   new_contrast_row = get(a_hdls(1),'UserData');

   if (new_contrast_row == rows),  	% the new contrast row is the last row
      top_contrast_idx = getappdata(gcf,'TopContrastIdx');
      setappdata(gcf,'TopContrastIdx',top_contrast_idx+1);
   end;

   DisplayContrasts;

   contrast_hdls = getappdata(gcf,'Contrast_hlist');
   if (new_contrast_row == rows),  	% the new contrast row is the last row
      set(gcf,'CurrentObject',contrast_hdls(rows-1,2));
   else
      set(gcf,'CurrentObject',contrast_hdls(new_contrast_row,2));
   end;

   UpdateSlider;

   return;						% AddContrasts


%----------------------------------------------------------------------------
function MoveSlider()

   slider_hdl = findobj(gcf,'Tag','ContrastSlider');
   curr_value = round(get(slider_hdl,'Value'));
   total_rows = round(get(slider_hdl,'Max'));

   top_contrast_idx = total_rows - curr_value + 1;

   setappdata(gcf,'TopContrastIdx',top_contrast_idx);

   DisplayContrasts;

   return;						% MoveSlider


%----------------------------------------------------------------------------
function SetupSlider()

   contrast_hdls = getappdata(gcf,'Contrast_hlist');
   top_pos = get(contrast_hdls(1),'Position');
   bottom_pos = get(contrast_hdls(end),'Position');

   slider_hdl = findobj(gcf,'Tag','ContrastSlider');
   pos = get(slider_hdl,'Position');
   pos(2) = bottom_pos(2);
   pos(4) = top_pos(2)+top_pos(4) - pos(2);
   set(slider_hdl,'Position', pos);

   return;						% SetupSlider


%----------------------------------------------------------------------------
function UpdateSlider()

   top_contrast_idx = getappdata(gcf,'TopContrastIdx');
   rows = getappdata(gcf,'NumRows');

   g = get(findobj(gcf,'Tag','GroupListBox'),'value');
   CurrContrast = getappdata(gcf,'CurrContrasts');

   if ~isempty(CurrContrast) & length(CurrContrast) >= g
      curr_contrast = CurrContrast{g};
   else
      curr_contrast = [];
   end

   num_contrast = length(curr_contrast);

   total_rows = num_contrast+1;
   slider_hdl = findobj(gcf,'Tag','ContrastSlider');

   if (num_contrast ~= 0)		% don't need to update when no contrast
      set(slider_hdl,'Min',1,'Max',total_rows, ...
                  'Value',total_rows-top_contrast_idx+1, ...
                  'Sliderstep',[1/(total_rows-1)-0.00001 1/(total_rows-1)]); 
   end;
   
   return;						% UpdateSlider


%----------------------------------------------------------------------------
function ShowSlider()

   slider_hdl = findobj(gcf,'Tag','ContrastSlider');
   set(slider_hdl,'visible','on'); 

   return;						% ShowSlider


%----------------------------------------------------------------------------
function HideSlider()

   slider_hdl = findobj(gcf,'Tag','ContrastSlider');
   set(slider_hdl,'visible','off'); 

   return;						% HideSlider


%----------------------------------------------------------------------------
function PlotContrast()

   ax_hdl = getappdata(gcf,'PlotAxes');
   
   g = get(findobj(gcf,'Tag','GroupListBox'),'value');
   CurrContrast = getappdata(gcf,'CurrContrasts');

   if ~isempty(CurrContrast) & length(CurrContrast) >= g
      curr_contrast = CurrContrast{g};
   else
      curr_contrast = [];
   end

   contrast_hdls = getappdata(gcf,'Contrast_hlist');
         
   row_idx = get(gcbo,'UserData');
   contrast_idx = str2num(get(contrast_hdls(row_idx,1),'String'));

   contrast_name = curr_contrast(contrast_idx).name;
   contrast_values = curr_contrast(contrast_idx).value;

   if isempty(contrast_values)
	return;
   end;

   axes(ax_hdl);

   min_x = 0.4;
   max_x = length(contrast_values)+0.6;
   min_y = min(contrast_values) - 0.2;
   max_y = max(contrast_values) + 0.2;

   bar(contrast_values);

   set(ax_hdl,'xgrid','on', 'ygrid','on',...
	      'XTick',[1:length(contrast_values)], ...
	      'XLim', [min_x max_x], 'YLim', [min_y max_y]);
   title(contrast_name);

   return;						% PlotContrast


%----------------------------------------------------------------------------
function ShowGroups()

   num_groups = getappdata(gcf,'num_groups');

   h = findobj(gcf,'Tag','GroupListBox');
  
   group_str = cell(1,num_groups);
   for i=1:num_groups,
      group_str{i} = sprintf('%3d. %s %d', i, 'Group', i);
   end;
   set(h,'String',group_str);

   return;						% ShowGroups


%----------------------------------------------------------------------------
function ShowConditions()

   conditions = getappdata(gcf,'Conditions');
   num_conds = length(conditions);

   h = findobj(gcf,'Tag','ConditionListBox');
  
   cond_str = cell(1,num_conds);
   for i=1:num_conds,
      cond_str{i} = sprintf('%3d. %s',i,conditions{i});
   end;
   set(h,'String',cond_str);

   return;						% ShowConditions


%----------------------------------------------------------------------------
function status = ChkContrastModified()
%  status = 0  for cancel
%  status = 1  for ok
%

   status = 1;

   curr_contrasts = getappdata(gcf,'CurrContrasts');
   old_contrasts = getappdata(gcf,'OldContrasts');

   if (isequal(curr_contrasts,old_contrasts) == 0),    
      dlg_title = 'Session Information has been changed';
      msg = 'WARNING: The contrasts have been changed.  Do you want to save it?';
      response = questdlg(msg,dlg_title,'Yes','No','Cancel','Yes');

      switch response,
         case 'Yes'
              prefix=getappdata(gcf,'prefix');
 	      status = SaveText(prefix,0);		
         case 'Cancel'
 	      status = 0;
      end; 
   end;

   return;						% ChkContrastModified


%----------------------------------------------------------------------------
function ClearContrasts()

   ax_hdl = getappdata(gcf,'PlotAxes');
   axes(ax_hdl);
   cla;

   setappdata(gcf,'OldContrasts',[]);
   setappdata(gcf,'CurrContrasts',[]);
   setappdata(gcf,'TopContrastIdx',1);

   setappdata(gcf,'ContrastFile','');
   set(gcf,'Name','New Contrasts');

   DisplayContrasts;
   UpdateSlider;

   return;						% ClearContrasts


%----------------------------------------------------------------------------
function LoadContrasts(design)
   
   if ~isempty(getappdata(gcf,'OldContrasts'))
      if (ChkContrastModified == 0)
         return;                        % error
      end;
   end;

   conditions = getappdata(gcf,'Conditions');
   num_cond = length(conditions);
   num_groups = getappdata(gcf,'num_groups');

   for g = 1:num_groups
      span = [((g-1)*num_cond+1) : g*num_cond];
      contrast_text = design(span, :);

      for i = 1:size(contrast_text,2)
         contrast_info.pls_contrasts(i).name = ['Contrast ', num2str(i)];
         contrast_info.pls_contrasts(i).value = contrast_text(:,i)';
      end

      old_contrast{g} = contrast_info.pls_contrasts;
   end

   setappdata(gcf,'OldContrasts',old_contrast);
   setappdata(gcf,'CurrContrasts',old_contrast);
%   setappdata(gcf,'Conditions',contrast_info.conditions);
   setappdata(gcf,'TopContrastIdx',1);
   setappdata(gcf,'ContrastFile','');

   set(gcf,'Name','Loaded Contrasts');

   return;						% LoadContrasts


%----------------------------------------------------------------------------
function LoadConditions(prefix,pls_session,cond_selection)

   if (ChkContrastModified == 0)
      return;                        % error
   end;

   if ~isempty(pls_session) & iscell(pls_session)
      conditions = pls_session;
   else
      if isempty(pls_session)   
         [filename, pathname] = uigetfile( ['*_',prefix,'session.mat'], ...
                                'Load conditions from a session file');
        
         if isequal(filename,0) | isequal(pathname,0)
            return;
         end;
   
         pls_session = [pathname, filename];
      end

      try
         pls_session = load(pls_session);
         conditions = pls_session.session_info.condition;
      catch
         msg = 'ERROR: Cannot load the conditions from the session file.';
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
         return;
      end;
   end

   if ~isempty(cond_selection)
      conditions = conditions(find(cond_selection));
   end

   setappdata(gcf,'Conditions',conditions);
   set(gcf,'Name','New Contrasts');

   ShowGroups;
   ShowConditions;
   ClearContrasts;

   return;						% LoadConditions   


%----------------------------------------------------------------------------
function status = SaveContrasts(prefix,save_as_flag)
%  save_as_flag = 0,  save to the loaded file
%  save_as_flag = 1,  save to a new file
%
   if ~exist('save_as_flag','var')
     save_as_flag = 0;
   end;

   pls_contrasts = getappdata(gcf,'CurrContrasts');
   conditions = getappdata(gcf,'Conditions');
   contrast_file = getappdata(gcf,'ContrastFile');

   if isempty(pls_contrasts),
      msg = 'ERROR: No contrast available to be saved.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   num_conds = length(conditions);
   num_contrasts = length(pls_contrasts);

   for i=1:num_contrasts,
      if (isempty(pls_contrasts(i).name))
         msg = 'ERROR: All contrasts must have name specified.';
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
	 return;
      end;

      if (isempty(pls_contrasts(i).value))
         msg = 'ERROR: All contrasts must have values specified.';
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
	 return;
      end;
   end;

   contrast_mat = []; 

   for i=1:num_contrasts;
	if ~isempty(pls_contrasts(i).value) 
           contrast_mat = [contrast_mat; pls_contrasts(i).value];
	end;
   end;

   %  check if the contrast matrix is rank deficient
   %
   if (rank(contrast_mat) ~= size(contrast_mat,1))
      msg = 'Your Contrast matrix is rank deficient. Please check your contrasts and run the program again';
      uiwait(msgbox(msg,'Warning: Contrasts are not linear independent','modal'));
%      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
%      return;	
   end;

   %  check if the contrast matrix is orthogonal
   %
%   if abs(sum(sum(contrast_mat*contrast_mat'-diag(diag(contrast_mat*contrast_mat'))))) > 1e-6
   check_orth = abs(triu(contrast_mat*contrast_mat') - tril(contrast_mat*contrast_mat'));
   if max(check_orth(:)) > 1e-6
      msg = 'Effects expressed by each contrast are not independent. Check variable lvintercorrs in result file to see overlap of effects between LVs';
      uiwait(msgbox(msg,'Warning: Contrasts are not orthogonal','modal'));
%      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
%      return;	
   end

   if (save_as_flag == 1) | isempty(contrast_file)
      [filename, pathname] = ...
           uiputfile( ['*_',prefix,'contrast.mat'], ...
		'Save the Contrasts ');

      if ischar(filename) & (length(filename)<9 | isempty(findstr(lower(filename),'contrast')))
         [tmp filename] = fileparts(filename);
         filename = [filename, '_', prefix, 'contrast.mat'];
      end

      if isequal(filename,0)
         status = 0;
         return;
      end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -