【问题标题】:Nested For Loop to build a struct嵌套 For 循环以构建结构
【发布时间】:2015-11-26 12:05:06
【问题描述】:

我想使用嵌套 for 循环构建嵌套结构。我想要的结构首先会根据方向(8 个方向之一)进行拆分,然后每个方向都有两个字段。我尝试过类似的方法:

for i=1:8
    data(i).direction=i;
    for j=1:numTrials
         data(i).direction(j).sp_time=spikeTimes
         data(i).direction(j).sm_time=smoothedTimes
    end
end

我收到一条错误消息,提示“将字段分配给非结构数组对象”。我需要使用嵌套的 for 循环,因为其他数据操作发生在 for 循环内,以便为我提供 peakTimes 和 smoothedTimes 的值。我已阅读有关创建结构的文档,但无法弄清楚如何在 for 循环中构造此嵌套结构。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    这个怎么样:

    for i=1:8
        % initialize to empty struct, rather than number
        data(i).direction = struct();
        for j=1:numTrials
             data(i).direction(j).sp_time=spikeTimes
             data(i).direction(j).sm_time=smoothedTimes
        end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-02
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2021-11-17
      • 2021-09-26
      相关资源
      最近更新 更多