0

I would like to build a nested struct using a nested for loop. The structure I want would first split according to direction (one of 8 directions), and then each direction would have two fields. I have tried something like:

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

I get an error saying "Field assignment to a non-structure array object". I need to use the nested for loop because other data manipulation is happening inside the for loops to give me the values for spikeTimes and smoothedTimes. I've read the documentation for creating structs, but can't figure out how this nested struct can be constructed inside the for loops.

1 Answer 1

3

How about this:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.