如何在MATLAB中遍历嵌套的struct字段 [英] How to loop through nested struct fields in MATLAB
问题描述
我有几个主题的数据,每个主题都对一项活动进行了多次试验.我已经将所有数据读入格式的结构中 subject(1).trial(1).something subject(1).trial(2).somethingelse 等
I have data on several subjects that each performed several trials of an activity. I have read all the data into structs in the format subject(1).trial(1).something subject(1).trial(2).somethingelse etc.
现在,我需要将每个试验连续读入一个大矩阵[A],以便对每个试验进行一些计算,就好像该主题无关紧要.所以我从这里开始:
Now I need to read each of the trials into a row a big matrix [A] to perform some calculations on each trial, as if the subject didn't matter. So I started with this:
for i = 2:numSubjects
for j = 1:numTrials
A(j,:) = cat(2,subject(i).trial(j).torque_integral, subject(i).trial(j).work_integral);
end
end
但这仅适用于第一个主题.当主题(i)增至3时,试验(j)将恢复为1.因此,想法是输出A像这样排列:
But this will only work for the first subject. When the subject (i) increments to 3, the trial (j) will be back at one. So the idea is the output A lines up like this:
subject | trial | A
subject|trial|A
1 1 1
1 2 2
1 3 3
2 1 4
2 2 5
2 3 6
希望这很清楚.有什么想法吗?
Hopefully this is clear. Any thoughts?
推荐答案
如果我正确阅读了您的问题,则需要将每个试验都放在一行中,因此在使A变量时,不要使用j作为索引,而只需使用一个新的索引... 大声笑似乎有人在我之前发布了同样的内容...
If i read your question correctly, you need to put each trial in one row, so when make your A variable, instead of using j as index, just use a new index... lol seems like someone just posted the same thing before me...
m=1
for i = 2:numSubjects
for j = 1:numTrials
A(m,:) = cat(2,subject(i).trial(j).torque_integral,subject(i).trial(j).work_integral);
m=m+1;
end
end
这篇关于如何在MATLAB中遍历嵌套的struct字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!