Add data from struct with dynamic field to column of a matrix in a for loop
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to create a matrix from data currently stored under a dyanmic field in a matlab struct. I need each column of the matrix to represent a subjects data for 492 time points.
Here is what I am currently trying:
all_time_points2 = zeros(73,492);
for s=1:length(YOUNG)
all_time_points2(s,:) = cell2mat(grouplevel.agegroup.young.(YOUNG{s}).Occipital.Var);
end
Any help would be greatly appreciated!
2 个评论
Stephen23
2021-10-25
"Here is what I am currently trying:"
You showed us a tiny bit of code, but you did not explain what problem(s) you want us to help you with: do you get e.g. unexpected data values, unexpected data sequence, unexpected warnings, or unexpected error messages? If so, please tell us the complete message. Even better: provide us with a complete working example that replicates the problem.
采纳的回答
Pranjal Kaura
2021-10-28
Hey Mackenzie,
You can refer to the following code snippet wherein I've tried to replicate your usecase and provided a solution.
n = 5; %492 for you
num_Young = 3; %73 participants for you
test_BR1 = struct('Var',{cell(n,1)}, 'x', {cell(n, 1)},'y', {cell(n, 1)}); %assume Var is the first entry here. This here represents brain region(BR) in your example
test_BR2 = struct('Var',{cell(n,1)}, 'x', {cell(n, 1)},'y', {cell(n, 1)});
test_BR3 = struct('Var',{cell(n,1)}, 'x', {cell(n, 1)},'y', {cell(n, 1)});
test_Young = {test_BR1, test_BR2, test_BR3};
test_Output = zeros(n, num_Young); %each column holds participant Var data, therefore num_Rows = num_Var_Entries
for i1 = 1:num_Young
test_Young{i1}.Var = i1:i1 + 4;
end
for i1 = 1:num_Young
test_Output(:, i1) = test_Young{i1}.Var;
end
Hope this helps!
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!