MATLAB Answers

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

2 views (last 30 days)
Claire Wilson
Claire Wilson on 18 Apr 2018
Commented: Stephen Cobeldick on 18 Apr 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 Comments

Claire Wilson
Claire Wilson on 18 Apr 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,
Stephen Cobeldick
Stephen Cobeldick on 18 Apr 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.

Sign in to comment.

Answers (1)

njj1
njj1 on 18 Apr 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.

  0 Comments

Sign in to comment.

Sign in to answer this question.