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

📄 struct_create_datamat.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 2 页
字号:
%STRUCT_CREATE_DATAMAT Creates a datamat file
%
%  USAGE: struct_create_datamat({session_file})
%

%   Called by struct_session_profile_ui
%
%   I (session_file) - Matlab data file that contains a structure array
%			containing the session information for the study
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function struct_create_datamat(varargin)

   if nargin == 0 | ~ischar(varargin{1})
      session_file = varargin{1}{1};
      init(session_file);
      return;
   end;

   %  clear the message line,
   %
   h = findobj(gcf,'Tag','MessageLine');
   set(h,'String','');

   action = varargin{1};

   if strcmp(action,'filename_edit')
      filename_hdl = getappdata(gcf, 'filename_hdl');
      filename = get(filename_hdl, 'string');
      setappdata(gcf,'filename',filename);
   elseif strcmp(action,'create_bn_pressed')
      if (SaveDatamatOptions) 
         CreateDatamat;
      end
   elseif strcmp(action,'EDIT_NORMAL_VOLUME')
      msg = 'Please keep this check box unchecked unless you have a good reason not to do so.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
   elseif strcmp(action,'MERGE_CONDITIONS'),
      MergeConditions;
   elseif strcmp(action,'EDIT_BRAIN_REGION_FILE'),
      EditBrainRegionFile;
   elseif strcmp(action,'BRAIN_REGION_FILE_BUTTON'),
      SelectBrainRegionFile;
   elseif strcmp(action,'ORIENT'),
      orient;
   elseif strcmp(action,'close_bn_pressed')
      close(gcf);
   end

   return;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   init: Initialize the GUI layout
%
%   I (session_file) - Matlab data file that contains a structure array
%			containing the session information for the study
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function init(session_file)

   session_win_hdl = gcf;
   load(session_file);					% session info

   subj_name = session_info.subj_name;
   cond_name = session_info.condition;

   pls_data_path = session_info.pls_data_path;
   filename = [session_info.datamat_prefix, '_STRUCTdatamat.mat'];
   dataname = [session_info.datamat_prefix, '_STRUCTdata.mat'];

   selected_subjects = ones(1, session_info.num_subjects);	% select all

   h01 = struct_create_modify_ui(0);

   subj_lst_hdl = getappdata(h01,'subj_lst_hdl');
   filename_hdl = getappdata(h01,'filename_hdl');

   set(subj_lst_hdl, 'string', subj_name);
   set(filename_hdl, 'string', filename);

   setappdata(h01,'selected_subjects', selected_subjects);
   setappdata(h01,'filename', filename);
   setappdata(h01,'dataname', dataname);
   setappdata(h01,'session_info', session_info);
   setappdata(h01,'session_file', session_file);
   setappdata(h01,'SessionFile',session_file);
   setappdata(h01,'session_win_hdl',session_win_hdl);

   setappdata(h01,'BrainRegionFile',[]);
   setappdata(h01, 'merged_conds', []);

   set(subj_lst_hdl, 'value',find(selected_subjects), 'list',1);

   return;					% init


%----------------------------------------------------------------------------
function MergeConditions()

   session_info = getappdata(gcf, 'session_info');
   condition = session_info.condition;
   merged_conds = fmri_merge_condition_ui(condition);
   setappdata(gcf, 'merged_conds', merged_conds);

   return;					% MergeConditions


%----------------------------------------------------------------------------
function EditBrainRegionFile()

   fname = get(gcbo,'String');
   fname = deblank(fliplr(deblank(fliplr(fname))));

   if isempty(fname)
      setappdata(gcf,'BrainRegionFile',[]);
      return;
   end;

   pathfile = fullfile(pwd, fname);

   if (exist(pathfile,'file') ~= 2)

      pathfile = fname;

      if (exist(pathfile,'file') ~= 2)
         msg = 'ERROR: Invalid file specified.';
         set(findobj(gcf,'Tag','MessageLine'),'String',msg);
         setappdata(gcf,'BrainRegionFile',[]);
         return;
      end
   end;

   %  make sure the IMG file can be accessed.
   %
   try
      dummy = rri_imginfo(pathfile);
   catch 
      msg = 'ERROR: Cannot open the file.  Make sure it is an IMG or NII file.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   set(gcbo,'String',pathfile);
   setappdata(gcf,'BrainRegionFile',pathfile);

   return;					% EditBrainRegionFile


%----------------------------------------------------------------------------
function SelectBrainRegionFile()

   session_info = getappdata(gcf,'session_info');
   [fname, fpath] = uigetfile(['*' session_info.img_ext],'Brain Region Mask');

   if isequal(fname,0),			% no file has been selected
      return;
   end;

   if ~isequal(fpath,filesep) & ~isequal(fpath,0)
      fname = fullfile(fpath,fname);
   end;

   %  make sure the IMG file can be accessed.
   %
   try
      dummy = rri_imginfo(fname);
   catch 
      msg = 'ERROR: Cannot open the file.  Make sure it is an IMG or NII file.';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   set(findobj(gcbf,'Tag','PredefineRegionFileEdit'),'String',fname);
   setappdata(gcf,'BrainRegionFile',fname);
   
   return;					% SelectBrainRegionFile


%----------------------------------------------------------------------------
function orient()

   nii = getappdata(gcf, 'nii');
   orient_pattern = getappdata(gcf, 'orient_pattern');
   session_info = getappdata(gcf, 'session_info');
   imgfile = fullfile(session_info.dataset_path, session_info.subj_files{1,1});

   [dims, voxel_size, origin, nii, orient_pattern] = ...
	rri_orient_pattern_ui(imgfile, nii, orient_pattern);

   if isempty(nii)
      return;
   end

   setappdata(gcf, 'dims', double(nii.hdr.dime.dim(2:4)));
   setappdata(gcf, 'voxel_size', double(nii.hdr.dime.pixdim(2:4)));
   setappdata(gcf, 'origin', double(nii.hdr.hist.originator(1:3)));
   setappdata(gcf, 'nii', nii);
   setappdata(gcf, 'orient_pattern', orient_pattern);

   return;					% orient


%----------------------------------------------------------------------------
function status = SaveDatamatOptions()

   status = 0;			% set status to fail first

   STOptions.session_win_hdl = getappdata(gcf,'session_win_hdl');
   STOptions.UseBrainRegionFile = 1;
   STOptions.BrainRegionFile = getappdata(gcf,'BrainRegionFile');

   if isempty(STOptions.BrainRegionFile),
       msg = 'ERROR: Invalid file for the brain region mask.';
       set(findobj(gcf,'Tag','MessageLine'),'String',msg);
       return;
   end;

   STOptions.Threshold = [];
   STOptions.ConsiderAllVoxels = 0;

   h = findobj(gcbf,'Tag','MeanRatioChkButton');
   STOptions.NormalizeVolumeMean = get(h,'Value');

   session_info = getappdata(gcf,'session_info');
   selected_subjects = zeros(1, session_info.num_subjects);
   subj_value = get(getappdata(gcf,'subj_lst_hdl'), 'value');
   selected_subjects(subj_value) = 1;
   STOptions.selected_subjects = selected_subjects;

   status = 1;
   setappdata(gcf,'STOptions',STOptions);

   return;					% SaveDatamatOptions


%----------------------------------------------------------------------------
function CreateDatamat()

    if exist('plslog.m','file')
       plslog('Create STRUCT Datamat');
    end

    tic;
    options = getappdata(gcf,'STOptions');

    merged_conds = getappdata(gcf, 'merged_conds');
    session_file = getappdata(gcf,'SessionFile');

    load(session_file);

    if isfield(session_info, 'behavdata')
       behavdata = session_info.behavdata;
    else
       behavdata = [];
    end

    if isfield(session_info, 'behavname')
       behavname = session_info.behavname;
    else
       behavname = {};
    end

    pls_data_path = session_info.pls_data_path;
    dataset_path = session_info.dataset_path;
    num_behavior = session_info.num_behavior;
    datamat_prefix = session_info.datamat_prefix;
    num_conditions = session_info.num_conditions;
    condition = session_info.condition;
    num_subjects = session_info.num_subjects;
    subject = session_info.subject;
    subj_name = session_info.subj_name;
    subj_files = session_info.subj_files;

    k = num_conditions;
    n = num_subjects;
    filename = [datamat_prefix, '_STRUCTdatamat.mat'];
    dataname = [datamat_prefix, '_STRUCTdata.mat'];

    selected_subjects = options.selected_subjects;

    use_brain_mask = options.UseBrainRegionFile;
    brain_mask_file = options.BrainRegionFile;

    brain_mask = [];
    mask_dims = [];

    brain_mask = load_nii(brain_mask_file, 1);
    brain_mask = reshape(int8(brain_mask.img), [brain_mask.hdr.dime.dim(2:3) 1 brain_mask.hdr.dime.dim(4)]);
    mask_dims = size(brain_mask);

    create_datamat_info.brain_mask_file = brain_mask_file;

    normalize_volume_mean = options.NormalizeVolumeMean;
    create_datamat_info.normalize_volume_mean = normalize_volume_mean;

    curr = pwd;
    if isempty(curr)
       curr = filesep;
    end

    savepwd = curr;
    home = curr;	% pls_data_path;

    %  add path for all subject
    %
    for i = 1:k
        for j = 1:n
            subj_files{i,j} = fullfile(dataset_path, subj_files{i,j});
        end
    end

    orient_pattern = getappdata(gcf, 'orient_pattern');

    if isempty(orient_pattern)
       [dims, voxel_size, origin] = rri_imginfo(subj_files{1,1});
    else
       dims = getappdata(gcf, 'dims');
       voxel_size = getappdata(gcf, 'voxel_size');
       origin = getappdata(gcf, 'origin');
    end

    dims = [dims(1) dims(2) 1 dims(3)];

    if (use_brain_mask==1) & ~isequal(dims,mask_dims),
       errmsg ='ERROR: Dimensions of the data do not match that of the brain mask!';
       errordlg(errmsg,'Brain Mask Error');
       waitfor(gcf);
       return;
    end;

    if (use_brain_mask == 1)			% coords from brain_mask
       coords = find( brain_mask(:) > 0)';
       m = zeros(dims);		
       m(coords) = 1;
       coords = find(m == 1)';
    end;

    num_voxels = prod(dims);

    %  to make progress bar in the center
    %
    fig1 = gcf;
    progress_hdl = rri_progress_ui('initialize', 'Creating Datamat');
    close(fig1);

    section1 = n*k/(n*k+0);		% 1st section of progress bar
    factor = 1/(n*k+0);		% factor for the 2nd section

    rri_progress_ui(progress_hdl, '', 0.5*factor);


    sugg3=0;			% suggestion3 doesn't work here

    %  make datamat, which includes non_brain voxels
    %
if sugg3	
    try
       if str2num(v7(1))<7
          datamat=double(zeros(n*k,length(coords)));
       else
          datamat=single(zeros(n*k,length(coords)));
       end
    catch
       close(progress_hdl);
       errmsg = ['ERROR: there is too much data, 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
else
    datamat=[];
end

    for i = 1:k

if ~sugg3
        temp=[];
end

⌨️ 快捷键说明

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