I have a 1x5 structure array called Game
with two fields i.e.
Game(5) = struct(Points, Scorers);
Now, I also have a cell-array (5x2 cell array) (imported from xlsread
- so its all in cell-array form).
pts = [1 2;3 4;5 6;7 8;9 10];
How should I go about assigning each row of pts
, to each of the 5 structures in Game
, respectively?
For example: Game(3).Points
should be row 3
of pts
(which is [5 6]
).
Game(2).Points
should be [3 4]
. Game(1).Points
will be [1 2]
.
If your worksheet is organized such that rows correspond to observations and columns correspond to variables - e.g. points (numeric) and scorers (string) - you can import the data into Matlab using:
[pts, scr] = xlsread(file);
Then you can simply read the matrix pts
and cell array scr
into each field of a structure array as:
Game = struct('Points', num2cell(pts,2), 'Scorers', scr);
This takes advantage of struct()
's built-in ability to match output dimensions to its inputs, avoiding the use of a for
loop to iteratively assign imported values to the fields.