Add data from struct with dynamic field to column of a matrix in a for loop
조회 수: 5 (최근 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!