
%% Create similar OutputPort1 struct
OutputPort1 = InputPort1;


%% Mode Measure of Fundamental Mode

    %There are 12 modes in the corresponding OptiSystem file. Here we
    %choose to measure the fundamental mode. 
Fundmode = InputPort1.Sampled(1);

%Extract data from struct
    %Field Amplitude
    Ef = Fundmode.Spatial.ModeX.Amplitude;
    
    %Spatial Increments and grid
    dx = Fundmode.Spatial.ModeX.DeltaSpaceX;
    dy = Fundmode.Spatial.ModeX.DeltaSpaceY;
    
    xvector = linspace(-dx*0.5*size(Ef,2),dx*0.5*size(Ef,2), size(Ef,2));
    yvector = linspace(-dy*0.5*size(Ef,1),dy*0.5*size(Ef,1), size(Ef,1));
    
    [xgrid,ygrid] = meshgrid(xvector,yvector);
    
    %Mode Title
    Modetitle = Fundmode.Spatial.ModeX.Properties;
    
    %Field Amplitude
    Ef = Fundmode.Spatial.ModeX.Amplitude;  
    
% figure(1)
% 
% surf(xgrid,ygrid,Ef)
% title(Modetitle);

%% Calculate Mode Measure (Effective Area)

Integralsquared = trapz(xvector,trapz(yvector,Ef.^2,1));
Integralquad = trapz(xvector,trapz(yvector,Ef.^4,1));

Aeff = ((Integralsquared).^2./(Integralquad));

%save('yourdirectory\filename.mat','Aeff')

