将多个值放入1个单元格(例如数组{1} = [1,2,3])以进行多个条件SPM分析;我得到的只是数组{1} = [1] [2] [3]

时间:2015-10-26 22:06:07

标签: matlab analysis

我正努力在某些fMRI数据上完成第一级分析,并且因为这是我第一次以这种方式使用SPM,似乎我的挫败感无止境。 SPM包含以下具体说明:

"此* .mat文件必须包含以下单元格数组(每个1 x n):名称,起始位置和持续时间。例如。 names = cell(1,5),onsets = cell(1,5),durations = cell(1,5),then names {2} =" second condition", onsets {2} = [3,5,19,22] ,持续时间{2} = [0,0,0,0],包含第二个条件的必要详细信息。"

我使用的代码从存储行为数据的各种excel文件中获取所需的数据,并将它们添加到这些单元阵列中。

sessionFiles = dir('*.xlsx');
allNames = {sessionFiles.name}';
conditions = 36;
% go through excel files to grab relevant column information for SPM
for i=1:length(sessionFiles)
    [~,fileName,~] = fileparts(allNames{i});
    % initialize cells SPM needs
    names = cell(1,conditions);
    onsets = cell(1,conditions);
    durations = {1.75};
    durations = repmat(durations,1,conditions);    
    % read in excel file
    [num,~,~] = xlsread(sessionFiles(i).name);
    trialType = num(:,6);
    % grab condition information from columns: seconds=9, name=6
    for j=1:conditions
        index = find(trialType==j);
        trialOnsets = cell(1,length(index));
        names{1,j} = j;
        for k=1:length(index)
            trialOnsets{1,k}=double(num(index(k),9));
        end
        onsets{1,j} = trialOnsets;
    end
    % save new data for SPM
    save(fileName,'names','onsets','durations');
    clear names onsets durations fileName num raw text
end

我找到了一个示例,显示每个单元格应如下所示:

example format

我无法弄清楚如何自动获取数字并将它们放入这样的单元格中。

我知道这不是SPM论坛,但我发现了一些问题,我想我会试试运气。

1 个答案:

答案 0 :(得分:0)

使用行trialOnsets = cell(1,length(index));trialOnsets被指定为大小为1xlength(index)的单元格数组。然后,trialOnsets被分配给onsets{1,j}。使用此工作流程,onsets的每个单元格的大小为1xlength(index)

相反,onsets的每个单元格的大小应为1x1,而onsets中的每个1x1单元格都应具有大小为1xlength(index)的矩阵。为此,请执行以下操作。

  1. 指定trialOnsets作为矩阵,而不是单元格数组。为此,请将trialOnsets = cell(1,length(index));替换为trialOnsets = zeros(1,length(index));
  2. 将值从num分配到trialOnsets,现在是一个矩阵(以前是一个单元格数组)。为此,请将trialOnsets{1,k}=double(num(index(k),9));替换为trialOnsets(1,k)=double(num(index(k),9));
  3. 编辑后的代码应如下:

    sessionFiles = dir('*.xlsx');
    allNames = {sessionFiles.name}';
    conditions = 36;
    % go through excel files to grab relevant column information for SPM
    for i=1:length(sessionFiles)
        [~,fileName,~] = fileparts(allNames{i});
        % initialize cells SPM needs
        names = cell(1,conditions);
        onsets = cell(1,conditions);
        durations = {1.75};
        durations = repmat(durations,1,conditions);    
        % read in excel file
        [num,~,~] = xlsread(sessionFiles(i).name);
        trialType = num(:,6);
        % grab condition information from columns: seconds=9, name=6
        for j=1:conditions
            index = find(trialType==j);
            trialOnsets = zeros(1,length(index));
            names{1,j} = j;
            for k=1:length(index)
                trialOnsets(1,k)=double(num(index(k),9));
            end
            onsets{1,j} = trialOnsets;
        end
        % save new data for SPM
        save(fileName,'names','onsets','durations');
        clear names onsets durations fileName num raw text
    end
    

    我无法测试此代码,因为没有样本数据。如果这对您有用,请告诉我。