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

📄 batch_pls_analysis.m

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

   result_file = '';
   group_files = {};
   pls = 1;
   num_perm = 0;
   num_boot = 0;
   clim = 95;
   save_data = 0;
   contrasts = [];
   behavdata = [];
   behavname = {};
   selected_cond = [];
   selected_bcond = [];
   bscan = [];

   wrongbatch = 0;

   while ~feof(fid)

      tmp = fgetl(fid);

      if ischar(tmp) & ~isempty(tmp)
         tmp = strrep(tmp, char(9), ' ');
         tmp = deblank(fliplr(deblank(fliplr(tmp))));
      end

      while ~feof(fid) & (isempty(tmp) | isnumeric(tmp) | strcmpi(tmp(1), '%'))
         tmp = fgetl(fid);

         if ischar(tmp) & ~isempty(tmp)
            tmp = strrep(tmp, char(9), ' ');
            tmp = deblank(fliplr(deblank(fliplr(tmp))));
         end
      end

      if ischar(tmp) & ~isempty(tmp)
         [tok rem] = strtok(tmp);

         if ~isempty(rem)
            [rem junk] = strtok(rem, '%');
            rem = deblank(fliplr(deblank(fliplr(rem))));
         end
      else
         tok = '';
      end

      switch tok
      case 'result_file'
         result_file = rem;
         if isempty(rem), wrongbatch = 1; end;
      case 'group_files'
         this_group = {};

         while ~isempty(rem)
            [tmp rem] = strtok(rem);
            this_group = [this_group; {tmp}];
         end

         if isempty(this_group), wrongbatch = 1; end;
         group_files = [group_files {this_group}];
      case 'pls'
         pls = str2num(rem);
         if isempty(pls), pls = 1; end;
      case 'num_perm'
         num_perm = str2num(rem);
         if isempty(num_perm), num_perm = 0; end;
      case 'num_boot'
         num_boot = str2num(rem);
         if isempty(num_boot), num_boot = 0; end;
      case 'clim'
         clim = str2num(rem);
         if isempty(clim), clim = 95; end;
      case 'save_data'
         save_data = str2num(rem);
         if isempty(save_data), save_data = 0; end;
      case 'selected_cond'
         selected_cond = str2num(rem);
      case 'selected_bcond'
         selected_bcond = str2num(rem);
      case 'contrast_data'
         this_row = [];

         while ~isempty(rem)
            [tmp rem] = strtok(rem);
            this_row = [this_row str2num(tmp)];
         end

         if isempty(this_row)
            wrongbatch = 1;
            break;
         end

         if ~isempty(contrasts) & size(contrasts,2) ~= length(this_row)
            wrongbatch = 1;
            break;
         end

         contrasts = [contrasts; this_row];
      case 'behavior_data'
         this_row = [];

         while ~isempty(rem)
            [tmp rem] = strtok(rem);
            this_row = [this_row str2num(tmp)];
         end

         if isempty(this_row)
            wrongbatch = 1;
            break;
         end

         if ~isempty(behavdata) & size(behavdata,2) ~= length(this_row)
            wrongbatch = 1;
            break;
         end

         behavdata = [behavdata; this_row];
      case 'behavior_name'
         while ~isempty(rem)
            [tmp rem] = strtok(rem);
            behavname = [behavname {tmp}];
         end
      end
   end

   fclose(fid);

   if wrongbatch
      error('There is error(s) in batch file, please read ''UserGuide.htm'' for help');
   end

   progress_hdl = rri_progress_status('create','PLS Analysis');
   first_file = group_files{1}{1};
   load(first_file, 'session_info');

   if isempty(selected_cond) | sum(selected_cond) == 0 | ...
	( length(selected_cond) ~= session_info.num_conditions & ...
          isempty(findstr(first_file, '_ERPdatamat.mat')) )
      selected_cond = ones(1, session_info.num_conditions);
   end

   if isempty(selected_bcond) | sum(selected_bcond) == 0 | ...
	( length(selected_bcond) ~= session_info.num_conditions & ...
          isempty(findstr(first_file, '_ERPdatamat.mat')) )
      selected_bcond = ones(1, length(selected_cond));
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% restriction below will be removed in the future

   selected_bcond = selected_bcond .* selected_cond;

   if sum(selected_bcond) == 0
      selected_bcond = selected_cond;
   end

% restriction above will be removed in the future
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   bscan = find(selected_bcond(find(selected_cond)));

%   if isempty(bscan) | length(bscan) > sum(selected_cond) ...
%	| max(bscan) > sum(selected_cond)
%      bscan = 1:sum(selected_cond);
%   end

%   if ~isempty(findstr(first_file, '_BfMRIsession.mat'))	% Blocked fMRI
%   elseif ~isempty(findstr(first_file, '_fMRIsession.mat'))	% E.R. fMRI

   if length(first_file)>14 & strcmpi(first_file(end-14:end),'fMRIsession.mat')

      PLSoptions.profiles = group_files;
      PLSoptions.group_analysis = 1;
      PLSoptions.num_perm = num_perm;
      PLSoptions.num_boot = num_boot;
      PLSoptions.posthoc = [];
      PLSoptions.Clim = clim;
      PLSoptions.save_datamat = save_data;
      PLSoptions.cond_selection = selected_cond;

      PLSoptions.behavname = {};
      PLSoptions.behavdata = [];
      PLSoptions.behavdata_lst = {};
      PLSoptions.bscan = bscan;

      PLSoptions.output_file = result_file;

      if exist('plslog.m','file')
         if length(first_file)>15 & strcmpi(first_file(end-15:end),'BfMRIsession.mat')
            switch pls
            case 1
               plslog('Batch BfMRI Mean-Centering Analysis');
            case 2
               plslog('Batch BfMRI Non-Rotated Analysis');
            case 3
               plslog('Batch BfMRI Behavior Analysis');
            case 4
               plslog('Batch BfMRI Multiblock Analysis');
            end
         else
            switch pls
            case 1
               plslog('Batch fMRI Mean-Centering Analysis');
            case 2
               plslog('Batch fMRI Non-Rotated Analysis');
            case 3
               plslog('Batch fMRI Behavior Analysis');
            case 4
               plslog('Batch fMRI Multiblock Analysis');
            end
         end
      end

      switch pls
      case 1
         PLSoptions.ContrastFile = [];
      case 2
         PLSoptions.ContrastFile = contrasts;
      case 3
         PLSoptions.ContrastFile = 'BEHAV';

%         for bcol=1:size(behavdata, 2)
 %           PLSoptions.behavname = ...
%		[PLSoptions.behavname, {['behav', num2str(bcol)]}];
 %        end

         PLSoptions.behavdata = behavdata;
         PLSoptions.behavname = behavname;

         for g = 1:length(group_files)
            session_files = group_files{g};
            count = 0;

            for s = 1:length(session_files)
               fn = session_files{s};
               fn = strrep(fn, 'session.mat', 'datamat.mat');
               load(fn, 'st_evt_list');
               count = count + length(st_evt_list);
            end

            count = count * sum(selected_cond) / session_info.num_conditions;
            PLSoptions.behavdata_lst{g} = behavdata(1:count, :);
            behavdata(1:count, :) = [];
         end
      case 4
         PLSoptions.ContrastFile = 'MULTIBLOCK';

%         for bcol=1:size(behavdata, 2)
 %           PLSoptions.behavname = ...
%		[PLSoptions.behavname, {['behav', num2str(bcol)]}];
 %        end

         PLSoptions.behavdata = behavdata;
         PLSoptions.behavname = behavname;

         for g = 1:length(group_files)
            session_files = group_files{g};
            count = 0;

            for s = 1:length(session_files)
               fn = session_files{s};
               fn = strrep(fn, 'session.mat', 'datamat.mat');
               load(fn, 'st_evt_list');
               count = count + length(st_evt_list);
            end

            count = count * sum(selected_cond) / session_info.num_conditions;
            PLSoptions.behavdata_lst{g} = behavdata(1:count, :);
            behavdata(1:count, :) = [];
         end
      end

      if(exist(result_file, 'file')==2)
         disp(['WARNING: File ',result_file,' is overwritten.']);
      end

      fmri_pls_analysis(PLSoptions.profiles, PLSoptions.ContrastFile, ...
				   PLSoptions.num_perm, ...
				   PLSoptions.num_boot, ...
				   PLSoptions.Clim, ...
				   PLSoptions.posthoc, ...
				   PLSoptions.save_datamat, ...
                                   PLSoptions.group_analysis, ...
                                   PLSoptions.cond_selection, ...
                                   PLSoptions.behavname, ...
                                   PLSoptions.behavdata, ...
                                   PLSoptions.behavdata_lst, ...
				   PLSoptions.bscan, ...
				   PLSoptions.output_file);

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

   elseif ~isempty(findstr(first_file, '_ERPdatamat.mat'))	% ERP

      PLSoptions.profiles = [group_files{:}];
      PLSoptions.num_perm = num_perm;
      PLSoptions.num_boot = num_boot;
      PLSoptions.posthoc = [];
      PLSoptions.Clim = clim;
      PLSoptions.save_datamat = save_data;
      PLSoptions.cond_selection = selected_cond;
      PLSoptions.system = session_info.system;
      PLSoptions.ishelmert = 0;

      PLSoptions.behavname = {};
      PLSoptions.behavdata = [];
      PLSoptions.behavdata_lst = {};
      PLSoptions.bscan = bscan;
      PLSoptions.BehavDataCol = 1;
      PLSoptions.ContrastDataCol = 1;

      PLSoptions.output_file = result_file;

⌨️ 快捷键说明

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