MATLAB: How to extract partial data from a matrix field of an array of structures

arrayextractMATLABmatrixpartialstruct

I have an array of structures with a field that stores a matrix in each record of the structure, as follows:
a(1).matrixfield = [1 2 3; 4 5 6; 7 8 9];
a(2).matrixfield = [4 1 2; 5 6 7; 2 6 8];
a(3).matrixfield = [5 1 2; 1 6 2; 1 7 1];
I would like to extract the matrix element (1,3) of "matrixfield" of all records in "a". In other words, I would like to be able to use a command such as:
a.matrixfield(1,3)

Best Answer

The ability to extract partial data from a matrix field of an array of structures is not available in MATLAB.
To work around this issue, use matrix manipulations to extract the desired data. In this example, the following code can extract the matrix element (1,3) of matrixfield of all records in "a":
b = [a.matrixfield];
b(1, 3:3:end)
The above code will horizontally concatenate all of the matrices into a single matrix, "b". Now, since all of the matrices had 3 columns, the desired elements will be in columns 3,6 and 9, i.e. positions (1,3), (1,6), and (1,9), which can be accessed in a single command by creating a vector of column indices.