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

📄 fmri_input_contrast_ui.m

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

   return;						% ShowSlider


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

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

   return;						% HideSlider


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

   ax_hdl = getappdata(gcf,'PlotAxes');
   
   curr_contrast = getappdata(gcf,'CurrContrasts'); 
   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 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'
 	      status = SaveContrasts(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);

   DisplayContrasts;
   UpdateSlider;


   return;						% ClearContrasts

   
%----------------------------------------------------------------------------
function LoadContrasts()
   
   if ~isempty(getappdata(gcf,'OldContrasts'))
      if (ChkContrastModified == 0)
         return;                        % error
      end;
   end;
   
   [filename, pathname] = uigetfile( 'PLScontrast*.mat','Load a contrast file');
        
   if isequal(filename,0) | isequal(pathname,0)
      return;
   end;
   
   contrast_file = [pathname, filename];

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

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

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

   return;						% LoadContrasts
   

%----------------------------------------------------------------------------
function LoadConditions()
   

   if (ChkContrastModified == 0)
      return;                        % error
   end;
   
   [filename, pathname] = uigetfile( '*session.mat', ...
                                'Load conditions from a PLS session file');
        
   if isequal(filename,0) | isequal(pathname,0)
      return;
   end;
   
   session_file = [pathname, filename];

   try
      pls_session = load(session_file);
      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;

   setappdata(gcf,'Conditions',conditions);

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

   ShowConditions;
   ClearContrasts;

   return;						% LoadConditions
   


%----------------------------------------------------------------------------
function status = SaveContrasts(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;

   if (rank(contrast_mat) ~= size(contrast_mat,1))
      msg = 'ERROR:  Contrasts are not lineaer independent.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
	
   end;

   if (save_as_flag == 1) | isempty(contrast_file)
      [filename, pathname] = ...
           uiputfile('PLScontrast*.mat','Save the Contrasts ');
      if isequal(filename,0)
         status = 0;
         return;
      end;
      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','EditMenu'),'Visible','off');
   set(findobj(gcf,'Tag','ConditionMenu'),'Visible','off');

   return;						% HideMenuEntries


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

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

   setappdata(gcf,'CurrContrasts',curr_contrast);

   return;						% UpdateContrastName


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

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

   % 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;

   setappdata(gcf,'CurrContrasts',curr_contrast);

   err = PlotContrast2;

   return;						% UpdateContrastValue


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

   err = 0;
   ax_hdl = getappdata(gcf,'PlotAxes');
   
   curr_contrast = getappdata(gcf,'CurrContrasts'); 
   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

⌨️ 快捷键说明

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