Add data from struct with dynamic field to column of a matrix in a for loop

2 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 25 de Oct. de 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.
Mackenzie Taylor
Mackenzie Taylor el 25 de Oct. de 2021
I'm sorry this is is my first time posting a question here.
The structure would be complicated to replicate in a working example, but:
grouplevel.agegroup.young.(YOUNG{s}).Occipital.Var is calling to one of the last fields of a nested structure. Under grouplevel.agegroup.young I have 73 1x1 structs that each represent a participant from the YOUNG list (which is stored as a 1x73 cell). Within each of these participant structs I have two more 1x1 structs representing different brain regions (here i'm referencing Occipital) and then within these brain regions structs there are cell arrays representing different values within these regions. The one I am trying to call here, Var, is a 492x1 cell where each row is a variance value at time points 1 to 492. I am trying to concatenate the participants' Var data into a single matrix so that I can more easily perform additional calculations. I would like to create a matrix where each column holds a participants' data from the Var cell array.
I have tried variations of the code posted above and I get either this warning: "Subscripted assignment dimension mismatch." or the output is not what I would expect, where the matrix is only partially populated with 28 out of 73 participants' data from the Var cell array.
Please, let me know if there is any more information I can provide.

Iniciar sesión para comentar.

Respuesta aceptada

Pranjal Kaura
Pranjal Kaura el 28 de Oct. de 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!

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by