简体   繁体   English

分配给结构数组时在Matlab中“索引超过矩阵维数”

[英]“Index exceeds matrix dimensions” in Matlab when assigning to a struct array

I am trying to assign values to an array contained within a struct in Matlab. 我正在尝试将值分配给Matlab中的结构内包含的数组。 I am perplexed by the "Index exceeds matrix dimensions" error, because as far as I can tell, I am following the example of the official documentation and doing something that has worked for me in the past. 我对“索引超出矩阵维数”错误感到困惑,因为据我所知,我正在遵循官方文档的示例并做了过去对我有用的事情。 I hope this is enough relevant context: 我希望这是足够相关的上下文:

for m = 1:tally
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).scans = files{m};
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(1).name = 'button_press';
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(1).onset = events.bp.onsets{m};
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(1).duration = 0;
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(1).tmod = 0;
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(1).pmod = struct('name', {}, 'param', {}, 'poly', {});

    sess_onsets = events.conditions.sessions(tally).onsets{1};
    for n = 1:numel(sess_onsets)
        len = numel(sess_onsets{n});
        cond_list = events.conditions.names{tally};
        cond_onsets = sess_onsets{n};
        matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).name = cond_list{n};
        matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).onset = cond_onsets;
        matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).duration = repmat([4], len, 1);
        matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).tmod = 0;
        matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).pmod = struct('name', {}, 'param', {}, 'poly', {});
    end

    matlabbatch{2}.spm.stats.fmri_spec.sess(m).multi = {''};
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).regress = struct('name', {}, 'val', {});
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).multi_reg = rp{k};
    matlabbatch{2}.spm.stats.fmri_spec.sess(m).hpf = 128;
end

The error comes on this line: 该行出现错误:

matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).name = cond_list{n};

When I step to this line in the debugger, it is this expression 当我在调试器中进入此行时,它是此表达式

matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1)

that gives the "index exceeds matrix dimensions error." 给出“索引超出矩阵尺寸错误”。 It does this even when n is 1. Abstractly, based on my experience with other languages, that makes sense. 即使当n为1时,它也会这样做。抽象地,根据我对其他语言的经验,这是有道理的。 Cond is length 1 and I'm trying to assign to the second slot. Cond的长度为1,我正在尝试分配给第二个插槽。 But it doesn't make sense to me based on my experience with and (obviously flawed) knowledge of arrays in Matlab structs. 但是根据我对Matlab结构中数组的经验和(显然有缺陷的)知识,对我来说这没有意义。 In the past I've just made a struct array longer by announcing I'm going to with the new subscript. 过去,我通过声明要使用新的下标来延长结构数组的长度。

Here are Matlab's instructions for how to assign to a struct . 这是Matlab有关如何分配给struct的说明 The only difference I can see is that they don't use the (1) subscript the first time they assign to the array, and I tried that and it made no difference. 我能看到的唯一区别是,他们在第一次分配给数组时就没有使用(1)下标,而我尝试了一下,这没有任何区别。

This code works fine: 这段代码可以正常工作:

>> a.b(1).name = 'apple';
>> a.b(2).name = 'banana'
a = b: [1x2 struct]

I am struggling to understand the difference. 我正在努力了解差异。 Any assistance you can provide is greatly appreciated, and please let me know if I need to give more information. 非常感谢您提供的任何帮助,如果需要提供更多信息,请告诉我。 Thanks! 谢谢!

Thanks very much to all who tried to help me. 非常感谢所有尝试帮助我的人。 I am feeling like a doof, and somewhat guilty for not communicating accurate information to people who were trying to help me. 我感到自己像个傻瓜,因为没有向试图帮助我的人传达准确的信息而感到内gui。 This question should have been titled "I don't know how to use a debugger." 该问题的标题应该为“我不知道如何使用调试器”。 I thought it was failing on the first execution because when I stepped to that line and pasted in 我以为它在第一次执行时就失败了,因为当我走到那一行并粘贴时

matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1) 

I got an error. 我有一个错误。 But I now understand that the reason for that error is that pasting in that expression does not do the work of creating the next array element -- I needed 但是我现在知道该错误的原因是,粘贴该表达式不会完成创建下一个数组元素的工作-我需要

matlabbatch{2}.spm.stats.fmri_spec.sess(m).cond(n+1).name = foo

to make that element exist. 使该元素存在。 If I had clicked "continue" I would have realized that the real failure was in cond_list{n} when n = 5. This was just a conceptual failure in my code, and not anything anyone else would likely have recognized. 如果单击“继续”,我将意识到真正的失败是在n = 5时在cond_list {n}中。这只是我的代码中的概念性失败,其他任何人都不会意识到。 Thanks very much for your time, and I'm sorry for leading you astray with my claims. 非常感谢您抽出宝贵的时间,对于您的说法误导您,我们深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM