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

📄 rri_input_contrast_ui.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 4 页
字号:
      contrast_file = fullfile(pathname,filename);
   end;

   try
      save (contrast_file, 'pls_contrasts', 'conditions');
   catch
      msg = sprintf('Cannot save contrasts to %s',contrast_file),
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      status = 0;
      return;
   end;

   [fpath, fname, fext] = fileparts(contrast_file);
   msg = sprintf('Contrasts have been saved into ''%s'' ',[fname, fext]);
   set(findobj(gcf,'Tag','MessageLine'),'String',msg);

   setappdata(gcf,'ContrastFile',contrast_file);
   setappdata(gcf,'OldContrasts',pls_contrasts);

   set(gcf,'Name',['Contrast File: ' contrast_file]);

   status = 1;

   return;                                              % SaveContrasts


%----------------------------------------------------------------------------
function status = CloseContrastInput()

   status = ChkContrastModified; 

   return;                                              % CloseContrastInput


%----------------------------------------------------------------------------
function status = DeleteFigure()

    link_figure = getappdata(gcbf,'LinkFigureInfo');

    try 
       rmappdata(link_figure.hdl,link_figure.name);
    end;

    try
       load('pls_profile');
       pls_profile = which('pls_profile.mat');

       fmri_input_contrast_pos = get(gcbf,'position');

       save(pls_profile, '-append', 'fmri_input_contrast_pos');
    catch
    end

    return;                                              % DeleteFigure


%----------------------------------------------------------------------------
function num_str = Number2String(numbers)
 
   if isempty(numbers),
      num_str = '';
      return;
   end;

   len = length(numbers);
   num = numbers(:);		% make sure it is a column vector;

   tmp_str = strjust(num2str(num),'left');
   num_str = deblank(tmp_str(1,:));
   for i=2:len,
      num_str = [num_str ' ' deblank(tmp_str(i,:))];
   end;

   return;						% Number2String


%----------------------------------------------------------------------------
function HideMenuEntries()

   set(findobj(gcf,'Tag','LoadContrasts'),'Visible','off');
   set(findobj(gcf,'Tag','SaveContrasts'),'Visible','off');
   set(findobj(gcf,'Tag','SaveAsContrasts'),'Visible','off');

   set(findobj(gcf,'Tag','LoadText'),'Visible','off');
   set(findobj(gcf,'Tag','SaveText'),'Visible','off');
   set(findobj(gcf,'Tag','SaveAsText'),'Visible','off');
   set(findobj(gcf,'Tag','HelmertMatrix'),'Visible','off');
   set(findobj(gcf,'Tag','CloseContrasts'),'Separator','off');

   set(findobj(gcf,'Tag','EditMenu'),'Visible','off');
   set(findobj(gcf,'Tag','ConditionMenu'),'Visible','off');

   return;						% HideMenuEntries


%----------------------------------------------------------------------------
function UpdateContrastName2(contrast_idx)

   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');
   a_hdls = getappdata(gcf,'AddRowHdls');

   row_idx = get(a_hdls(2),'UserData');
   contrast_idx = str2num(get(a_hdls(1),'String'));
   curr_contrast(contrast_idx).name = deblank(get(a_hdls(2),'String'));

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

   return;						% UpdateContrastName


%----------------------------------------------------------------------------
function err = UpdateContrastValue2(contrast_idx)

   err = 0;

   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

   a_hdls = getappdata(gcf,'AddRowHdls');
   conditions = getappdata(gcf,'Conditions');

   row_idx = get(a_hdls(4),'UserData');
   contrast_idx = str2num(get(a_hdls(1),'String'));
   contrast_values = str2num(get(a_hdls(4),'String'));

   if isempty(contrast_values)
      msg = 'ERROR: Invalid contrast values.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      err = 1;
      return;
   end;

   if length(conditions) ~= length(contrast_values),
      msg = 'ERROR: The number of contrast values does not match the number of conditions.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      err = 1;
      return;
   end;

   curr_contrast(contrast_idx).value = contrast_values;

if 0	% verify before save
   % verify the contrasts are linear independent
   %
   num_contrasts = length(curr_contrast);
   contrast_mat = []; 
   for i=1:num_contrasts;
     if ~isempty(curr_contrast(i).value) 
        contrast_mat = [contrast_mat; curr_contrast(i).value];
     end;
   end;

   if (rank(contrast_mat) ~= size(contrast_mat,1))
      msg = 'ERROR:  The specified contrast is not linear independent to the others.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      err = 1;
      return;
   end;
end

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

   err = PlotContrast2;

   return;						% UpdateContrastValue


%----------------------------------------------------------------------------
function err = PlotContrast2()

   err = 0;
   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

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

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

   if isempty(contrast_values)
        err = 1;
	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 LoadText(prefix)
   
   if ~isempty(getappdata(gcf,'OldContrasts'))
      if (ChkContrastModified == 0)
         return;                        % error
      end;
   end;

   [filename, pathname] = uigetfile( ['*_',prefix,'contrast.txt'], ...   
					'Load contrast from text file');
        
   if isequal(filename,0) | isequal(pathname,0)
      return;
   end;
   
   contrast_file = [pathname, filename];

   try
      design = load(contrast_file);
   catch
      msg = 'ERROR: Cannot load the contrast file.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

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

   if (num_groups * num_cond) ~= size(design,1)
      msg = 'ERROR: rows of contrast file not equal to [num_of_group * number_of_condition]';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end

   old_contrast = cell(1,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,'ContrastFile',contrast_file);
   setappdata(gcf,'TopContrastIdx',1);

   set(gcf,'Name',['Contrast File: ' contrast_file]);

   return;						% LoadText


%----------------------------------------------------------------------------
function status = SaveText(prefix,save_as_flag)
%  save_as_flag = 0,  save to the loaded file
%  save_as_flag = 1,  save to a new file
%

   status = 0;

   if ~exist('save_as_flag','var')
     save_as_flag = 0;
   end;

   pls_contrasts = getappdata(gcf,'CurrContrasts');
   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;

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

   if length(pls_contrasts) ~= num_groups
      msg = 'ERROR: All groups must have contrasts specified.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   g_contrast_mat = []; 

   for g = 1:num_groups

      if (g > 1) & (length(pls_contrasts{g}) ~= length(pls_contrasts{g-1}))
         msg = 'ERROR: All groups must have same numbers of contrasts.';
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
         return;
      end

      num_contrasts = length(pls_contrasts{g});
      contrast_mat = []; 

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

         if ~isempty(pls_contrasts{g}(i).value) 
            contrast_mat = [contrast_mat; pls_contrasts{g}(i).value];
	 end;
      end;

      g_contrast_mat = [g_contrast_mat contrast_mat];
   end

   %  check if the contrast matrix is rank deficient
   %
   if (rank(g_contrast_mat) ~= size(g_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(g_contrast_mat*g_contrast_mat'-diag(diag(g_contrast_mat*g_contrast_mat'))))) > 1e-6
   check_orth = abs(triu(g_contrast_mat*g_contrast_mat') - tril(g_contrast_mat*g_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.txt'], ...
		'Save contrasts to text file');

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

      if isequal(filename,0)
         return;
      end;

      contrast_file = fullfile(pathname,filename);
   end;

   contrast_text = double(g_contrast_mat');

   try
      save(contrast_file, '-ascii', 'contrast_text');
   catch
      msg = sprintf('Cannot save contrasts to %s',contrast_file),
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end;

   [fpath, fname, fext] = fileparts(contrast_file);
   msg = sprintf('Contrasts have been saved into ''%s'' ',[fname, fext]);
   set(findobj(gcf,'Tag','MessageLine'),'String',msg);

   setappdata(gcf,'ContrastFile',contrast_file);
   setappdata(gcf,'OldContrasts',pls_contrasts);

   set(gcf,'Name',['Contrast File: ' contrast_file]);

   status = 1;

   return;                                              % SaveText


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

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

   contrast_text = rri_helmert_matrix(length(conditions));
%   contrast_text = fliplr(rri_helmert_matrix(length(conditions)));
%   contrast_text = flipud(rri_helmert_matrix(length(conditions)));

   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

   CurrContrast{g} = contrast_info.pls_contrasts;

%   setappdata(gcf,'OldContrasts',contrast_info.pls_contrasts);
   setappdata(gcf,'CurrContrasts',CurrContrast);
%   setappdata(gcf,'Conditions',contrast_info.conditions);

%   setappdata(gcf,'ContrastFile','');
%   setappdata(gcf,'TopContrastIdx',1);

%   set(gcf,'Name','New Contrasts');

   return;						% HelmertMatrix


%----------------------------------------------------------------------------
function ChangeGroup

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

   set(findobj(gcf,'Tag','ContrastTitleLabel'), ...
	'String',['Contrasts for Group ', num2str(g), ': ']);

   DisplayContrasts;
   UpdateSlider;

   return;						% ChangeGroup

⌨️ 快捷键说明

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