Add data from struct with dynamic field to column of a matrix in a for loop
4 ビュー (過去 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!