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

📄 struct_analysis_ui.m

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

   num_groups = length(group_profiles);

   cell_buffer = [];
   for i=1:num_groups,
      cell_buffer = [cell_buffer; group_profiles(i)];
   end;

   % check for duplicated profile
   %
   total_profiles = length(cell_buffer);
   for i=1:total_profiles-1,
      for j=i+1:total_profiles,
         if isequal(cell_buffer{i},cell_buffer{j}),
            [p_path, p_name, p_ext] = fileparts(cell_buffer{i});
            msg = sprintf('"%s" has been used for more than 1 group',p_name);
            set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
            total_profiles = -1;
            return;
         else
            set(findobj(gcf,'Tag','MessageLine'),'String','');
         end;
      end;
   end;


   % make sure all conditions are the same
   %
   s = load(cell_buffer{1},'session_info');
   prev_condition = s.session_info.condition;
   prev_select = getappdata(gcf,'cond_selection');
   for i=2:total_profiles,
      s = load(cell_buffer{i},'session_info');
      curr_condition = s.session_info.condition;
      curr_select = getappdata(gcf,'cond_selection');
      if ~isequal(curr_condition,prev_condition)...
	 | ~isequal(curr_select, prev_select)
          [p_path, p_name, p_ext] = fileparts(cell_buffer{i});
          msg = sprintf('Incompatible conditions found in "%s".',p_name);
          set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
          total_profiles = -1;
          return;
      else
          prev_condition = curr_condition;
          prev_select = curr_select;
      end;
   end;

   setappdata(gcf,'num_selected_cond',sum(prev_select));

   ismultiblock = get(findobj(gcf,'Tag','SelectMultiblockData'),'Value');
   isbehav = get(findobj(gcf,'Tag','BehavPLSBn'),'Value');

   if ismultiblock | isbehav

      % make sure all behaviors are the same
      %
      newbehavdata = getappdata(gcf,'behavdata');

      s = load(cell_buffer{1},'behavdata','behavname');

      if isempty(s.behavdata) & isempty(newbehavdata)
           total_profiles = -1;

           msg = 'Behavior data is required';
           set(findobj(gcf,'Tag','MessageLine'),'String',msg);
           return;         
      end

      if(~exist('behavname','var'))
         s.behavname = {};
         for j=1:size(s.behavdata, 2)
            s.behavname = [s.behavname, {['behav', num2str(j)]}];
         end
      end
      
      prev_behavname = s.behavname;
      for i=2:total_profiles,
         s = load(cell_buffer{i},'behavdata','behavname');

         if(~exist('behavname','var'))
            s.behavname = {};
            for j=1:size(s.behavdata, 2)
               s.behavname = [s.behavname, {['behav', num2str(j)]}];
            end
         end

         curr_behavname = s.behavname;
         if ~isequal(curr_behavname,prev_behavname) & isempty(newbehavdata)
             [p_path, p_name, p_ext] = fileparts(cell_buffer{i});
             msg = sprintf('Incompatible behaviors found in "%s".',p_name);
             set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
             total_profiles = -1;
             return;
         else
             prev_behavname = curr_behavname;
         end;
      end;

      if isempty(newbehavdata)  
         setappdata(gcf,'num_selected_behav',size(s.behavdata,2));
      else
         setappdata(gcf,'num_selected_behav',size(newbehavdata,2));
      end

   end

   return;						% ValidateProfiles


%----------------------------------------------------------------------------
function SwitchFullPath()

   h = findobj(gcf,'Tag','FullPathChkbox');
   full_path = get(h,'Value');

   setappdata(gcf,'full_path',full_path);
   DisplayGroupProfiles(full_path);

   return;					% SwitchFullPath


%----------------------------------------------------------------------------
function ExecutePLS()

   tic
   PLSoptions = getappdata(gcf,'PLSoptions');
   progress_hdl = rri_progress_ui('initialize','Loading datamat');

   if exist('plslog.m','file')
      if PLSoptions.ismultiblock
         plslog('STRUCT Multiblock Analysis');
      elseif PLSoptions.isbehav
         plslog('STRUCT Behavior Analysis');
      elseif PLSoptions.iscontrast
         plslog('STRUCT Non-Rotated Analysis');
      else
         plslog('STRUCT Mean-Centering Analysis');
      end
   end

   [resultFile, elapsed_time] = struct_analysis(PLSoptions.isbehav, ...
	PLSoptions.profiles, PLSoptions.num_perm, ...
	PLSoptions.num_boot, PLSoptions.Clim, PLSoptions.posthoc, ...
	PLSoptions.cond_selection, PLSoptions.behavname, ...
	PLSoptions.behavdata, PLSoptions.behavdata_lst, PLSoptions.ContrastFile, ...
	PLSoptions.iscontrast, PLSoptions.ismean, PLSoptions.save_datamat, ...
	PLSoptions.ismultiblock, PLSoptions.bscan);

   if exist('progress_hdl','var') & ishandle(progress_hdl)
      close(progress_hdl);
   end

   msg1 = ['Result file "',resultFile,'" has been created and saved on your hard drive.'];
   msg2 = ['The total elapse time to build this datamat is ',num2str(elapsed_time),' seconds.'];

   if 0 % ~isempty(resultFile)
      uiwait(msgbox({msg1;'';msg2},'Completed','modal'));
%      uiwait(msgbox(msg1,'Completed','modal'));
   end

   uiresume;
   return;						% ExecutePLS


%----------------------------------------------------------------------------
function EditPosthocDataFile

   posthoc_data_file = deblank(strjust(get(gcbo,'String'),'left'));

   set(gcbo,'String',posthoc_data_file);

   if ~isempty(posthoc_data_file)
      if ( exist(posthoc_data_file,'file') == 2 )		% specified file exists
         setappdata(gcf,'posthoc_data_file',posthoc_data_file);
         return;
      end;

      if ( exist(posthoc_data_file,'dir') == 7 )		% it is a directory!
         msg = 'ERROR: The specified file is a direcotry!';
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
         setappdata(gcf,'posthoc_data_file','');
         return;
      end;
   else
%      msg = 'ERROR: Invalid input file!';
%      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      setappdata(gcf,'posthoc_data_file','');
      return;
   end;

   return;						% EditPosthocDataFile


%----------------------------------------------------------------------------
function SelectPosthocDataFile

   h = findobj(gcf,'Tag','PosthocDataEdit');

   [filename,pathname]=uigetfile('*.*','Select Posthoc Data File');

   if isequal(filename,0) | isequal(pathname,0)
      return;
   end;

   posthoc_data_file = [pathname, filename];

   set(h,'String',posthoc_data_file);
   set(h,'TooltipString',posthoc_data_file);
   setappdata(gcf,'posthoc_data_file',posthoc_data_file);

   return;						% SelectPosthocDataFile


%----------------------------------------------------------------------------
function deselect_condition

   curr_profiles = getappdata(gcf,'CurrGroupProfiles');

   if isempty(curr_profiles)
      msg = 'Group need to be added before you can deselect condition';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end

   tmp = load(curr_profiles{1});
   condition = tmp.session_info.condition;
   cond_selection = getappdata(gcf,'cond_selection');
   new_cond_selection = rri_deselect_cond_ui(condition, cond_selection);

   if ~isequal(new_cond_selection, cond_selection)
      setappdata(gcf,'cond_selection',new_cond_selection);
      setappdata(gcf,'behavname',{});
      setappdata(gcf,'behavdata',[]);
      setappdata(gcf,'behavdata_lst',{});
      setappdata(gcf, 'bscan', []);
   end

   return;						% deselect_condition


%----------------------------------------------------------------------------
function modify_behavdata

   curr_profiles = getappdata(gcf,'CurrGroupProfiles');

   if isempty(curr_profiles)
      msg = 'Group need to be added before you can modify behavior data';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end

   cond_selection = getappdata(gcf,'cond_selection');
   behavname = getappdata(gcf,'behavname');
   behavdata = getappdata(gcf,'behavdata');
   behavdata_lst = getappdata(gcf,'behavdata_lst');
   new_evt_list = getappdata(gcf,'new_evt_list');
   newdata_lst = getappdata(gcf,'newdata_lst');

   if isempty(behavdata)
      [status,behavname,behavdata,behavdata_lst,new_evt_list,newdata_lst] = ...
		struct_get_behavior(curr_profiles, cond_selection);
   else
      status = 1;
   end

   if status == 0
      msg = 'Condition or behavior incompatible in datamat file.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   else
      nr = length(new_evt_list);
      [new_behavdata, new_behavname] = rri_edit_behav(num2str(behavdata), ...
		behavname, 'Edit Behavior Data');
      new_behavdata = str2num(new_behavdata);

      while ~isempty(new_behavdata) & size(new_behavdata, 1) ~= nr
         if size(new_behavdata, 1) ~= nr
            msg1 = ['Number of rows should be equal to ' num2str(nr)];
            msg2 = '. For Multiblock PLS analysis, please also fill';
            msg3 = ' any values for subjects of those conditions that';
            msg4 = ' will be deselected in the behavior block';
            msg = [msg1 msg2 msg3 msg4];
            % set(findobj(gcf,'Tag','MessageLine'),'String',msg);
            uiwait(msgbox(msg, 'Error'));
         end

         [new_behavdata, new_behavname] = rri_edit_behav(num2str(behavdata), ...
		behavname, 'Edit Behavior Data');
         new_behavdata = str2num(new_behavdata);
      end

      if isempty(new_behavdata)
         msg = ['Behavior Data is not set'];
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
         return;
      elseif ~isequal(new_behavdata, behavdata) | ...
		~isequal(new_behavname, behavname)
         behavname = new_behavname;
         behavdata = new_behavdata;
         behavdata_lst = {};

         for i = 1:length(newdata_lst)
            mask = [];

            for j = 1:length(newdata_lst)
               if j == i
                  mask = [mask;ones(length(newdata_lst{j}),1)];
               else
                  mask = [mask;zeros(length(newdata_lst{j}),1)];
               end
            end

            behavdata_lst{i} = behavdata(find(mask),:);
         end
      end
   end

   setappdata(gcf,'behavname',behavname);
   setappdata(gcf,'behavdata',behavdata);
   setappdata(gcf,'behavdata_lst',behavdata_lst);
   setappdata(gcf,'new_evt_list',new_evt_list);
   setappdata(gcf,'newdata_lst',newdata_lst);

   return;						% modify_behavdata


%----------------------------------------------------------------------------
function multiblock_deselect_condition

   curr_profiles = getappdata(gcf,'CurrGroupProfiles');

   if isempty(curr_profiles)
      msg = 'Group need to be added before you can deselect condition';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end

   tmp = load(curr_profiles{1});
   cond_selection = getappdata(gcf,'cond_selection');

   bscan = getappdata(gcf,'bscan');
   if isempty(bscan)
      multiblock_cond_selection = ones(1, sum(cond_selection));
   else
      multiblock_cond_selection = zeros(1, sum(cond_selection));
      multiblock_cond_selection(bscan) = 1;
   end

   condition = tmp.session_info.condition;
   condition = condition(find(cond_selection));

   new_cond_selection = rri_deselect_cond_ui(condition, multiblock_cond_selection);
   new_bscan = find(new_cond_selection);

   if ~isequal(new_bscan, bscan)
      setappdata(gcf, 'bscan', new_bscan);
   end

   return;						% multiblock_deselect_condition


%----------------------------------------------------------------------------
function status = cExecutePLS()

   status = 0;

   PLSoptions = getappdata(gcf,'PLSoptions');

   if exist('plslog.m','file')
      if PLSoptions.ismultiblock
         plslog('STRUCT Multiblock Analysis');
      elseif PLSoptions.isbehav
         plslog('STRUCT Behavior Analysis');
      elseif PLSoptions.iscontrast
         plslog('STRUCT Non-Rotated Analysis');
      else
         plslog('STRUCT Mean-Centering Analysis');
      end
   end

   if PLSoptions.ismultiblock
      method = 4;			% Multiblock PLS
      ContrastFile = 'MULTIBLOCK';
   elseif PLSoptions.isbehav
      method = 3;			% Behavior PLS
      ContrastFile = 'BEHAV';
   elseif PLSoptions.iscontrast
      method = 2;			% Non-Rotated Task PLS
      ContrastFile = PLSoptions.ContrastFile;
   else
      method = 1;			% Mean-Centering Task PLS
      ContrastFile = 'NONE';
   end

   datamat_files = PLSoptions.profiles;
   perm_result_num_perm = PLSoptions.num_perm;
   boot_result_num_boot = PLSoptions.num_boot;
   Clim = PLSoptions.Clim;
   save_datamat = PLSoptions.save_datamat;
   cond_selection = PLSoptions.cond_selection;
   behavname = PLSoptions.behavname;
   behavdata = PLSoptions.behavdata;
   bscan = PLSoptions.bscan;

   %  save results
   %
   fn = datamat_files{1};
   load(fn,'session_info');
   datamat_prefix = session_info.datamat_prefix;

   [result_file,result_path] = ...
      uiputfile([datamat_prefix,'_STRUCTresult.mat'],'Please enter a result file name that will be used by the created batch file');

   if isequal(result_file,0)			% Cancel was clicked
      return;
   else
      resultFile = result_file;
   end;

   %  save batch input file
   %
   [input_file,input_path] = ...
      uiputfile([datamat_prefix,'_STRUCTanalysis.txt'],'Please enter a batch file name that will be used by "batch_plsgui"');

   if isequal(input_file,0)			% Cancel was clicked
      return;
   else
      inputFile = input_file;
   end;

   fid = fopen(inputFile, 'wt');

   fprintf(fid, '\n%s\n\n', '%---

⌨️ 快捷键说明

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