Borrar filtros
Borrar filtros

How to assign fields from a Struct into every 5th column of a Matrix

1 visualización (últimos 30 días)
Claire Wilson
Claire Wilson el 18 de Abr. de 2018
Comentada: Stephen23 el 18 de Abr. de 2018
Hi,
I'm relatively new to Matlab so forgive me if the answer is obvious.
I have created a struct which contains four arrays of numbers. I want to extract one of the arrays called Box and place each row of the array in every 5th column of a Matrix.
I have tried the following code;
c = max(a);
arraywidth = 5 * length(NumberSpot)-1;
arrayResult = NaN(c,arraywidth);
for IndexloopStruct = 1:length(structInterp)
List = structInterp(IndexloopStruct).Box;
for IndexloopFn = 1:5:(arraywidth)
arrayResult(1:length(List),IndexloopFn)= List;
end
end
However, I don't get the output I want - I get the last row of the struct repeated in every 5th column of the matrix.
I understand that its just cycling through my first loop and that's why I get the last row of the struct repeated in every 5th column of the Matrix, but I can't figure out how to fix it.
I've tried alternating the loops and order but can't seem to get it right.I either get 1 array of the last struct row or a separate array for each row of the struct.
I would very much appreciate any help.
Many thanks in advance.
  3 comentarios
Claire Wilson
Claire Wilson el 18 de Abr. de 2018
Editada: Stephen23 el 18 de Abr. de 2018
It will be the other columns of the struct. so;
box1 1:5:(arraywidth)
box2 2:6:(arraywidth)
box3 3:7:(arraywidth)
box4 4:8:(arraywidth)
then the 5th should just be a column of NaNs.
thanks,
Stephen23
Stephen23 el 18 de Abr. de 2018
@Claire Wilson: I don't really follow. Your question states that "I want to extract one of the arrays called Box and place each row of the array in every 5th column of a Matrix", but now you state that in fact "5th should just be a column of NaNs."
So should the 5th column be data from box, as your originally requested, or filled with NaN as you are now requesting? What about the other columns?
Perhaps it might be simpler if you showed some example data, with sample input values and the corresponding output.

Iniciar sesión para comentar.

Respuestas (1)

njj1
njj1 el 18 de Abr. de 2018
It is often helpful to step through your code and examine the variables at each step. One problem could be the shape of your variable "List", which is also "structInterp(IndexloopStruct).Box". If this is a row vector, then it may be trying to fill it in as a row vector. If this is the case, then you can simply transpose List by calling List'. Another possibility is that you are not changing the first iteration number in your second for loop (for IndexloopFn = 1:5:(arraywidth)). This always starts with IndexloopFn = 1, which means you will always call columns 1,6,11,16,... If this is the case, you could simply call your for loop as: for IndexloopFn = IndexloopStruct:5:(arraywidth). This will produce sequences: 1,6,11,16,..., then 2,7,12,17,..., then 3,8,13,18,... and so on.
If not these problems, it's difficult to judge your problem exactly, but I encourage you to step through your code by clicking on the left side of the editor and creating a red dot on the line where you would like Matlab to pause.

Categorías

Más información sobre Loops and Conditional Statements 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