classdef Emit_GUI < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        UIAxes                         matlab.ui.control.UIAxes
        ExperimentLabel                matlab.ui.control.Label
        expDropDown                    matlab.ui.control.DropDown
        CameraDropDownLabel            matlab.ui.control.Label
        CameraDropDown                 matlab.ui.control.DropDown
        dataSetIDLabel                 matlab.ui.control.Label
        dataSetVal                     matlab.ui.control.NumericEditField
        ImageEditFieldLabel            matlab.ui.control.Label
        ImageEditField                 matlab.ui.control.NumericEditField
        PlotButton                     matlab.ui.control.Button
        DataSetInfoTextAreaLabel       matlab.ui.control.Label
        DataSetInfoTextArea            matlab.ui.control.TextArea
        SelectDataSetLabel             matlab.ui.control.Label
        LoadDataSetButton              matlab.ui.control.Button
        DANlogTextAreaLabel            matlab.ui.control.Label
        DANlogTextArea                 matlab.ui.control.TextArea
        EnergyCalibrationPanel         matlab.ui.container.Panel
        DipoleStrengthGeVEditFieldLabel  matlab.ui.control.Label
        DipoleStrengthGeVEditField     matlab.ui.control.NumericEditField
        NominalDispersionmmEditFieldLabel  matlab.ui.control.Label
        NominalDispersionmmEditField   matlab.ui.control.NumericEditField
        GeVBeamPositionmmEditFieldLabel  matlab.ui.control.Label
        GeVBeamPositionmmEditField     matlab.ui.control.NumericEditField
        ToggleECalButton               matlab.ui.control.StateButton
        EnergyAxisTicksEditFieldLabel  matlab.ui.control.Label
        EnergyAxisTicksEditField       matlab.ui.control.NumericEditField
        ECallogTextAreaLabel           matlab.ui.control.Label
        ECallogTextArea                matlab.ui.control.TextArea
        ChargePlotButton               matlab.ui.control.Button
        AutoPopulateButton             matlab.ui.control.Button
        RecordPanel                    matlab.ui.container.Panel
        SavedfilenameEditFieldLabel    matlab.ui.control.Label
        SavedfilenameEditField         matlab.ui.control.EditField
        SaveplotdataButton             matlab.ui.control.Button
        PrinttologbookButton           matlab.ui.control.Button
        UIAxes2                        matlab.ui.control.UIAxes
        ResetButton                    matlab.ui.control.Button
        RotatedCheckBox                matlab.ui.control.CheckBox
        UIAxes3                        matlab.ui.control.UIAxes
        BeamWidthPlotSettingsPanel     matlab.ui.container.Panel
        minEEditFieldLabel             matlab.ui.control.Label
        minEEditField                  matlab.ui.control.NumericEditField
        maxEEditFieldLabel             matlab.ui.control.Label
        maxEEditField                  matlab.ui.control.NumericEditField
        PlotGaussianButton             matlab.ui.control.Button
        UsechargeplottodetermineROIenterinGeVLabel  matlab.ui.control.Label
        NotePlottingmaytakeaminuteorlongerLabel  matlab.ui.control.Label
    end

    
    properties (Access = private)
        aobj % Description
        yOut % Description
    end

    
    methods (Access = private)
        
        function applyECal(app, dipE, dnom, Offset, Eticks)
                
               
                y = yticks(app.UIAxes);
                E = abs((dipE * dnom) ./ (Offset + y));
                minE = min(E);
                maxE = max(E);
                
                step = (maxE -   minE) / (Eticks - 1);
                tickPos = minE:step:maxE;
                
                %If you want even integer spacing on energy axis,
                %uncomment roundFLipTick and replace flipTick in
                %yticklabels;
                roundFlipTick = round(fliplr(tickPos));
                flipTick = fliplr(tickPos);
                yt = interp1(E, y, flipTick);
                yticks(app.UIAxes, yt);
                yticklabels(app.UIAxes, num2str(flipTick'));
                
                
                
 
        end


    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.aobj = AppSupportTemplate(app);
           
        end

        % Value changed function: expDropDown
        function expDropDownValueChanged(app, event)
            value = app.expDropDown.Value;
            app.aobj.data.exp = value;
        end

        % Value changed function: dataSetVal
        function dataSetValValueChanged(app, event)
            value = app.dataSetVal.Value;
            app.aobj.data.dataSetID = value;
        end

        % Button pushed function: PlotButton
        function PlotButtonPushed(app, event)
            exp = app.expDropDown.Value;
            dataSetID = app.dataSetVal.Value;
            [data_struct, header] = dataYum(app.aobj, dataSetID, exp);
            cam = app.CameraDropDown.Value;
            n = app.ImageEditField.Value;
            if app.RotatedCheckBox.Value == 1
                isrot = 1;
            elseif app.RotatedCheckBox.Value == 0 
                isrot = 0;
            end

            try


            [img, ~, ~, ~, ymm, xmm] = dataYummy(app.aobj, app, data_struct, header, cam, n, isrot);
            app.UIAxes.Title.String = sprintf('exp: %s, dataSetID: %s, cam: %s:, image: %s', exp, num2str(dataSetID), cam, num2str(n)); 
            plotty(app.aobj, xmm, ymm, img, app);
            

            
            
            
            catch
                    
            app.DANlogTextArea.Value = 'No images availble for selected inputs.';
            end
            
        end

        % Button pushed function: LoadDataSetButton
        function LoadDataSetButtonPushed(app, event)
            exp = app.expDropDown.Value;
            dataSetID = app.dataSetVal.Value;
            try
                [data_struct, header] = dataYum(app.aobj, dataSetID, exp);
                app.DANlogTextArea.Value = 'Data set successfully loaded.';
                
                %Load Cameras:
                Cams = fieldnames(data_struct.images);
                app.DataSetInfoTextArea.Value = 'Available cameras sucessfully loaded.';
                app.CameraDropDown.Items = Cams;

            catch 
                 app.DANlogTextArea.Value = 'Invalid data set.';
                 app.DataSetInfoTextArea.Value = 'No cameras listed.';
            end
             
              
            
        end

        % Button pushed function: PrinttologbookButton
        function PrinttologbookButtonPushed(app, event)
            fh = figure(101);
            newAx = axes;
            
            yyaxis(app.UIAxes,'right');
            ylabel(newAx, '', 'Interpreter', 'none');
            yyaxis(app.ImageAxes,'left');
            
            copyobj(app.ImageAxes.Children, newAx);
            uiAxParams = get(app.ImageAxes);
            uiAxParamsNames = fieldnames(uiAxParams);
            editableParams = fieldnames(set(newAx));
            
            badFields = uiAxParamsNames(~ismember(uiAxParamsNames, editableParams));
            badFields = [badFields; 'Parent'; 'Children'; 'XAxis'; ...
                'YAxis'; 'ZAxis'; 'Position'; 'OuterPosition'; ...
                'InnerPosition'];
            
            uiAxGoodParams = rmfield(uiAxParams, badFields);
            listOfProps = fieldnames(uiAxGoodParams);
%             for k = 1:numel(listOfProps)
% %                 disp(listOfProps{k})
%                 newAx.(listOfProps{k}) = uiAxGoodParams.(listOfProps{k});
%             end
            
            titleS = app.ImageAxes.Title.String;
            xlabS = app.ImageAxes.XLabel.String;
            ylabS = app.ImageAxes.YLabel.String;
            yyaxis(app.ImageAxes,'right');
            ylab2S = app.ImageAxes.YLabel.String;
            yyaxis(app.ImageAxes,'left');

            set(newAx, uiAxGoodParams);
            %coptiyobj(uiAxGoodParams,newAx);

            
            print(fh, '-dpsc2', ['-P','physics-facetlog']);
%             opts.title = 'DAN picture';
%             opts.author = 'Matlab';
%             opts.text = '';
%             util_printLog(fh,opts);
            close(fh)
            title(app.ImageAxes, titleS, 'Interpreter', 'none');
            xlabel(app.ImageAxes, xlabS, 'Interpreter', 'none');
            ylabel(app.ImageAxes, ylabS, 'Interpreter', 'none');
            yyaxis(app.ImageAxes,'right');
            ylabel(app.ImageAxes, ylab2S, 'Interpreter', 'none');
            yyaxis(app.ImageAxes,'left');
            
       
            
            info_str = sprintf('%s %s', xlabS, ylabS);
            
            set(gcf,'Position',[10 10 10 10]);
            util_printLog2020(fh,'title',titleS,...
            'author','DAN','text',info_str);
            clf(99), close(99);
        end

        % Value changed function: ToggleECalButton
        function ToggleECalButtonValueChanged(app, event)
            value = app.ToggleECalButton.Value;
            


            if value == 1
                
                dipE = app.DipoleStrengthGeVEditField.Value;
                dnom = app.NominalDispersionmmEditField.Value;
                Eticks = app.EnergyAxisTicksEditField.Value;
                ybeam = app.GeVBeamPositionmmEditField.Value;
                
                
                %Calibration for E vs x
                yyaxis(app.UIAxes, 'right');
                app.UIAxes.YLabel.String = 'Energy [GeV]';
              
                
                Offset = abs(dnom)*dipE/10 - ybeam;
                applyECal(app, dipE, dnom, Offset, Eticks); 
                
                %Calibration for E vs Charge
                y = xticks(app.UIAxes2);
                E = abs((dipE * dnom) ./ (Offset + y));
                minE = min(E);
                maxE = max(E);
                
                step = (maxE -   minE) / (Eticks - 1);
                tickPos = maxE:-step:minE;
                roundTick = round(tickPos, 4, 'significant');

                yt = interp1(E, y, tickPos);
                xticks(app.UIAxes2, yt);
                xticklabels(app.UIAxes2, roundTick);
                app.UIAxes2.XLabel.String = 'Energy [GeV]';
            
                
 
                
            end
                
                

            if value == 0
                    
                %E vs x 
                yyaxis(app.UIAxes, 'right');
                yticks(app.UIAxes, 'auto');
                yticklabels(app.UIAxes, 'auto');
                ylim(app.UIAxes, 'auto');
                
                app.UIAxes.YLabel.String = 'y position [mm]';
                
                %E vs charge
                xticks(app.UIAxes2, 'auto');
                xticklabels(app.UIAxes2, 'auto');
                xlim(app.UIAxes2, 'auto');
                
                app.UIAxes2.XLabel.String = 'y position [mm]';
                
                
                
            end
        end

        % Value changed function: CameraDropDown
        function CameraDropDownValueChanged(app, event)
            value = app.CameraDropDown.Value;
            
            switch value
                case 'LFOV'
                    app.ECallogTextArea.Value = 'For selected camera; dnom = -58.1, 10 GeV Beam Position = 3.4';
                    app.NominalDispersionmmEditField.Value = -58.1;
                    app.GeVBeamPositionmmEditField.Value = 3.4;
                    app.RotatedCheckBox.Value = 1;
                  
                case 'DTOTR1'
                    app.ECallogTextArea.Value = 'For selected camera; dnom = -55.9, 10 GeV Beam Position =  1.3';
                    app.NominalDispersionmmEditField.Value = -55.9;
                    app.GeVBeamPositionmmEditField.Value = 1.3;
                    app.RotatedCheckBox.Value = 0;
                    
                case 'DTOTR2' 
                    app.ECallogTextArea.Value = 'For selected camera; dnom = -55.9, 10 GeV Beam Position = 17.8';
                    app.NominalDispersionmmEditField.Value = -55.9;
                    app.GeVBeamPositionmmEditField.Value = 17.8;
                    app.RotatedCheckBox.Value = 1;
                    
                case 'CHER'
                    app.ECallogTextArea.Value = 'For selected camera; dnom = -61.7, 10 GeV Beam Position = UPDATE';
                    app.NominalDispersionmmEditField.Value = -61.7;
                    app.GeVBeamPositionmmEditField.Value = 3.4;
                    app.RotatedCheckBox.Value = 0;
                    
                case 'PRDMP'
                    app.ECallogTextArea.Value = 'For selected camera; dnom = -69.5, 10 GeV Beam Position = UPDATE';
                    app.NominalDispersionmmEditField.Value = -69.5;
                    app.GeVBeamPositionmmEditField.Value = 3.4;
                    app.RotatedCheckBox.Value = 0;
                   
                otherwise
                    app.ECallogTextArea.Value = 'For selected camera; Unknown calibration';
            end
            
        end

        % Button pushed function: AutoPopulateButton
        function AutoPopulateButtonPushed(app, event)
            exp = app.expDropDown.Value;
            dataSetID = app.dataSetVal.Value;
            [data_struct, ~] = dataYum(app.aobj, dataSetID, exp);
            
            n = app.ImageEditField.Value;
            
            try
            dip = data_struct.scalars.nonBSA_List_S20Magnets.LI20_LGPS_3330_BACT(n);
            app.DipoleStrengthGeVEditField.Value = dip;
            app.ECallogTextArea.Value = 'Calibration values successfully loaded.';
                
            catch
            app.ECallogTextArea.Value = 'Could not locate specific calibration values for selected data set - Dipole Strength set to 10 GeV';
                
            end
            %use data_struct.images.'Camera' to find common indices 
        end

        % Button pushed function: ResetButton
        function ResetButtonPushed(app, event)
            app.DANlogTextArea.Value = '';
            app.DataSetInfoTextArea.Value = '';
            app.ECallogTextArea.Value = '';
            
            yyaxis(app.UIAxes, 'left');
            cla(app.UIAxes);
            
            yyaxis(app.UIAxes, 'right');
            cla(app.UIAxes);
            
            cla(app.UIAxes2);
        end

        % Button pushed function: ChargePlotButton
        function ChargePlotButtonPushed(app, event)
            exp = app.expDropDown.Value;
            dataSetID = app.dataSetVal.Value;
            [data_struct, header] = dataYum(app.aobj, dataSetID, exp);
            cam = app.CameraDropDown.Value;
            n = app.ImageEditField.Value;
            isrot = 0;
            
            
            %Note: xmm and ymm are switched in dataYummy to align with LFOV,
            %not sure if this will work for other cameras   
            [img, ~, ~, ~, ymm, ~] = dataYummy(app.aobj, app, data_struct, header, cam, n, isrot);
            
            plot(app.UIAxes2, ymm, sum(img, 2));
            


        end

        % Button pushed function: PlotGaussianButton
        function PlotGaussianButtonPushed(app, event)
            exp = app.expDropDown.Value;
            dataSetID = app.dataSetVal.Value;
            [data_struct, header] = dataYum(app.aobj, dataSetID, exp);
            cam = app.CameraDropDown.Value;
            n = app.ImageEditField.Value;
            isrot = 0;  
            
            dipE = app.DipoleStrengthGeVEditField.Value;
            dnom = app.NominalDispersionmmEditField.Value;
            ybeam = app.GeVBeamPositionmmEditField.Value;
            
            [img, x, y, ~, ymm, xmm] = dataYummy(app.aobj, app, data_struct, header, cam, n, isrot);
            
            % Loop through all possible y-values and create line profiles
            for i = 1:length(y)
                chosen_y = find(y == i);
                % Extract the row of data corresponding to the chosen x-value
                chosen_row = double(img(i,:));
                dy = ones(1,length(xmm));
                y_off = 1;
                
                [yfit,q,dq,chisq_ndf] = gauss_fit(xmm,chosen_row,dy,y_off);
                
                sigma_x(i) = q(4);
                
                
            end
           
            minE = app.minEEditField.Value;
            maxE = app.maxEEditField.Value;
            
            Offset = abs(dnom)*dipE/10 - ybeam;
            E = abs((dipE * dnom) ./ (Offset + ymm));
            
            plot(app.UIAxes3, E, sigma_x);
            xlim(app.UIAxes3, [minE, maxE]);
            

                


        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 1199 917];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.Scrollable = 'on';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, '')
            xlabel(app.UIAxes, 'x position [mm]')
            ylabel(app.UIAxes, 'y position [mm]')
            app.UIAxes.Position = [621 508 520 330];

            % Create ExperimentLabel
            app.ExperimentLabel = uilabel(app.UIFigure);
            app.ExperimentLabel.HorizontalAlignment = 'right';
            app.ExperimentLabel.Position = [51 816 67 22];
            app.ExperimentLabel.Text = 'Experiment';

            % Create expDropDown
            app.expDropDown = uidropdown(app.UIFigure);
            app.expDropDown.Items = {'SELECT', 'E300', 'E301', 'E305', 'E308', 'E320', 'E325', 'E326', 'E327', 'E331', 'E332'};
            app.expDropDown.ValueChangedFcn = createCallbackFcn(app, @expDropDownValueChanged, true);
            app.expDropDown.Position = [132 816 124 22];
            app.expDropDown.Value = 'SELECT';

            % Create CameraDropDownLabel
            app.CameraDropDownLabel = uilabel(app.UIFigure);
            app.CameraDropDownLabel.HorizontalAlignment = 'right';
            app.CameraDropDownLabel.Position = [41 636 49 22];
            app.CameraDropDownLabel.Text = {'Camera'; ''};

            % Create CameraDropDown
            app.CameraDropDown = uidropdown(app.UIFigure);
            app.CameraDropDown.Items = {'Load Data', ''};
            app.CameraDropDown.ValueChangedFcn = createCallbackFcn(app, @CameraDropDownValueChanged, true);
            app.CameraDropDown.Position = [101 636 100 22];
            app.CameraDropDown.Value = 'Load Data';

            % Create dataSetIDLabel
            app.dataSetIDLabel = uilabel(app.UIFigure);
            app.dataSetIDLabel.HorizontalAlignment = 'right';
            app.dataSetIDLabel.Position = [281 816 59 22];
            app.dataSetIDLabel.Text = 'dataSetID';

            % Create dataSetVal
            app.dataSetVal = uieditfield(app.UIFigure, 'numeric');
            app.dataSetVal.Limits = [0 Inf];
            app.dataSetVal.ValueDisplayFormat = '%d';
            app.dataSetVal.ValueChangedFcn = createCallbackFcn(app, @dataSetValValueChanged, true);
            app.dataSetVal.Position = [352 814 100 22];
            app.dataSetVal.Value = 3355;

            % Create ImageEditFieldLabel
            app.ImageEditFieldLabel = uilabel(app.UIFigure);
            app.ImageEditFieldLabel.HorizontalAlignment = 'right';
            app.ImageEditFieldLabel.Position = [221 636 40 22];
            app.ImageEditFieldLabel.Text = {'Image'; ''; ''};

            % Create ImageEditField
            app.ImageEditField = uieditfield(app.UIFigure, 'numeric');
            app.ImageEditField.Limits = [1 Inf];
            app.ImageEditField.Position = [274 636 100 22];
            app.ImageEditField.Value = 1;

            % Create PlotButton
            app.PlotButton = uibutton(app.UIFigure, 'push');
            app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
            app.PlotButton.Position = [481 636 100 23];
            app.PlotButton.Text = 'Plot';

            % Create DataSetInfoTextAreaLabel
            app.DataSetInfoTextAreaLabel = uilabel(app.UIFigure);
            app.DataSetInfoTextAreaLabel.HorizontalAlignment = 'right';
            app.DataSetInfoTextAreaLabel.Position = [39 779 72 22];
            app.DataSetInfoTextAreaLabel.Text = 'DataSet Info';

            % Create DataSetInfoTextArea
            app.DataSetInfoTextArea = uitextarea(app.UIFigure);
            app.DataSetInfoTextArea.Position = [35 684 275 94];

            % Create SelectDataSetLabel
            app.SelectDataSetLabel = uilabel(app.UIFigure);
            app.SelectDataSetLabel.Position = [44 851 101 22];
            app.SelectDataSetLabel.Text = 'Select Data Set:';

            % Create LoadDataSetButton
            app.LoadDataSetButton = uibutton(app.UIFigure, 'push');
            app.LoadDataSetButton.ButtonPushedFcn = createCallbackFcn(app, @LoadDataSetButtonPushed, true);
            app.LoadDataSetButton.Position = [481 815 100 22];
            app.LoadDataSetButton.Text = 'Load DataSet';

            % Create DANlogTextAreaLabel
            app.DANlogTextAreaLabel = uilabel(app.UIFigure);
            app.DANlogTextAreaLabel.HorizontalAlignment = 'right';
            app.DANlogTextAreaLabel.Position = [322 779 52 22];
            app.DANlogTextAreaLabel.Text = 'DAN log';

            % Create DANlogTextArea
            app.DANlogTextArea = uitextarea(app.UIFigure);
            app.DANlogTextArea.Position = [320 686 284 94];

            % Create EnergyCalibrationPanel
            app.EnergyCalibrationPanel = uipanel(app.UIFigure);
            app.EnergyCalibrationPanel.Title = 'Energy Calibration';
            app.EnergyCalibrationPanel.Position = [21 369 584 233];

            % Create DipoleStrengthGeVEditFieldLabel
            app.DipoleStrengthGeVEditFieldLabel = uilabel(app.EnergyCalibrationPanel);
            app.DipoleStrengthGeVEditFieldLabel.HorizontalAlignment = 'right';
            app.DipoleStrengthGeVEditFieldLabel.Position = [34 158 123 22];
            app.DipoleStrengthGeVEditFieldLabel.Text = 'Dipole Strength [GeV]';

            % Create DipoleStrengthGeVEditField
            app.DipoleStrengthGeVEditField = uieditfield(app.EnergyCalibrationPanel, 'numeric');
            app.DipoleStrengthGeVEditField.Limits = [0 Inf];
            app.DipoleStrengthGeVEditField.Position = [172 158 100 22];
            app.DipoleStrengthGeVEditField.Value = 10;

            % Create NominalDispersionmmEditFieldLabel
            app.NominalDispersionmmEditFieldLabel = uilabel(app.EnergyCalibrationPanel);
            app.NominalDispersionmmEditFieldLabel.HorizontalAlignment = 'right';
            app.NominalDispersionmmEditFieldLabel.Position = [14 128 142 22];
            app.NominalDispersionmmEditFieldLabel.Text = 'Nominal Dispersion [mm]';

            % Create NominalDispersionmmEditField
            app.NominalDispersionmmEditField = uieditfield(app.EnergyCalibrationPanel, 'numeric');
            app.NominalDispersionmmEditField.Position = [172 128 100 22];
            app.NominalDispersionmmEditField.Value = -50;

            % Create GeVBeamPositionmmEditFieldLabel
            app.GeVBeamPositionmmEditFieldLabel = uilabel(app.EnergyCalibrationPanel);
            app.GeVBeamPositionmmEditFieldLabel.HorizontalAlignment = 'right';
            app.GeVBeamPositionmmEditFieldLabel.Position = [-6 98 157 22];
            app.GeVBeamPositionmmEditFieldLabel.Text = '10 GeV Beam Position [mm]';

            % Create GeVBeamPositionmmEditField
            app.GeVBeamPositionmmEditField = uieditfield(app.EnergyCalibrationPanel, 'numeric');
            app.GeVBeamPositionmmEditField.Position = [172 98 100 22];
            app.GeVBeamPositionmmEditField.Value = 5;

            % Create ToggleECalButton
            app.ToggleECalButton = uibutton(app.EnergyCalibrationPanel, 'state');
            app.ToggleECalButton.ValueChangedFcn = createCallbackFcn(app, @ToggleECalButtonValueChanged, true);
            app.ToggleECalButton.Text = {'Toggle ECal'; ''};
            app.ToggleECalButton.Position = [172 22 100 30];

            % Create EnergyAxisTicksEditFieldLabel
            app.EnergyAxisTicksEditFieldLabel = uilabel(app.EnergyCalibrationPanel);
            app.EnergyAxisTicksEditFieldLabel.HorizontalAlignment = 'right';
            app.EnergyAxisTicksEditFieldLabel.Position = [44 68 102 22];
            app.EnergyAxisTicksEditFieldLabel.Text = 'Energy-Axis Ticks';

            % Create EnergyAxisTicksEditField
            app.EnergyAxisTicksEditField = uieditfield(app.EnergyCalibrationPanel, 'numeric');
            app.EnergyAxisTicksEditField.Limits = [1 Inf];
            app.EnergyAxisTicksEditField.Position = [172 68 100 22];
            app.EnergyAxisTicksEditField.Value = 9;

            % Create ECallogTextAreaLabel
            app.ECallogTextAreaLabel = uilabel(app.EnergyCalibrationPanel);
            app.ECallogTextAreaLabel.HorizontalAlignment = 'right';
            app.ECallogTextAreaLabel.Position = [288 176 52 22];
            app.ECallogTextAreaLabel.Text = 'ECal log';

            % Create ECallogTextArea
            app.ECallogTextArea = uitextarea(app.EnergyCalibrationPanel);
            app.ECallogTextArea.Position = [288 72 283 103];

            % Create ChargePlotButton
            app.ChargePlotButton = uibutton(app.EnergyCalibrationPanel, 'push');
            app.ChargePlotButton.ButtonPushedFcn = createCallbackFcn(app, @ChargePlotButtonPushed, true);
            app.ChargePlotButton.Position = [288 22 100 30];
            app.ChargePlotButton.Text = 'Charge Plot';

            % Create AutoPopulateButton
            app.AutoPopulateButton = uibutton(app.EnergyCalibrationPanel, 'push');
            app.AutoPopulateButton.ButtonPushedFcn = createCallbackFcn(app, @AutoPopulateButtonPushed, true);
            app.AutoPopulateButton.Position = [172 187 100 22];
            app.AutoPopulateButton.Text = 'Auto Populate';

            % Create RecordPanel
            app.RecordPanel = uipanel(app.UIFigure);
            app.RecordPanel.Title = 'Record';
            app.RecordPanel.Position = [933 97 223 113];

            % Create SavedfilenameEditFieldLabel
            app.SavedfilenameEditFieldLabel = uilabel(app.RecordPanel);
            app.SavedfilenameEditFieldLabel.HorizontalAlignment = 'right';
            app.SavedfilenameEditFieldLabel.Position = [11 61 96 22];
            app.SavedfilenameEditFieldLabel.Text = 'Saved file name:';

            % Create SavedfilenameEditField
            app.SavedfilenameEditField = uieditfield(app.RecordPanel, 'text');
            app.SavedfilenameEditField.Position = [18 38 100 22];
            app.SavedfilenameEditField.Value = 'myPlotName';

            % Create SaveplotdataButton
            app.SaveplotdataButton = uibutton(app.RecordPanel, 'push');
            app.SaveplotdataButton.Position = [18 7 100 23];
            app.SaveplotdataButton.Text = 'Save plot data';

            % Create PrinttologbookButton
            app.PrinttologbookButton = uibutton(app.RecordPanel, 'push');
            app.PrinttologbookButton.ButtonPushedFcn = createCallbackFcn(app, @PrinttologbookButtonPushed, true);
            app.PrinttologbookButton.BackgroundColor = [0 1 0];
            app.PrinttologbookButton.Position = [136 7 76 70];
            app.PrinttologbookButton.Text = {'Print to '; 'logbook'};

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, '')
            xlabel(app.UIAxes2, '')
            ylabel(app.UIAxes2, 'Charge [au]')
            app.UIAxes2.Position = [624 229 520 260];

            % Create ResetButton
            app.ResetButton = uibutton(app.UIFigure, 'push');
            app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
            app.ResetButton.Position = [1081 9 75 69];
            app.ResetButton.Text = {'Reset'; ''};

            % Create RotatedCheckBox
            app.RotatedCheckBox = uicheckbox(app.UIFigure);
            app.RotatedCheckBox.Text = 'Rotated';
            app.RotatedCheckBox.Position = [401 636 65 22];

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.UIFigure);
            title(app.UIAxes3, '')
            xlabel(app.UIAxes3, 'Beam Energy [Gev]')
            ylabel(app.UIAxes3, 'Beam Width ')
            app.UIAxes3.Position = [21 15 585 321];

            % Create BeamWidthPlotSettingsPanel
            app.BeamWidthPlotSettingsPanel = uipanel(app.UIFigure);
            app.BeamWidthPlotSettingsPanel.Title = 'Beam Width Plot Settings';
            app.BeamWidthPlotSettingsPanel.Position = [632 26 280 184];

            % Create minEEditFieldLabel
            app.minEEditFieldLabel = uilabel(app.BeamWidthPlotSettingsPanel);
            app.minEEditFieldLabel.HorizontalAlignment = 'right';
            app.minEEditFieldLabel.Position = [10 102 36 22];
            app.minEEditFieldLabel.Text = 'min E';

            % Create minEEditField
            app.minEEditField = uieditfield(app.BeamWidthPlotSettingsPanel, 'numeric');
            app.minEEditField.Position = [61 102 100 22];

            % Create maxEEditFieldLabel
            app.maxEEditFieldLabel = uilabel(app.BeamWidthPlotSettingsPanel);
            app.maxEEditFieldLabel.HorizontalAlignment = 'right';
            app.maxEEditFieldLabel.Position = [6 71 40 22];
            app.maxEEditFieldLabel.Text = 'max E';

            % Create maxEEditField
            app.maxEEditField = uieditfield(app.BeamWidthPlotSettingsPanel, 'numeric');
            app.maxEEditField.Position = [61 71 100 22];

            % Create PlotGaussianButton
            app.PlotGaussianButton = uibutton(app.BeamWidthPlotSettingsPanel, 'push');
            app.PlotGaussianButton.ButtonPushedFcn = createCallbackFcn(app, @PlotGaussianButtonPushed, true);
            app.PlotGaussianButton.Position = [61 29 100 23];
            app.PlotGaussianButton.Text = {'Plot Gaussian'; ''};

            % Create UsechargeplottodetermineROIenterinGeVLabel
            app.UsechargeplottodetermineROIenterinGeVLabel = uilabel(app.BeamWidthPlotSettingsPanel);
            app.UsechargeplottodetermineROIenterinGeVLabel.Position = [9 132 261 22];
            app.UsechargeplottodetermineROIenterinGeVLabel.Text = 'Use charge plot to determine ROI, enter in GeV';

            % Create NotePlottingmaytakeaminuteorlongerLabel
            app.NotePlottingmaytakeaminuteorlongerLabel = uilabel(app.BeamWidthPlotSettingsPanel);
            app.NotePlottingmaytakeaminuteorlongerLabel.Position = [10 6 234 22];
            app.NotePlottingmaytakeaminuteorlongerLabel.Text = 'Note: Plotting may take a minute or longer';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Emit_GUI

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end