MATLAB: Do I get incorrect results when I create a function under the /@gf class directory in the Communications Toolbox 2.1 (R13)

classcommunicationsCommunications Toolboxgfsubsreftoolbox

I have created a function called firstcol.m. The function returns the first column of the input matrix.
function B = firstcol(A);
B = A(:,1);
I saved this MATLAB file to "../my_work/@gf/" directory. I have added the path "../my_work/" to the MATLAB search path. When I type the following code in the MATLAB command window:
A = gf(randint(5,5,[0,15]),4)
B = firstcol(A)
it returns the entire matrix A except for the first column.
If I modify the function to the following and test the same code in the MATLAB command window, it returns the first column of A correctly.
function B = firstcol(A);
B = subsref(A, substruct('()', {':',1}));

Best Answer

This issue is actually not a bug, it is a feature of MATLAB object oriented programming.
The basic problem is that whenever you are working with a function inside the @gf directory, MATLAB interprets the Galois Field object as a structure instead of an object. For almost all purposes, there is no difference, with the exception of indexing. Since the indexing functions can also be overloaded (via the SUBSREF function), allowing indexing inside the @gf directory could lead to an infinite loop, so it was not allowed.
The workaround to this particular problem is fairly simple, since inside the directory, you treat the Galois field object as a simple structure, you can just access its fields directly. Try the following function instead:
function B = firstcol(A);
B = gf(A.x(:,1),A.m, A.prim_poly);
Notice, accessing and indexing into the fields of the structure is allowed directly, that is not allowed outside of the @gf directory.