You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: magtable.m
    path of file: ./repos/spinw/swfiles/@spinw
    
    the code of the file until where you have to start completion: function moments = magtable(obj)
% creates tabulated list of all magnetic moments stored in obj
%
% moments = MAGTABLE(obj)
%
% The function lists the APPROXIMATED moment directions (using the rotating
% coordinate system notation) in the magnetic supercell, whose size is
% defined by the obj.mag_str.nExt field. The positions of the magnetic
% atoms are in lattice units.
%
% Input:
%
% obj           spinw class object.
%
% Output:
%
% 'moments' is struct type data that contains the following fields:
%   M           Matrix, where every column defines a magnetic moment,
%               dimensions are [3 nMagExt].
%   e1,e2,e3    Unit vectors of the coordinate system used for the spin
%               wave calculation, the i-th column contains a normalized
%               vector for the i-th moment. e3 is parallel to the magnetic
%               moment, e1 and e2 span a right handed orthogonal coordinate
%               system.
%   R           Matrix, where every column defines the position of the
%               magnetic atom in lattice units.
%   atom        Pointer to the magnetic atom in the subfields of
%               spinw.unit_cell.
%
% See also SPINW.GENMAGSTR.
%

M0 = obj.magstr.S;

    
function moments = magtable(obj)
% creates tabulated list of all magnetic moments stored in obj
%
% moments = MAGTABLE(obj)
%
% The function lists the APPROXIMATED moment directions (using the rotating
% coordinate system notation) in the magnetic supercell, whose size is
% defined by the obj.mag_str.nExt field. The positions of the magnetic
% atoms are in lattice units.
%
% Input:
%
% obj           spinw class object.
%
% Output:
%
% 'moments' is struct type data that contains the following fields:
%   M           Matrix, where every column defines a magnetic moment,
%               dimensions are [3 nMagExt].
%   e1,e2,e3    Unit vectors of the coordinate system used for the spin
%               wave calculation, the i-th column contains a normalized
%               vector for the i-th moment. e3 is parallel to the magnetic
%               moment, e1 and e2 span a right handed orthogonal coordinate
%               system.
%   R           Matrix, where every column defines the position of the
%               magnetic atom in lattice units.
%   atom        Pointer to the magnetic atom in the subfields of
%               spinw.unit_cell.
%
% See also SPINW.GENMAGSTR.
%

M0 = obj.magstr.S;
S0 = sqrt(sum(M0.^2,1));
nMagExt = size(M0,2);

% Local (e1,e2,e3) coordinate system fixed to the moments.
if obj.symbolic
    e3 = simplify(M0./[S0; S0; S0]);
    % e2 = Si x [1,0,0], if Si || [1,0,0] --> e2 = [0,0,1]
    e2  = [zeros(1,nMagExt); e3(3,:); -e3(2,:)];
    % select zero vector and make them parallel to [0,0,1]
    selidx = abs(e2)>0;
    if isa(selidx,'sym')
        e2(3,~any(~sw_always(abs(e2)==0))) = 1;
    else
        e2(3,~any(abs(e2)>0)) = 1;
    end