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

📄 struct_create_datamat.m

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

        for j=1:n

            message=['Loading condition ',num2str(i),', subject ',num2str(j),'.'];
            rri_progress_ui(progress_hdl,'Loading Images',message);

            img = load_nii(subj_files{i,j}, 1);

            v7 = version;
            if str2num(v7(1))<7
               img = reshape(double(img.img), [img.hdr.dime.dim(2:3) 1 img.hdr.dime.dim(4)]);
            else
               img = reshape(single(img.img), [img.hdr.dime.dim(2:3) 1 img.hdr.dime.dim(4)]);
            end

            if ~isempty(orient_pattern)
               img = img(orient_pattern);
            end

            try
               img = img(coords)';
if sugg3
               datamat(((i-1)*n+j),:) = img(:)';
else
               temp = [temp; img(:)'];
end
            catch
               close(progress_hdl);
               errmsg = ['ERROR: there is too much data after condition ' num2str(i) ' at subject ' num2str(j) ', please delete some subjects in the "Edit Subject" window by clicking "Select Subjects" button in the session information window.'];
               errordlg(errmsg,'Too much data');
               waitfor(gcf);
               return;
            end

        end

if ~sugg3
        datamat=[datamat;temp];
end
        rri_progress_ui(progress_hdl, '', ((i-1)*n+j)*factor);

    end

    message = 'Selecting only the brain voxels ...';
    rri_progress_ui(progress_hdl,'Building datamat',message);

    [dr,dc]=size(datamat);

    factor = 0.1/dr;			% factor for the 2nd section
    for i=1:dr
        rri_progress_ui(progress_hdl,'',section1+(i*factor)*(1-section1));
    end

    %  Check zero variance voxels & NaN voxels, and remove them from coords
    %
    try
       check_var = var(datamat);
    catch
       close(progress_hdl);
       errmsg = ['ERROR: there is too much data to check zero variance, please delete some subjects in the "Edit Subject" window by clicking "Select Subjects" button in the session information window.'];
       errordlg(errmsg,'Too much data');
       waitfor(gcf);
       return;
    end

    bad_coords = find( (check_var==0) | isnan(check_var) );
    coords(bad_coords) = [];
    datamat(:,bad_coords) = [];

    [dr,dc]=size(datamat);		% update dr/dc

    if (normalize_volume_mean == 1)

       % perform whole-brain ratio adjustment
       gmean=mean(datamat,2);		% grand mean for each image

       rri_progress_ui(progress_hdl,'Normalizing datamat',section1+0.2*(1-section1));

       message = 'Normalize datamat with its volume mean ...';
       rri_progress_ui(progress_hdl,'',message);

       factor = 0.8/dc;				% factor for the 2nd section
       checkpoint = floor(dc/10);		% set check point
       check = checkpoint;
       percentage = 10;

       for i=1:dc
           datamat(:,i)=(datamat(:,i))./gmean;	% normalized on the mean of each img

           if(i==check)
               rri_progress_ui(progress_hdl,'',section1+(0.2+i*factor)*(1-section1));
               message = [num2str(percentage), '% of the volume is normalized.'];
               rri_progress_ui(progress_hdl,'',message);
               check = check + checkpoint;
               percentage = percentage + 10;
           end
       end

    end

    if ~isempty(merged_conds)
        new_num_conditions = length(merged_conds);
        new_condition = {merged_conds.name};
        [b_dr b_dc] = size(behavdata);

        for i = 1:k
            datamat_cond{i} = datamat((i-1)*n+1:i*n,:);
            datamat_cond{i} = datamat_cond{i}(:)';

            if ~isempty(behavdata)
                behav_cond{i} = behavdata((i-1)*n+1:i*n,:);
                behav_cond{i} = behav_cond{i}(:)';
            end
        end

        clear datamat;

        for i = 1:new_num_conditions
            new_datamat_cond{i} = ...
		mean(cat(1, datamat_cond{merged_conds(i).cond_idx}), 1);
            new_datamat_cond{i} = reshape(new_datamat_cond{i}, [n dc]);

            if ~isempty(behavdata)
                new_behav_cond{i} = ...
			mean(cat(1, behav_cond{merged_conds(i).cond_idx}), 1);
                new_behav_cond{i} = reshape(new_behav_cond{i}, [n b_dc]);
            end
        end

        clear datamat_cond behav_cond;
        datamat = cat(1, new_datamat_cond{:});

        if ~isempty(behavdata)
            behavdata = cat(1, new_behav_cond{:});
            session_info.behavdata = behavdata;
            session_info.num_behavior = size(behavdata,1);
        end

        clear new_datamat_cond new_behav_cond;
        session_info.num_conditions = new_num_conditions;
        session_info.condition = new_condition;

    end

    rri_progress_ui(progress_hdl,'',1);
    message = 'Saving to the disk ...';
    rri_progress_ui(progress_hdl,'Save',message);

    elapsed_time = toc;
%    disp('Datamat is created ...');

    % save to disk

    datamatfile = fullfile(home, filename);
    datafile = fullfile(home, dataname);

    if(exist(datamatfile,'file')==2)	% datamat file with the same file name exist

        dlg_title = 'Confirm File Overwrite';
        msg = ['File ',filename,' exist. Are you sure you want to overwrite it?'];
        response = questdlg(msg,dlg_title,'Yes','No','Yes');

        if(strcmp(response,'No'))

            cd(home);

          savfig = [];
          if strcmpi(get(gcf,'windowstyle'),'modal')
             savfig = gcf;
             set(gcf,'windowstyle','normal');
          end

          done1 = 0;
          while ~done1

            putfile_filter = [datamat_prefix,'_STRUCTdatamat.mat'];
            [filename, pathname] = uiputfile(putfile_filter,'Save As');

            if isequal(filename,0)
                datamatfile = [];
                close(progress_hdl);

                msg1 = ['WARNING: No file is saved.'];
                % msgbox(msg1,'Uncomplete');
                %uiwait(msgbox(msg1,'Uncomplete','modal'));
                set(findobj(gcf,'Tag','MessageLine'),'String',msg1);
                done1 = 1;
%                uiresume;
                return;
            else
                if rri_chkfname(filename, 'STRUCT', 'datamat')
                   datamatfile = fullfile(pathname, filename);
                   done1 = 1;
                else
                   msg = ['File name must be ended with _STRUCTdatamat.mat'];
                   uiwait(msgbox(msg,'Wrong filename','modal'));
                end
            end

          end

          if ~isempty(savfig)
             set(savfig,'windowstyle','modal');
          end

        end
    end

   %  check if exist data file
   %
   if(exist(datafile,'file')==2)  % data file with same filename exist
      dlg_title = 'Confirm File Overwrite';
      msg = ['File ',dataname,' exist. Are you sure you want to overwrite it?'];
      response = questdlg(msg,dlg_title,'Yes','No','Yes');

      if(strcmp(response,'No'))


       savfig = [];
       if strcmpi(get(gcf,'windowstyle'),'modal')
          savfig = gcf;
          set(gcf,'windowstyle','normal');
       end

       done1=0;
       while ~done1

         putfile_filter = [datamat_prefix,'_STRUCTdata.mat'];
         [filename, pathname] = uiputfile(putfile_filter,'Save As');

         if isequal(filename,0)

            close(progress_hdl);
            msg1 = ['WARNING: No file is saved.'];
            %uiwait(msgbox(msg1,'Uncomplete','modal'));
            set(findobj(gcf,'Tag','MessageLine'),'String',msg1);
            done1=1;
            return;
         else
            if rri_chkfname(filename, 'STRUCT', 'data')
               datafile = fullfile(pathname, filename);
               done1=1;
            else
               msg = ['File name must be ended with _STRUCTdata.mat'];
               uiwait(msgbox(msg,'Wrong filename','modal'));
            end
         end

       end

       if ~isempty(savfig)
          set(savfig,'windowstyle','modal');
       end

      end
   end

    savfig = [];
    if strcmpi(get(gcf,'windowstyle'),'modal')
       savfig = gcf;
       set(gcf,'windowstyle','normal');
    end

    create_ver = plsgui_vernum;

   %  save data file
   %
    done = 0;

    v7 = version;
    if str2num(v7(1))<7
%       datamat = double(datamat);
       singleprecision = 0;
    else
       singleprecision = 1;
    end

    while ~done
       try
          save(datafile,'datamat','create_ver');
          done = 1;
       catch

          done1 = 0;
          while ~done1

            putfile_filter = [datamat_prefix,'_STRUCTdata.mat'];
            [filename, pathname] = uiputfile(putfile_filter, ...
		'Can not save data file, please try again');

            if isequal(filename,0)
                datafile = [];
                close(progress_hdl);

                msg1 = ['WARNING: Data file is not saved.'];
                % msgbox(msg1,'Uncomplete');
                %uiwait(msgbox(msg1,'Uncomplete','modal'));
                set(findobj(gcf,'Tag','MessageLine'),'String',msg1);
                done1=1;
%                uiresume;
                return;
            else
                if rri_chkfname(filename, 'STRUCT', 'data')
                   datamatfile = fullfile(pathname, filename);
                   done1=1;
                else
                   msg = ['File name must be ended with _STRUCTdata.mat'];
                   uiwait(msgbox(msg,'Wrong filename','modal'));
                end
            end

          end

       end
    end

   %  save datamat file
   %
    done = 0;

    while ~done
       try
          save(datamatfile,'datafile','coords','behavdata','behavname', ...
		'bad_coords', 'selected_subjects', ...
		'dims','voxel_size','origin','session_file','session_info', ...
		'create_ver','create_datamat_info','singleprecision');
          done = 1;
       catch

          done1 = 0;
          while ~done1

            putfile_filter = [datamat_prefix,'_STRUCTdatamat.mat'];
            [filename, pathname] = uiputfile(putfile_filter, ...
		'Can not save datamat file, please try again');
            if isequal(filename,0)
                datamatfile = [];
                close(progress_hdl);
                msg1 = ['WARNING: Datamat file is not saved.'];
                % msgbox(msg1,'Uncomplete');
                %uiwait(msgbox(msg1,'Uncomplete','modal'));
                set(findobj(gcf,'Tag','MessageLine'),'String',msg1);
                done1=1;
%                uiresume;
                return;
            else
                if rri_chkfname(filename, 'STRUCT', 'datamat')
                   datamatfile = fullfile(pathname, filename);
                   done1=1;
                else
                   msg = ['File name must be ended with _STRUCTdatamat.mat'];
                   uiwait(msgbox(msg,'Wrong filename','modal'));
                end
            end

          end

       end
    end

    if ~isempty(savfig)
       set(savfig,'windowstyle','modal');
    end

    cd(savepwd);

    close(progress_hdl);

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

%    uiwait(msgbox({msg1;'';msg2},'Completed','modal'));
%%    uiwait(msgbox(msg1,'Completed','modal'));
    %% msgbox({msg1;'';msg2},'Completed');	% however, this works for PC

   msg1 = ['WARNING: Do not change file name manually in command window.'];
   uiwait(msgbox(msg1,'File has been saved'));

%    uiresume;
    return;					% CreateDatamat

⌨️ 快捷键说明

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