Add data from struct with dynamic field to column of a matrix in a for loop
1 view (last 30 days)
Show older comments
Mackenzie Taylor
on 24 Oct 2021
Answered: Pranjal Kaura
on 28 Oct 2021
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 Comments
Stephen23
on 25 Oct 2021
"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.
Accepted Answer
Pranjal Kaura
on 28 Oct 2021
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 Comments
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!