classdef main < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                      matlab.ui.Figure
        InstructionFigWeldpenetrationsimulationLabel  matlab.ui.control.Label
        BackpoolwidthEditField        matlab.ui.control.NumericEditField
        BackpoolwidthEditFieldLabel   matlab.ui.control.Label
        ExampleDropDown               matlab.ui.control.DropDown
        ExampleDropDownLabel          matlab.ui.control.Label
        InitialcurrentEditField       matlab.ui.control.NumericEditField
        InitialcurrentEditFieldLabel  matlab.ui.control.Label
        InitialspeedEditField         matlab.ui.control.NumericEditField
        InitialspeedEditFieldLabel    matlab.ui.control.Label
        WeldingcurrentEditField       matlab.ui.control.NumericEditField
        WeldingcurrentEditFieldLabel  matlab.ui.control.Label
        WeldingspeedEditField         matlab.ui.control.NumericEditField
        WeldingspeedEditFieldLabel    matlab.ui.control.Label
        Panel                         matlab.ui.container.Panel
        WeldingParameterControlPanel  matlab.ui.container.Panel
        UIAxes_4                      matlab.ui.control.UIAxes
        UIAxes_3                      matlab.ui.control.UIAxes
        WeldingPenetrationSimulationPanel  matlab.ui.container.Panel
        UIAxes_5                      matlab.ui.control.UIAxes
        UIAxes                        matlab.ui.control.UIAxes
        AcousticandImageSignalPanel   matlab.ui.container.Panel
        RobotstatusButton             matlab.ui.control.Button
        MoreacousticanalysisButton    matlab.ui.control.Button
        MoreimageanalysisButton       matlab.ui.control.Button
        UIAxes3                       matlab.ui.control.UIAxes
        UIAxes2                       matlab.ui.control.UIAxes
        StartButton                   matlab.ui.control.Button
    end

%     properties (Access = private)
%         acousticanalysis
%         imageanalysis
%         robot
%         Recognitionmodel
%     end

    properties (Access = public)
        loopCompleted = false; 
        acousticanalysis
        imageanalysis
        robot
%         Recognitionmodel
        edge
    end

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

        % Button pushed function: StartButton
        function StartButtonPushed(app, event)
        load dataimgaco.mat
        
        value = app.ExampleDropDown.Value;
             switch value
                 case 'Example 1'
                     ExI = 80;
                     ExV = 0.05;
                     Exy = 12;
                     n1 = size(img1);
                     n = n1(1,2);
                     chose = img1;
                     choseaco = a1;
                     app.edge = 1;
                     adjustj = 9;
                 case 'Example 2'
                     ExI = 80;
                     ExV = 1.4;
                     Exy = 5.25;
                     n1 = size(img2);
                     n = n1(1,2);
                     chose = img2;
                     choseaco = a2;
                     app.edge = 2;
                     adjustj = 31;
                 case 'Example 3'
                     ExI = 190;
                     ExV = 2.75;
                     Exy = 6.2;
                     n1 = size(img3);
                     n = n1(1,2);
                     chose = img3;
                     choseaco = a3;
                     app.edge = 3;
                     adjustj = 34;
             end
             
        app.InitialcurrentEditField.Value = ExI;
        app.InitialspeedEditField.Value = ExV;
        
        [resulty, resultV, resultI] = Recmodel(Exy,ExI,ExV);
        
%         n1 = size(hunimg801);
%         n = n1(1,2);
        linex = 1:n*adjustj;
        bliney = 5*ones(1,n*adjustj);
        eliney = 3.2*ones(1,n*adjustj);
        iliney = 2.6*ones(1,n*adjustj);
        
        for i = 1:n
            
        imshow(chose{i}, 'Parent', app.UIAxes3);
        
        plot(app.UIAxes2,choseaco(i,:));
        
        plot(app.UIAxes,resulty(1:adjustj*i),'LineWidth',2);
        
        plot(app.UIAxes_4,resultI(1:adjustj*i),'LineWidth',2);
        
        plot(app.UIAxes_3,resultV(1:adjustj*i),'LineWidth',2);
        
%         tabdata = [resultV(1:n),resultI(1:n),resulty(1:n)]
        app.WeldingcurrentEditField.Value = resultV(adjustj*i);
        app.WeldingspeedEditField.Value = resultI(adjustj*i);
        app.BackpoolwidthEditField.Value = resulty(adjustj*i);

        hold(app.UIAxes_5,'on');
        plot(app.UIAxes_5,linex,bliney,'k:', ...
            linex,-bliney,'k:', ...
            linex,-eliney,'k:', ...
            linex,eliney,'k:', ...
            linex,-iliney,'k:', ...
            linex,iliney,'k:', ...
            [0,i*adjustj],[0,0],'k--','LineWidth',1.5);
        hold(app.UIAxes_5,'on');
        if resulty(adjustj*i)/2 < 2.8
            plot(app.UIAxes_5,[adjustj*i,adjustj*i],[resulty(adjustj*i)/2,-resulty(adjustj*i)/2],'y');
            hold(app.UIAxes_5,'off');
        elseif resulty(adjustj*i)/2 > 2.8 && resulty(adjustj*i)/2 < 3.2
            plot(app.UIAxes_5,[adjustj*i,adjustj*i],[resulty(adjustj*i)/2,-resulty(adjustj*i)/2],'g');
            hold(app.UIAxes_5,'off');
        elseif resulty(adjustj*i)/2 >3.2 && resulty(adjustj*i)/2 < 5
            plot(app.UIAxes_5,[adjustj*i,adjustj*i],[resulty(adjustj*i)/2,-resulty(adjustj*i)/2],'b');
            hold(app.UIAxes_5,'off');
        elseif resulty(adjustj*i)/2 > 5
            plot(app.UIAxes_5,[adjustj*i,adjustj*i],[resulty(adjustj*i)/2,-resulty(adjustj*i)/2],'r');
            hold(app.UIAxes_5,'off');
        end
        pause(0.8);
        end
        
        app.MoreimageanalysisButton.Enable = 'on';
        app.MoreacousticanalysisButton.Enable = 'on';
        app.RobotstatusButton.Enable = 'on';
%         app.RecognitionmodelButton.Enable = 'on';
        app.loopCompleted = true;
        end

        % Button pushed function: MoreimageanalysisButton
        function MoreimageanalysisButtonPushed(app, event)
            if app.loopCompleted
            app.imageanalysis = image_analysis(app.edge);
            else
                disp('Please complete the main program first.');
            end
        end

        % Button pushed function: MoreacousticanalysisButton
        function MoreacousticanalysisButtonPushed(app, event)
            if app.loopCompleted
            app.acousticanalysis = acoustic_analysis(app.edge);
            else
                disp('Please complete the main program first.');
            end
        end

        % Button pushed function: RobotstatusButton
        function RobotstatusButtonPushed(app, event)
            if app.loopCompleted
            app.robot = robotstatus(app.edge);
            else
                disp('Please complete the main program first.');
            end
        end

        % Callback function
        function RecognitionmodelButtonPushed(app, event)
%             if app.loopCompleted
%             app.Recognitionmodel = recognitionmodel;
%             else
%                 disp('Please complete the main program first.');
%             end
        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 = [10 50 1250 760];
            app.UIFigure.Name = 'MATLAB App';

            % Create StartButton
            app.StartButton = uibutton(app.UIFigure, 'push');
            app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
            app.StartButton.FontSize = 24;
            app.StartButton.Position = [1050 31 140 50];
            app.StartButton.Text = 'Start';

            % Create AcousticandImageSignalPanel
            app.AcousticandImageSignalPanel = uipanel(app.UIFigure);
            app.AcousticandImageSignalPanel.Title = 'Acoustic and Image Signal';
            app.AcousticandImageSignalPanel.FontSize = 21;
            app.AcousticandImageSignalPanel.Position = [16 501 1218 246];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.AcousticandImageSignalPanel);
            title(app.UIAxes2, 'Acoustic Signal')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.XTick = [];
            app.UIAxes2.YTick = [];
            app.UIAxes2.FontSize = 18;
            app.UIAxes2.Position = [283 8 752 197];

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.AcousticandImageSignalPanel);
            title(app.UIAxes3, 'Welding Image Signal')
            zlabel(app.UIAxes3, 'Z')
            app.UIAxes3.XTick = [];
            app.UIAxes3.YTick = [];
            app.UIAxes3.FontSize = 18;
            app.UIAxes3.Position = [4 8 280 200];

            % Create MoreimageanalysisButton
            app.MoreimageanalysisButton = uibutton(app.AcousticandImageSignalPanel, 'push');
            app.MoreimageanalysisButton.ButtonPushedFcn = createCallbackFcn(app, @MoreimageanalysisButtonPushed, true);
            app.MoreimageanalysisButton.FontSize = 16;
            app.MoreimageanalysisButton.Position = [1050 147 160 50];
            app.MoreimageanalysisButton.Text = 'More image analysis';

            % Create MoreacousticanalysisButton
            app.MoreacousticanalysisButton = uibutton(app.AcousticandImageSignalPanel, 'push');
            app.MoreacousticanalysisButton.ButtonPushedFcn = createCallbackFcn(app, @MoreacousticanalysisButtonPushed, true);
            app.MoreacousticanalysisButton.FontSize = 16;
            app.MoreacousticanalysisButton.Position = [1050 83 160 50];
            app.MoreacousticanalysisButton.Text = 'More acoustic analysis';

            % Create RobotstatusButton
            app.RobotstatusButton = uibutton(app.AcousticandImageSignalPanel, 'push');
            app.RobotstatusButton.ButtonPushedFcn = createCallbackFcn(app, @RobotstatusButtonPushed, true);
            app.RobotstatusButton.FontSize = 16;
            app.RobotstatusButton.Position = [1050 19 160 50];
            app.RobotstatusButton.Text = 'Robot status';

            % Create WeldingPenetrationSimulationPanel
            app.WeldingPenetrationSimulationPanel = uipanel(app.UIFigure);
            app.WeldingPenetrationSimulationPanel.Title = 'Welding Penetration Simulation';
            app.WeldingPenetrationSimulationPanel.FontSize = 21;
            app.WeldingPenetrationSimulationPanel.Position = [16 23 480 465];

            % Create UIAxes
            app.UIAxes = uiaxes(app.WeldingPenetrationSimulationPanel);
            title(app.UIAxes, 'Back welding pool width(calculated value)')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.YLim = [5 12.5];
            app.UIAxes.XTick = [];
            app.UIAxes.YTick = [5 6.5 8 9.5 11 12.5];
            app.UIAxes.YTickLabel = {'5'; '6.5'; '8'; '9.5'; '11'; '12.5'};
            app.UIAxes.FontSize = 18;
            app.UIAxes.Position = [4 217 470 210];

            % Create UIAxes_5
            app.UIAxes_5 = uiaxes(app.WeldingPenetrationSimulationPanel);
            title(app.UIAxes_5, 'Welding penetration recognition simulation')
            zlabel(app.UIAxes_5, 'Z')
            app.UIAxes_5.YLim = [-6 6];
            app.UIAxes_5.XTick = [];
            app.UIAxes_5.YTick = [-5 -3.2 -2.8 0 2.8 3.2 5];
            app.UIAxes_5.YTickLabel = {'b'; 'e'; 'I'; '0'; 'I'; 'e'; 'b'};
            app.UIAxes_5.FontSize = 18;
            app.UIAxes_5.Position = [4 8 470 210];

            % Create WeldingParameterControlPanel
            app.WeldingParameterControlPanel = uipanel(app.UIFigure);
            app.WeldingParameterControlPanel.Title = 'Welding Parameter Control';
            app.WeldingParameterControlPanel.FontSize = 21;
            app.WeldingParameterControlPanel.Position = [514 23 480 465];

            % Create UIAxes_3
            app.UIAxes_3 = uiaxes(app.WeldingParameterControlPanel);
            title(app.UIAxes_3, 'Welding current control (A)')
            zlabel(app.UIAxes_3, 'Z')
            app.UIAxes_3.YLim = [70 200];
            app.UIAxes_3.XTick = [];
            app.UIAxes_3.YTick = [80 100 120 140 160 180 200];
            app.UIAxes_3.YTickLabel = {'80'; '100'; '120'; '140'; '160'; '180'; '200'};
            app.UIAxes_3.FontSize = 16;
            app.UIAxes_3.Position = [5 216 470 210];

            % Create UIAxes_4
            app.UIAxes_4 = uiaxes(app.WeldingParameterControlPanel);
            title(app.UIAxes_4, 'Welding speed control(mm/s)')
            zlabel(app.UIAxes_4, 'Z')
            app.UIAxes_4.YLim = [0.05 2.75];
            app.UIAxes_4.XTick = [];
            app.UIAxes_4.YTick = [0.05 0.5 0.95 1.4 1.85 2.3 2.75];
            app.UIAxes_4.YTickLabel = {'0.05'; '0.5'; '0.95'; '1.4'; '1.85'; '2.3'; '2.75'};
            app.UIAxes_4.FontSize = 16;
            app.UIAxes_4.Position = [4 8 470 210];

            % Create Panel
            app.Panel = uipanel(app.UIFigure);
            app.Panel.Position = [997 279 104 209];

            % Create WeldingspeedEditFieldLabel
            app.WeldingspeedEditFieldLabel = uilabel(app.UIFigure);
            app.WeldingspeedEditFieldLabel.HorizontalAlignment = 'right';
            app.WeldingspeedEditFieldLabel.Position = [1010 466 84 22];
            app.WeldingspeedEditFieldLabel.Text = 'Welding speed';

            % Create WeldingspeedEditField
            app.WeldingspeedEditField = uieditfield(app.UIFigure, 'numeric');
            app.WeldingspeedEditField.FontSize = 21;
            app.WeldingspeedEditField.Position = [1006 433 90 27];

            % Create WeldingcurrentEditFieldLabel
            app.WeldingcurrentEditFieldLabel = uilabel(app.UIFigure);
            app.WeldingcurrentEditFieldLabel.HorizontalAlignment = 'right';
            app.WeldingcurrentEditFieldLabel.Position = [1007 393 89 22];
            app.WeldingcurrentEditFieldLabel.Text = 'Welding current';

            % Create WeldingcurrentEditField
            app.WeldingcurrentEditField = uieditfield(app.UIFigure, 'numeric');
            app.WeldingcurrentEditField.FontSize = 21;
            app.WeldingcurrentEditField.Position = [1005 360 89 27];

            % Create InitialspeedEditFieldLabel
            app.InitialspeedEditFieldLabel = uilabel(app.UIFigure);
            app.InitialspeedEditFieldLabel.HorizontalAlignment = 'right';
            app.InitialspeedEditFieldLabel.Position = [1158 466 70 22];
            app.InitialspeedEditFieldLabel.Text = 'Initial speed';

            % Create InitialspeedEditField
            app.InitialspeedEditField = uieditfield(app.UIFigure, 'numeric');
            app.InitialspeedEditField.FontSize = 21;
            app.InitialspeedEditField.Position = [1151 436 69 27];

            % Create InitialcurrentEditFieldLabel
            app.InitialcurrentEditFieldLabel = uilabel(app.UIFigure);
            app.InitialcurrentEditFieldLabel.HorizontalAlignment = 'right';
            app.InitialcurrentEditFieldLabel.Position = [1156 393 74 22];
            app.InitialcurrentEditFieldLabel.Text = 'Initial current';

            % Create InitialcurrentEditField
            app.InitialcurrentEditField = uieditfield(app.UIFigure, 'numeric');
            app.InitialcurrentEditField.FontSize = 21;
            app.InitialcurrentEditField.Position = [1151 360 69 27];

            % Create ExampleDropDownLabel
            app.ExampleDropDownLabel = uilabel(app.UIFigure);
            app.ExampleDropDownLabel.HorizontalAlignment = 'right';
            app.ExampleDropDownLabel.FontSize = 21;
            app.ExampleDropDownLabel.Position = [1143 319 87 26];
            app.ExampleDropDownLabel.Text = 'Example';

            % Create ExampleDropDown
            app.ExampleDropDown = uidropdown(app.UIFigure);
            app.ExampleDropDown.Items = {'Example 1', 'Example 2', 'Example 3'};
            app.ExampleDropDown.FontSize = 21;
            app.ExampleDropDown.Position = [1142 291 88 27];
            app.ExampleDropDown.Value = 'Example 1';

            % Create BackpoolwidthEditFieldLabel
            app.BackpoolwidthEditFieldLabel = uilabel(app.UIFigure);
            app.BackpoolwidthEditFieldLabel.HorizontalAlignment = 'right';
            app.BackpoolwidthEditFieldLabel.Position = [1011 323 90 22];
            app.BackpoolwidthEditFieldLabel.Text = 'Back pool width';

            % Create BackpoolwidthEditField
            app.BackpoolwidthEditField = uieditfield(app.UIFigure, 'numeric');
            app.BackpoolwidthEditField.FontSize = 21;
            app.BackpoolwidthEditField.Position = [1005 290 89 27];

            % Create InstructionFigWeldpenetrationsimulationLabel
            app.InstructionFigWeldpenetrationsimulationLabel = uilabel(app.UIFigure);
            app.InstructionFigWeldpenetrationsimulationLabel.VerticalAlignment = 'top';
            app.InstructionFigWeldpenetrationsimulationLabel.WordWrap = 'on';
            app.InstructionFigWeldpenetrationsimulationLabel.FontSize = 17;
            app.InstructionFigWeldpenetrationsimulationLabel.Position = [1005 120 245 120];
            app.InstructionFigWeldpenetrationsimulationLabel.Text = {'Welding penetration recognition simulation : '; 'Red - burn through; '; 'Blue - excessive penetration;'; 'Green - complete penetration;'; 'Yellow - incomplete penetration'};

            % 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 = main

            % Create UIFigure and components
            createComponents(app)

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

            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