    classdef Stand_SW < matlab.apps.AppBase

        % Properties that correspond to app components
        properties (Access = public)
            UIFigure          matlab.ui.Figure               % UI Figure
            RECFigure         matlab.ui.Figure               % REC Figure
            Plot              matlab.ui.control.UIAxes       % Current measuring
            LabelDropDown     matlab.ui.control.Label        % Motor Type
            Type              matlab.ui.control.DropDown     % Option 1, Option...
            LabelDiscreteKnob matlab.ui.control.Label        % Motor
            Motor             matlab.ui.control.DiscreteKnob % BLDC, ACTUATOR, DC
            Label             matlab.ui.control.Label        % Voltage
            ErrENC
            ErrCUR
            ErrTIME
            ErrFG
            ResN1             matlab.ui.control.Label        % name of first result
            ResV1             matlab.ui.control.Label        % value of first result
            ResN2             matlab.ui.control.Label        % name of first result
            ResV2             matlab.ui.control.Label        % value of first result
            ResN3             matlab.ui.control.Label        % name of first result
            ResV3             matlab.ui.control.Label        % value of first result
            ResN4             matlab.ui.control.Label        % name of first result
            ResV4             matlab.ui.control.Label        % value of first result
            Error             matlab.ui.control.Label        % value of first result
            Voltage           matlab.ui.control.DiscreteKnob % 36V, 24V, 12V, 6V
            LabelSwitch       matlab.ui.control.Label        % Direction of rot...
            Direction         matlab.ui.control.Switch       % CW, CCW
            LabelSwitch2      matlab.ui.control.Label        % Mode of operation
            Mode              matlab.ui.control.Switch       % Automatic, Manual
            LabelKnob         matlab.ui.control.Label        % Speed [%]
            Speed             matlab.ui.control.Knob         % [0 100]
            Start             matlab.ui.control.StateButton  % START
            LabelLamp         matlab.ui.control.Label        % Running
            LED               matlab.ui.control.Lamp
            Connect           matlab.ui.control.StateButton  % CONNECTED
            Label3            matlab.ui.control.Label        % Connected
            LED2              matlab.ui.control.Lamp
            Label4            matlab.ui.control.Label        % Time [s]
            Time              matlab.ui.control.Knob         % [0 100]
            REC               matlab.ui.control.StateButton  % REC
            Timer                                            % is measuring how often will STAND send data
            LTN            matlab.ui.control.Label        % Current CCW:    //left top name
            LBN            matlab.ui.control.Label        % Current CW:      //left bottom name
            LTV            matlab.ui.control.Label        % Value           //left top value
            LBV            matlab.ui.control.Label        % Value
            RTN            matlab.ui.control.Label        % Channel A:      //right top name
            RBN           matlab.ui.control.Label        % Channel B:
            LOW           matlab.ui.control.Label        % Low
            HIGH           matlab.ui.control.Label        % High
            V1           matlab.ui.control.Label        % 1
            V2           matlab.ui.control.Label        % 2
            V3           matlab.ui.control.Label        % 3
            V4           matlab.ui.control.Label        % 4
            MBV           matlab.ui.control.Label        % ECW
            MTV           matlab.ui.control.Label        % ECCW              //midle top value
            MTN           matlab.ui.control.Label        % Encoder CCW:
            MBN           matlab.ui.control.Label        % Encoder CW:

            MBBV           matlab.ui.control.Label        % ECW
            MTTV           matlab.ui.control.Label        % ECCW              //midle top value
            MTTN           matlab.ui.control.Label        % Encoder CCW:
            MBBN           matlab.ui.control.Label

            motor
            timerStep = 0.05;%period of timer
            type
            voltage
            speed
            direction
            mode
            com
            time
            start
            PlotTime
            PlotLength
            PlotCurrCW
            PlotPotCW   %variable to store potentiometr position on actiator with pot.
            PlotCurrCCW
            PlotPotCCW  %variable to store potentiometr position on actiator with pot.
            msg
            ECCW        %amount of encoder ticks CCW
            ECW         %amount of encoder ticks CW
            EAH         %encoder channel A high value
            EAL         %encoder channel A low value
            EBH         %encoder channel B high value
            EBL         %encoder channel B low value
            ACTTimeExt     %time needed for actuator to extend/retract
            ACTTimeRet
            ACTtime = 0;
            BLDCTimeCCW %period of FG signal from BLDC motor
            BLDCTimeCW
            meanCurrCCW
            meanCurrCW
            maxPot      %maximal position on potentiometr
            minPot      %minimal position on potentiometr
            indx = 1;
            ratio = 4;      %used for current recalculation from ADC result to [A]
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            FigSTART      matlab.ui.control.Button  % START
            FigCancel     matlab.ui.control.Button  % Cancel
            FigLabelListBox  matlab.ui.control.Label   % Motor
            FigMotor      matlab.ui.control.ListBox % Item 1, Item 2, Item 3, I...
            FigLabel         matlab.ui.control.Label   % Type
            FigType       matlab.ui.control.ListBox % Item 1, Item 2, Item 3, I...
            FigNew        matlab.ui.control.Button  % NEW
            FigContinue   matlab.ui.control.Button  % Continue
            FigLabel2        matlab.ui.control.Label   % Voltage
            FigVoltage    matlab.ui.control.ListBox % Item 1, Item 2, Item 3, I...
            FigLabelListBox4 matlab.ui.control.Label   % File
            FigFile       matlab.ui.control.ListBox % Item 1, Item 2, I
            LabelEditField matlab.ui.control.Label     % File Name
            EditField      matlab.ui.control.EditField
            fileID
            new = 1;                            %if new == 1 than operator wants to make new recording file
        end

        methods (Access = private)

            % Code that executes after component creation
            function startupFcn(app)
                app.Set();
            end

            % Motor value changed function
            function MotorValueChanged(app)
                value = app.Motor.Value;
                if (strcmp(value,'DC'))
                    app.Direction.Enable = 'on';
                    app.Type.Items = {'With_encoder', 'Without_encoder'};
                    app.Type.Value = 'With_encoder';
                    app.LabelSwitch.Text = {'Direction '; 'of '; 'rotation'};

                end
                if (strcmp(value,'ACTUATOR'))
                    app.Direction.Enable = 'on';
                    app.Type.Items = {'With_potentiometr', 'With_Hall_sensor', 'Without_encoder'};
                    app.Type.Value = 'With_potentiometr';
                    app.LabelSwitch.Text = {'Direction '; 'of '; 'rotation'};

                end
                if (strcmp(value,'BLDC'))
                    app.Type.Items = {'Without_FG', 'With_FG'};
                    app.Type.Value = 'With_FG';
                    app.Direction.Value = 'CW';
                    app.LabelSwitch.Text = {'Direction '; 'of BLDC'; 'rotation'};
                end

                app.Set();

            end

            % Voltage value changed function
            function VoltageValueChanged(app)
                value = app.Voltage.Value;
                app.Set();

            end

            % Mode value changed function
            function ModeValueChanged(app)
                value = app.Mode.Value;
                app.Set();

            end

            % Direction value changed function
            function DirectionValueChanged(app)
                value = app.Direction.Value;
                app.Set();

            end

            % Type value changed function
            function TypeValueChanged(app, event)
                value = app.Type.Value;
                app.Set();

            end

            % REC value changed function
            function RECValueChanged(app)
                value = app.REC.Value;
                if(value && (~isequal(app.REC.BackgroundColor,[1,0,0])))
                    SetFig(app);
                    createFigNew(app);
                else
                    app.REC.BackgroundColor = [1 1 1];
                    app.REC.Value = 0;
                    fclose(app.fileID);
                end

            end

            function SetFig(app)
                app.RECFigure.Visible = 'on';
                app.FigVoltage.Items = app.Voltage.Items;
                app.FigVoltage.Value = app.Voltage.Value;
                app.FigType.Items = app.Type.Items;
                app.FigType.Value = app.Type.Value;
                app.FigMotor.Items = app.Motor.Items;
                app.FigMotor.Value = app.Motor.Value;
            end

            %function witch sets STAND via USB communication
            function Set(app)
                app.Time.Visible = 'on';
                app.Label4.Visible = 'on';
                app.MBBN.Visible = 'off';
                app.MBBV.Visible = 'off';
                app.MTTN.Visible = 'off';
                app.MTTV.Visible = 'off';

                if (strcmp(app.Mode.Value, 'Automatic'))
                    app.Speed.Enable = 'off';
                    if (~strcmp(app.Motor.Value,'ACTUATOR'))
                        app.Time.Enable = 'on';
                    else
                        app.Time.Enable = 'off';
                    end
                    app.Direction.Enable = 'off';

                    if (strcmp(app.Motor.Value,'DC'))
                        app.motor = 'A';

                        if (strcmp(app.Type.Value,'With_encoder'))
                            app.type = 'A';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current CCW:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current CW:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';
                            app.RTN.Visible = 'on';
                            app.RTN.Text = 'Channel A:';
                            app.RBN.Visible = 'on';
                            app.RBN.Text = 'Channel B:';
                            app.LOW.Visible = 'on';
                            app.LOW.Text = 'Low';
                            app.HIGH.Visible = 'on';
                            app.HIGH.Text = 'High';
                            app.V1.Visible = 'on';
                            app.V1.Text = '---';
                            app.V2.Visible = 'on';
                            app.V2.Text = '---';
                            app.V3.Visible = 'on';
                            app.V3.Text = '---';
                            app.V4.Visible = 'on';
                            app.V4.Text = '---';
                            app.MBV.Visible = 'on';
                            app.MBV.Text = '---';
                            app.MTV.Visible = 'on';
                            app.MTV.Text = '---';
                            app.MTN.Visible = 'on';
                            app.MTN.Text = 'Encoder CCW:';
                            app.MBN.Visible = 'on';
                            app.MBN.Text = 'Encoder CW:';

                        elseif (strcmp(app.Type.Value,'Without_encoder'))
                            app.type = 'B';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current CCW:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current CW:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';
                            app.RTN.Visible = 'off';
                            app.RBN.Visible = 'off';
                            app.LOW.Visible = 'off';
                            app.HIGH.Visible = 'off';
                            app.V1.Visible = 'off';
                            app.V2.Visible = 'off';
                            app.V3.Visible = 'off';
                            app.V4.Visible = 'off';
                            app.MBV.Visible = 'off';
                            app.MTV.Visible = 'off';
                            app.MTN.Visible = 'off';
                            app.MBN.Visible = 'off';
                        end
                    elseif (strcmp(app.Motor.Value,'ACTUATOR'))
                        app.motor = 'B';

                        if (strcmp(app.Type.Value,'Without_encoder'))
                            app.type = 'A';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current EXT:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current RET:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';

                            app.MBV.Visible = 'on';
                            app.MBV.Text = '---';
                            app.MTV.Visible = 'on';
                            app.MTV.Text = '---';
                            app.MTN.Visible = 'on';
                            app.MTN.Text = 'Extending:';
                            app.MBN.Visible = 'on';
                            app.MBN.Text = 'Retracting:';

                            app.RTN.Visible = 'off';
                            app.RTN.Text = 'Channel A:';
                            app.RBN.Visible = 'off';
                            app.RBN.Text = 'Channel B:';
                            app.LOW.Visible = 'off';
                            app.LOW.Text = 'Low';
                            app.HIGH.Visible = 'off';
                            app.HIGH.Text = 'High';
                            app.V1.Visible = 'off';
                            app.V1.Text = '---';
                            app.V2.Visible = 'off';
                            app.V2.Text = '---';
                            app.V3.Visible = 'off';
                            app.V3.Text = '---';
                            app.V4.Visible = 'off';
                            app.V4.Text = '---';

                        elseif (strcmp(app.Type.Value,'With_potentiometr'))
                            app.type = 'B';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current EXT:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current RET:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';

                            app.MBV.Visible = 'on';
                            app.MBV.Text = '---';
                            app.MTV.Visible = 'on';
                            app.MTV.Text = '---';
                            app.MTN.Visible = 'on';
                            app.MTN.Text = 'Extending:';
                            app.MBN.Visible = 'on';
                            app.MBN.Text = 'Retracting:';

                            app.RTN.Visible = 'off';
                            app.RTN.Text = 'Channel A:';
                            app.RBN.Visible = 'on';
                            app.RBN.Text = 'Potentiometr:';
                            app.LOW.Visible = 'off';
                            app.LOW.Text = 'Low';
                            app.HIGH.Visible = 'off';
                            app.HIGH.Text = 'High';
                            app.V1.Visible = 'on';
                            app.V1.Text = 'MAX';
                            app.V2.Visible = 'on';
                            app.V2.Text = '---';
                            app.V3.Visible = 'on';
                            app.V3.Text = 'MIN';
                            app.V4.Visible = 'on';
                            app.V4.Text = '---';

                        elseif (strcmp(app.Type.Value,'With_Hall_sensor'))
                            app.type = 'C';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current EXT:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current RET:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';

                            app.MBV.Visible = 'on';
                            app.MBV.Text = '---';
                            app.MTV.Visible = 'on';
                            app.MTV.Text = '---';
                            app.MTN.Visible = 'on';
                            app.MTN.Text = 'Extending:';
                            app.MBN.Visible = 'on';
                            app.MBN.Text = 'Retracting:';

                            app.RTN.Visible = 'on';
                            app.RTN.Text = 'Channel A:';
                            app.RBN.Visible = 'on';
                            app.RBN.Text = 'Channel B:';
                            app.LOW.Visible = 'on';
                            app.LOW.Text = 'Low';
                            app.HIGH.Visible = 'on';
                            app.HIGH.Text = 'High';
                            app.V1.Visible = 'on';
                            app.V1.Text = '---';
                            app.V2.Visible = 'on';
                            app.V2.Text = '---';
                            app.V3.Visible = 'on';
                            app.V3.Text = '---';
                            app.V4.Visible = 'on';
                            app.V4.Text = '---';

                            app.Time.Visible = 'off';
                            app.Label4.Visible = 'off';
                            app.MBBN.Visible = 'on';
                            app.MBBV.Visible = 'on';
                            app.MBBV.Text = '---';
                            app.MTTN.Visible = 'on';
                            app.MTTV.Visible = 'on';
                            app.MTTV.Text = '---';

                        end
                    elseif (strcmp(app.Motor.Value,'BLDC'))
                        app.motor = 'C';

                        if (strcmp(app.Type.Value,'Without_FG'))
                            app.type = 'A';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current CCW:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current CW:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';

                            app.MBV.Visible = 'off';
                            app.MTV.Visible = 'off';
                            app.MTN.Visible = 'off';
                            app.MBN.Visible = 'off';

                            app.RTN.Visible = 'off';
                            app.RBN.Visible = 'off';
                            app.LOW.Visible = 'off';
                            app.HIGH.Visible = 'off';
                            app.V1.Visible = 'off';
                            app.V2.Visible = 'off';
                            app.V3.Visible = 'off';
                            app.V4.Visible = 'off';

                        elseif (strcmp(app.Type.Value,'With_FG'))
                            app.type = 'B';
                            app.LTN.Visible = 'on';
                            app.LTN.Text = 'Current CCW:';
                            app.LBN.Visible = 'on';
                            app.LBN.Text = 'Current CW:';
                            app.LTV.Visible = 'on';
                            app.LTV.Text = '---';
                            app.LBV.Visible = 'on';
                            app.LBV.Text = '---';

                            app.MBV.Visible = 'on';
                            app.MBV.Text = '---us';
                            app.MTV.Visible = 'on';
                            app.MTV.Text = '---us';
                            app.MTN.Visible = 'on';
                            app.MTN.Text = 'FG CCW:';
                            app.MBN.Visible = 'on';
                            app.MBN.Text = 'FG CW:';

                            app.RTN.Visible = 'on';
                            app.RBN.Visible = 'off';
                            app.RTN.Text = '           FG:';
                            app.LOW.Visible = 'on';
                            app.LOW.Text = 'Low';
                            app.HIGH.Visible = 'on';
                            app.HIGH.Text = 'High';
                            app.V1.Visible = 'on';
                            app.V1.Text = '---';
                            app.V2.Visible = 'off';
                            app.V3.Visible = 'on';
                            app.V3.Text = '---';
                            app.V4.Visible = 'off';
                        end
                    end
                 else
                    app.Speed.Enable = 'on';
                    app.Time.Enable = 'off';
                    app.Direction.Enable = 'on';
                    app.LTN.Visible = 'off';
                    app.LBN.Visible = 'off';
                    app.LTV.Visible = 'off';
                    app.LBV.Visible = 'off';
                    app.MBN.Visible = 'off';
                    app.MBV.Visible = 'off';
                    app.MTN.Visible = 'off';
                    app.MTV.Visible = 'off';
                    app.V4.Visible = 'off';
                    app.V3.Visible = 'off';
                    app.V2.Visible = 'off';
                    app.V1.Visible = 'off';
                    app.HIGH.Visible = 'off';
                    app.LOW.Visible = 'off';
                    app.RBN.Visible = 'off';
                    app.RTN.Visible = 'off';
                end

                if (strcmp(app.Voltage.Value,'6V'))
                    app.voltage = 'A';
                elseif (strcmp(app.Voltage.Value,'12V'))
                    app.voltage = 'B';
                elseif (strcmp(app.Voltage.Value,'24V'))
                    app.voltage = 'C';
                elseif (strcmp(app.Voltage.Value,'36V'))
                    app.voltage = 'D';
                end

                app.speed = int8(app.Speed.Value);
                app.time = int8(app.Time.Value);

                if(strcmp(app.Direction.Value,'CW'))
                    app.direction = 'A';
                else
                    app.direction = 'B';
                end

                if(strcmp(app.Mode.Value,'Automatic'))
                    app.mode = 'A';
                else
                    app.mode = 'B';
                end

                stop(app.Timer);
                STAND.Send(app.com,'U');
                STAND.Send(app.com,app.motor);
                STAND.Send(app.com,app.type);
                STAND.Send(app.com,app.voltage);
                STAND.Send(app.com,app.speed);
                STAND.Send(app.com,app.time);
                if(strcmp(app.Mode.Value,'Automatic'))
                    STAND.Send(app.com,'C');
                else
                    STAND.Send(app.com,app.direction);
                end
                STAND.Send(app.com,app.mode);
                if(app.Start.Value)
                    start(app.Timer);
                end

            end

            % Start value changed function
            function StartValueChanged(app)
                value = app.Start.Value;

                if(value)
                    app.LED.Color = [0,1,0];
                    STAND.Send(app.com,'S');
                    STAND.Clear(app.com);
                    app.Plot.reset;
                    app.Plot.Box = 'on';
                    app.Plot.XGrid = 'on';
                    app.Plot.YGrid = 'on';
                    app.PlotTime = 0;
                    app.PlotCurrCCW = 0;%zeros(1,app.PlotLength);
                    app.PlotCurrCW = 0; %zeros(1,app.PlotLength);
                    app.PlotPotCCW = 0;
                    app.PlotPotCW = 0;
                    app.indx = 1;
                    %app.Set();
                    app.Error.Visible = 'off';
                    app.ResN1.Visible = 'off';
                    app.ResV1.Visible = 'off';
                    app.ResN2.Visible = 'off';
                    app.ResV2.Visible = 'off';
                    app.ResN3.Visible = 'off';
                    app.ResV3.Visible = 'off';
                    app.ResN4.Visible = 'off';
                    app.ResV4.Visible = 'off';
                    start(app.Timer);

                else
                    app.LED.Color = [1,0,0];
                    STAND.Send(app.com,'s');
                    stop(app.Timer);
                end

            end

            % Speed value changing function
            function SpeedValueChanging(app, event)
                app.LabelKnob.Text = strcat('Speed [', num2str(event.Value,'%3.0f'), '%]');
            end

            % Speed value changed function
            function SpeedValueChanged(app)
                value = app.Speed.Value;
                app.Set();

            end

            % Connect value changed function
            function ConnectValueChanged(app)

                if(app.Connect.Value)
                    [~,isOK]=STAND.open(app.com);
                    if isOK
                        app.LED2.Color = [0, 1, 0];
                        app.Label3.Text = 'Connected';
                    else
                        app.LED2.Color = [1, 0, 0];
                        app.Label3.Text = 'Disconnected';
                        app.Connect.Value = 0;
                    end
                    STAND.Send(app.com,'X');
                else
                    STAND.Send(app.com,'Y');
                    [~,isOK]=STAND.close(app.com);
                    close all
                    if isOK
                        app.LED2.Color = [1, 0, 0];
                        app.Label3.Text = 'Disconnected';
                    end
                end
                app.Set();
            end

             % Time value changed function
            function TimeValueChanged(app)
                if(app.Time.Value < 2)
                    app.Time.Value = 2;
                elseif(app.Time.Value > 99)
                    app.Time.Value = 99;
                end
                app.Label4.Text = strcat('Time [', num2str(app.Time.Value,'%2.0f'), 's]');

    %             app.Plot.XLim = [0 app.Time.Value];
    %             app.PlotTime = (0:0.2:app.Time.Value);
    %             app.PlotLength = length(app.PlotTime);
    %             app.PlotCurrCCW = zeros(1,app.PlotLength);
    %             app.PlotCurrCW = zeros(1,app.PlotLength);
                app.Set();
            end

            % Time value changing function
            function TimeValueChanging(app, event)
                if(event.Value < 2)
                    app.Label4.Text = strcat('Time [', num2str(2), 's]');
                    return
                elseif(event.Value > 99)
                    app.Label4.Text = strcat('Time [', num2str(99), 's]');
                    return
                end
                app.Label4.Text = strcat('Time [', num2str(event.Value,'%2.0f'), 's]');
            end

            % Construct app
            function TimingFcn(app,~,~)
                app.msg = STAND.request(app.com,'E');
                disp(app.msg);

                if(strcmp(app.Mode.Value,'Automatic'))
                    if(strcmp(app.Motor.Value,'DC'))
                        if(strcmp(app.Type.Value,'With_encoder'))
                            if(app.msg(1,1) == 0)
                            elseif(app.msg(1,1) == 1)
                                app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;         % current flowing through motor while rotating CCW
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                app.indx = app.indx + 1;
                            elseif(app.msg(1,1) == 2)
                                app.ECCW = typecast(uint8(app.msg(1,2:5)), 'uint32');                           % number of ticks on encoder while rotating CCW
                                app.MTV.Text = [num2str(app.ECCW) 'ticks'];
                                app.PlotTime = 0;
                                app.indx = 1;
                            elseif(app.msg(1,1) == 3)
                                app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;           % current flowing through motor while rotating CW
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                app.indx = app.indx + 1;
                            elseif(app.msg(1,1) == 4)
                                maxlen = max(length(app.PlotCurrCCW), length(app.PlotCurrCW));                  % algorytm to print both Curr CCW/CW into one graph
                                app.PlotCurrCCW(end+1:maxlen) = 0;
                                app.PlotCurrCW(end+1:maxlen) = 0;
                                app.PlotTime(end+1:maxlen) = max(app.PlotTime);

                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                hold(app.Plot,'on');
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                title(app.Plot, 'Current measuring');
                                xlabel(app.Plot, 'time [s]');
                                ylabel(app.Plot, 'current [A]');
                                app.indx = 1;

                                app.meanCurrCCW = mean(app.PlotCurrCCW(floor(length(app.PlotCurrCCW)/2):end));                                        %calculating mean value of currents
                                app.LTV.Text = [num2str(app.meanCurrCCW,'%4.3f') 'A'];
                                app.meanCurrCW = mean(app.PlotCurrCW(floor(length(app.PlotCurrCW)/2):end));
                                app.LBV.Text = [num2str(app.meanCurrCW,'%4.3f') 'A'];

                                app.ECW = typecast(uint8(app.msg(1,2:5)), 'uint32');                            % number of ticks on encoder while rotating CW
                                app.MBV.Text = [num2str(app.ECW) 'ticks'];
                            elseif(app.msg(1,1) == 5)
                                app.EAL = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase A LOW
                                app.V1.Text = [num2str(app.EAL,'%1.1f') 'V'];
                                app.EAH = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase A HIGH
                                app.V3.Text = [num2str(app.EAH,'%1.1f') 'V'];
                            elseif(app.msg(1,1) == 6)
                                app.EBL = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase B LOW
                                app.V2.Text = [num2str(app.EBL,'%1.1f') 'V'];
                                app.EBH = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase B HIGH
                                app.V4.Text = [num2str(app.EBH,'%1.1f') 'V'];
                                if(app.REC.Value)                                    %information writen to corresponding .txt file while recording            m
                                    fprintf(app.fileID, '\r\n%15s %15s %20s %20s                                   %10s %10s                                   %10s %10s %20s', ...
                                app.LTV.Text, app.LBV.Text, num2str(app.ECCW), num2str(app.ECW), app.V1.Text, app.V3.Text, app.V2.Text, app.V4.Text, datestr(now,'yymmdd_HHMMSS'));
                                end
                            elseif(app.msg(1,1) == 7)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                app.ErrENC = uint8(app.msg(1,3));
                                if(app.ErrCUR && app.ErrENC)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    app.ResN3.Visible = 'on';
                                    app.ResV3.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                    if(app.ErrENC)
                                        app.ResV3.Text = 'OK';
                                    else
                                        app.ResV3.Text = 'NOT_OK';
                                    end
                                end
                            end
                        elseif(strcmp(app.Type.Value,'Without_encoder'))

                            if(app.msg(1,1) == 1)
                                app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;      % current flowing through motor while rotating CCW
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                app.indx = app.indx + 1;
                            elseif(app.msg(1,1) == 2)
                                app.indx = 1;
                                app.PlotTime = 0;
                            elseif(app.msg(1,1) == 3)
                                app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;        % current flowing through motor while rotating CCW
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                app.indx = app.indx + 1;
                            elseif(app.msg(1,1) == 4)
                                maxlen = max(length(app.PlotCurrCCW), length(app.PlotCurrCW));               % algorytm to print both Curr CCW/CW into one graph
                                app.PlotCurrCCW(end+1:maxlen) = 0;
                                app.PlotCurrCW(end+1:maxlen) = 0;
                                app.PlotTime(end+1:maxlen) = max(app.PlotTime);

                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                hold(app.Plot,'on');
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                title(app.Plot, 'Current measuring');
                                xlabel(app.Plot, 'time [s]');
                                ylabel(app.Plot, 'current [A]');

                                app.meanCurrCCW = mean(app.PlotCurrCCW(floor(length(app.PlotCurrCCW)/2):end));    %calculating mean currents                                     %calculating mean value of currents
                                app.LTV.Text = [num2str(app.meanCurrCCW,'%4.3f') 'A'];
                                app.meanCurrCW = mean(app.PlotCurrCW(floor(length(app.PlotCurrCW)/2):end));
                                app.LBV.Text = [num2str(app.meanCurrCW,'%4.3f') 'A'];
                                app.indx = 1;
                                app.REC.Value
                                app.fileID
                                if(app.REC.Value)
                                    fprintf(app.fileID, '\r\n%15s %15s %20s',app.LTV.Text, app.LBV.Text, datestr(now,'yymmdd_HHMMSS'));           %information writen to corresponding .txt file while recording
                                end
                            elseif(app.msg(1,1) == 5)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                if(app.ErrCUR)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                end
                            end

                        end
                    elseif(strcmp(app.Motor.Value,'ACTUATOR'))
                        if (strcmp(app.Type.Value,'Without_encoder'))
                            if(app.msg(1,1) == 1)

                            elseif(app.msg(1,1) == 2)
                                app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;      %measuring current while extending
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                app.indx = app.indx + 1;

                            elseif(app.msg(1,1) == 3)
                                app.ACTTimeExt = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %time needed to fully extend
                                app.MTV.Text = [num2str(app.ACTTimeExt) 'ms'];
                                app.indx = 1;
                                app.PlotTime = 0;
                            elseif(app.msg(1,1) == 4)
                                app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;       %measuring current while contracting
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                app.indx = app.indx + 1;

                            elseif(app.msg(1,1) == 5)
                                maxlen = max(length(app.PlotCurrCCW), length(app.PlotCurrCW));              %algorytm to print both Curr CCW/CW into one graph
                                app.PlotCurrCCW(end+1:maxlen) = 0;
                                app.PlotCurrCW(end+1:maxlen) = 0;
                                app.PlotTime(end+1:maxlen) = max(app.PlotTime);

                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                hold(app.Plot,'on');
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                title(app.Plot, 'Current measuring');
                                xlabel(app.Plot, 'time [s]');
                                ylabel(app.Plot, 'current [A]');

                                floor(length(app.PlotCurrCCW)/3)
                                app.meanCurrCCW = mean(app.PlotCurrCCW(floor(length(app.PlotCurrCCW)/3):end-20));    %calculating mean currents                                     %calculating mean value of currents
                                app.LTV.Text = [num2str(app.meanCurrCCW,'%4.3f') 'A'];
                                app.meanCurrCW = mean(app.PlotCurrCW(floor(length(app.PlotCurrCW)/3):end-20));
                                app.LBV.Text = [num2str(app.meanCurrCW,'%4.3f') 'A'];

                                app.ACTTimeRet = typecast(uint8(app.msg(1,2:5)), 'uint32');                   %time needed to fully retract
                                app.MBV.Text = [num2str(app.ACTTimeRet) 'ms'];

                                app.indx = 1;

                                if(app.REC.Value)                                   %information writen to corresponding .txt file while recording
                                    fprintf(app.fileID, '\r\n%17s %18s %20s %20s %20s', app.LBV.Text, app.LTV.Text, app.MTV.Text, app.MBV.Text, datestr(now,'yymmdd_HHMMSS'));
                                end
                            elseif(app.msg(1,1) == 6)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                app.ErrTIME = uint8(app.msg(1,3));
                                if(app.ErrCUR && app.ErrTIME)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    app.ResN2.Visible = 'on';
                                    app.ResV2.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                    if(app.ErrTIME)
                                        app.ResV2.Text = 'OK';
                                    else
                                        app.ResV2.Text = 'NOT_OK';
                                    end
                                end
                            end

                        elseif (strcmp(app.Type.Value,'With_potentiometr'))
                            if(app.msg(1,1) == 1)

                            elseif(app.msg(1,1) == 2)
                                app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;      %current while extending
                                app.PlotPotCCW(app.indx) = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095;       %potentiometr value
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW,app.PlotTime,app.PlotPotCCW);
    %                             hold(app.Plot,'on');
    %                             plot(app.Plot,app.PlotTime,app.PlotPotCCW);
                                app.indx = app.indx + 1;

                            elseif(app.msg(1,1) == 3)
                                app.ACTTimeExt = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %time needed to fully extend
                                app.MTV.Text = [num2str(app.ACTTimeExt) 'ms'];
                                app.indx = 1;
                                app.PlotTime = 0;
                            elseif(app.msg(1,1) == 4)
                                app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;       %current while contracting
                                app.PlotPotCW(app.indx) = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095;        %potentiometr value
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW,app.PlotTime,app.PlotPotCW);
    %                             hold(app.Plot,'on');
    %                             plot(app.Plot,app.PlotTime,app.PlotPotCW);
                                app.indx = app.indx + 1;

                            elseif(app.msg(1,1) == 5)
                                maxlen = max(length(app.PlotCurrCCW), length(app.PlotCurrCW));              %algorytm to print both Curr ext/contr and potentiometr value into one graph
                                app.PlotCurrCCW(end+1:maxlen) = 0;
                                app.PlotCurrCW(end+1:maxlen) = 0;
                                app.PlotTime(end+1:maxlen) = max(app.PlotTime);
                                app.PlotPotCCW(end+1:maxlen) = app.PlotPotCCW(end);
                                app.PlotPotCW(end+1:maxlen) = app.PlotPotCW(end);

                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                hold(app.Plot,'on');
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                plot(app.Plot,app.PlotTime,app.PlotPotCW);
                                plot(app.Plot,app.PlotTime,app.PlotPotCCW);
                                title(app.Plot, 'Current measuring');
                                xlabel(app.Plot, 'time [s]');
                                ylabel(app.Plot, 'current [A] range[%]');
    %
    %                             app.Plot
                                app.meanCurrCCW = mean(app.PlotCurrCCW(floor(length(app.PlotCurrCCW)/3):end));    %calculating mean currents                                     %calculating mean value of currents
                                app.LTV.Text = [num2str(app.meanCurrCCW,'%4.3f') 'A'];
                                app.meanCurrCW = mean(app.PlotCurrCW(floor(length(app.PlotCurrCW)/3):end));
                                app.LBV.Text = [num2str(app.meanCurrCW,'%4.3f') 'A'];

                                app.ACTTimeRet = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %time needed to retract
                                app.MBV.Text = [num2str(app.ACTTimeRet) 'ms'];

                                app.indx = 1;
                            elseif(app.msg(1,1) == 6)
                                app.minPot = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/4095;                     %minimal value of potentiometr
                                app.maxPot = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095;                     %maximal value of potentiometr

                                app.V2.Text = [num2str(app.maxPot*100, '%3.0f') '%'];
                                app.V4.Text = [num2str(app.minPot*100, '%3.0f') '%'];

                                if(app.REC.Value)                                                           %information writen to corresponding .txt file while recording
                                    fprintf(app.fileID, '\r\n%17s  %18s %20s %20s           %8s %8s %20s',...
                                app.LBV.Text, app.LTV.Text, app.MTV.Text, app.MBV.Text, app.V4.Text, app.V2.Text, datestr(now,'yymmdd_HHMMSS'));
                                end
                            elseif(app.msg(1,1) == 7)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                app.ErrTIME = uint8(app.msg(1,3));
                                if(app.ErrCUR && app.ErrTIME)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    app.ResN2.Visible = 'on';
                                    app.ResV2.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                    if(app.ErrTIME)
                                        app.ResV2.Text = 'OK';
                                    else
                                        app.ResV2.Text = 'NOT_OK';
                                    end
                                end
                            end

                        elseif (strcmp(app.Type.Value,'With_Hall_sensor'))
                            if(app.msg(1,1) == 1)

                            elseif(app.msg(1,1) == 2)
                                app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;     %current while extending
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                app.indx = app.indx + 1;
                            elseif(app.msg(1,1) == 3)
                                app.ACTTimeExt = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %time needed to fully extend
                                app.MTV.Text = [num2str(app.ACTTimeExt) 'ms'];
                            elseif(app.msg(1,1) == 4)
                                app.ECCW = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %number of ticks in extending motion
                                app.MTTV.Text = [num2str(app.ECCW) 'ticks'];
                                app.indx = 1;
                                app.PlotTime = 0;
                            elseif(app.msg(1,1) == 5)
                                app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;       %current while contracting
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                app.indx = app.indx + 1;
                            elseif(app.msg(1,1) == 6)
                                app.ACTTimeRet = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %time needed to retract
                                app.MBV.Text = [num2str(app.ACTTimeRet) 'ms'];
                            elseif(app.msg(1,1) == 7)
                                app.ECW = typecast(uint8(app.msg(1,2:5)), 'uint32');                    %number of ticks in retracting motion
                                app.MBBV.Text = [num2str(app.ECW) 'ticks'];
                                maxlen = max(length(app.PlotCurrCCW), length(app.PlotCurrCW));              %algorytm to print both Curr ext/contr and potentiometr value into one graph
                                app.PlotCurrCCW(end+1:maxlen) = 0;
                                app.PlotCurrCW(end+1:maxlen) = 0;
                                app.PlotTime(end+1:maxlen) = max(app.PlotTime);

                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                hold(app.Plot,'on');
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                title(app.Plot, 'Current measuring');
                                xlabel(app.Plot, 'time [s]');
                                ylabel(app.Plot, 'current [A]');

                                %app.Plot
                                floor(length(app.PlotCurrCCW)/3)
                                app.meanCurrCCW = mean(app.PlotCurrCCW(floor(length(app.PlotCurrCCW)/3):end-20));    %calculating mean currents                                     %calculating mean value of currents
                                app.LTV.Text = [num2str(app.meanCurrCCW,'%4.3f') 'A'];
                                app.meanCurrCW = mean(app.PlotCurrCW(floor(length(app.PlotCurrCW)/3):end-20));
                                app.LBV.Text = [num2str(app.meanCurrCW,'%4.3f') 'A'];
                            elseif(app.msg(1,1) == 8)
                                app.EAL = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase A LOW
                                app.V1.Text = [num2str(app.EAL,'%1.1f') 'V'];
                                app.EAH = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase A HIGH
                                app.V3.Text = [num2str(app.EAH,'%1.1f') 'V'];
                            elseif(app.msg(1,1) == 9)
                                app.EBL = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase B LOW
                                app.V2.Text = [num2str(app.EBL,'%1.1f') 'V'];
                                app.EBH = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase B HIGH
                                app.V4.Text = [num2str(app.EBH,'%1.1f') 'V'];

                                if(app.REC.Value)                                                           %information writen to corresponding .txt file while recording
                                    fprintf(app.fileID, '\r\n%18s %18s %20s %20s            %8s %8s            %8s %8s %20s',...
                                app.LBV.Text, app.LTV.Text, app.MTV.Text, app.MBV.Text, app.V1.Text, app.V3.Text, app.V2.Text, app.V4.Text, datestr(now,'yymmdd_HHMMSS'));
                                end
                            elseif(app.msg(1,1) == 10)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                app.ErrTIME = uint8(app.msg(1,3));
                                app.ErrENC = uint8(app.msg(1,4));
                                if(app.ErrCUR && app.ErrTIME && app.ErrENC)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    app.ResN2.Visible = 'on';
                                    app.ResV2.Visible = 'on';
                                    app.ResN3.Visible = 'on';
                                    app.ResV3.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                    if(app.ErrTIME)
                                        app.ResV2.Text = 'OK';
                                    else
                                        app.ResV2.Text = 'NOT_OK';
                                    end
                                    if(app.ErrENC)
                                        app.ResV3.Text = 'OK';
                                    else
                                        app.ResV3.Text = 'NOT_OK';
                                    end
                                end
                            end
                        end
                    elseif(strcmp(app.Motor.Value,'BLDC'))
                        if(app.msg(1,1) == 1)
                                app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;           % current in CCW direction
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                app.indx = app.indx + 1;
                        elseif(app.msg(1,1) == 2)
                                if (~strcmp(app.Type.Value,'Without_FG'))
                                    app.BLDCTimeCW = typecast(uint8(app.msg(1,2:5)), 'uint32');
                                    app.MBV.Text = [num2str(app.BLDCTimeCW) 'us'];
                                end

                                app.indx = 1;
                                app.PlotTime = 0;
                        elseif(app.msg(1,1) == 3)
                                app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;          % current in CW direction
                                app.PlotTime(app.indx) = app.timerStep * app.indx;
                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                app.indx = app.indx + 1;
                        elseif(app.msg(1,1) == 4)
                                if (~strcmp(app.Type.Value,'Without_FG'))
                                    app.BLDCTimeCCW = typecast(uint8(app.msg(1,2:5)), 'uint32');
                                    app.MTV.Text = [num2str(app.BLDCTimeCCW) 'us'];
                                end
                                maxlen = max(length(app.PlotCurrCCW), length(app.PlotCurrCW));                  % plotting
                                app.PlotCurrCCW(end+1:maxlen) = 0;
                                app.PlotCurrCW(end+1:maxlen) = 0;
                                app.PlotTime(end+1:maxlen) = max(app.PlotTime);

                                plot(app.Plot,app.PlotTime,app.PlotCurrCCW);
                                hold(app.Plot,'on');
                                plot(app.Plot,app.PlotTime,app.PlotCurrCW);
                                title(app.Plot, 'Current measuring');
                                xlabel(app.Plot, 'time [s]');
                                ylabel(app.Plot, 'current [A]');

                                app.meanCurrCCW = mean(app.PlotCurrCCW(floor(length(app.PlotCurrCCW)/2):end));    %calculating mean currents                                     %calculating mean value of currents
                                app.LTV.Text = [num2str(app.meanCurrCCW,'%4.3f') 'A'];
                                app.meanCurrCW = mean(app.PlotCurrCW(floor(length(app.PlotCurrCW)/2):end));
                                app.LBV.Text = [num2str(app.meanCurrCW,'%4.3f') 'A'];

                                app.indx = 1;
                                if (~strcmp(app.Type.Value,'Without_FG'))
                                    if(app.REC.Value)
                                        fprintf(app.fileID, '\r\n%15s %15s %15s %15s %20s',app.LTV.Text, app.LBV.Text, [app.V1.Text 'us'],  [app.V2.Text 'us'], datestr(now,'yymmdd_HHMMSS'));               %information writen to corresponding .txt file while recording
                                    end
                                else
                                    if(app.REC.Value)
                                        fprintf(app.fileID, '\r\n%15s %15s %20s',app.LTV.Text, app.LBV.Text, datestr(now,'yymmdd_HHMMSS'));               %information writen to corresponding .txt file while recording
                                    end
                                end
                        elseif(app.msg(1,1) == 6)

                        elseif(app.msg(1,1) == 7)
                            app.EAL = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase A LOW
                            app.V1.Text = [num2str(app.EAL,'%1.1f') 'V'];
                            app.EAH = cast(typecast(uint8(app.msg(1,4:5)), 'uint16'),'double')/4095 * 5;                        %voltage lvl of signal from encoder phase A HIGH
                            app.V3.Text = [num2str(app.EAH,'%1.1f') 'V'];

                        end

                        if (strcmp(app.Type.Value,'Without_FG'))
                            if(app.msg(1,1) == 5)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                if(app.ErrCUR)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                end
                            end
                        else
                            if(app.msg(1,1) == 5)
                                app.ErrCUR = uint8(app.msg(1,2));                        %error results
                                app.ErrFG = uint8(app.msg(1,3));
                                if(app.ErrCUR && app.ErrFG)
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'Motor OK';
                                else
                                    app.Error.Visible = 'on';
                                    app.Error.Text = 'ERROR';
                                    app.ResN1.Visible = 'on';
                                    app.ResV1.Visible = 'on';
                                    app.ResN4.Visible = 'on';
                                    app.ResV4.Visible = 'on';
                                    if(app.ErrCUR)
                                        app.ResV1.Text = 'OK';
                                    else
                                        app.ResV1.Text = 'NOT_OK';
                                    end
                                    if(app.ErrFG)
                                        app.ResV4.Text = 'OK';
                                    else
                                        app.ResV4.Text = 'NOT_OK';
                                    end
                                end
                            end
                        end
                    end
                else                                                                                        % manual mode
                    if(strcmp(app.Direction.Value,'CW'))
                        % plotting current while direction is set as CW
                        app.PlotCurrCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;
                        app.PlotCurrCCW(app.indx) = 0;
                    else
                        app.PlotCurrCCW(app.indx) = cast(typecast(uint8(app.msg(1,2:3)), 'uint16'),'double')/16384*app.ratio;              % plotting current while direction is set as CCW
                        app.PlotCurrCW(app.indx) = 0;
                    end

                    app.PlotTime(app.indx) = app.timerStep * app.indx;
                    plot(app.Plot,app.PlotTime,app.PlotCurrCCW,app.PlotTime,app.PlotCurrCW);
                    app.indx = app.indx + 1;
                    title(app.Plot, 'Current measuring');
                    xlabel(app.Plot, 'time [s]');
                    ylabel(app.Plot, 'current [A]');

                end

                if(app.msg(1,:) == 0)
                    app.Start.Value = false;
                    StartValueChanged(app);
                end
            end

    %         function UIFigClosing(app)
    % %             if(app.Connect.Value)
    % %                 STAND.Send(app.com,'Y');
    % %                 [~,isOK]=STAND.close(app.com);
    % %             end
    %
    % %             fclose(app.fileID);
    % %             delete(app.Timer);
    % %             % Delete UIFigure when app is deleted
    % %             delete(app.RECFigure);
    % %             delete(app.UIFigure)
    %             delete();
    %         end

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            function RECFigClosing(app)
                app.RECFigure.Visible = 'off';
                app.REC.Value = 0;
            end

             % FigFile value changed function
            function FigFileValueChanged(app)
                value = app.FigFile.Value;
                app.EditField.Value = value;

            end

            % FigMotor value changed function
            function FigMotorValueChanged(app)
                value = app.FigMotor.Value;

                if (strcmp(value,'DC'))
                    app.FigType.Items = {'With_encoder', 'Without_encoder'};
                    app.FigType.Value = 'With_encoder';

                end
                if (strcmp(value,'ACTUATOR'))
                    app.FigType.Items = {'With_potentiometr', 'With_Hall_sensor', 'Without_encoder'};
                    app.FigType.Value = 'With_potentiometr';

                end
                if (strcmp(value,'BLDC'))
                    app.FigType.Items = {'Without_FG', 'With_FG'};
                    app.FigType.Value = 'With_FG';
                end

                app.EditField.Value = [app.FigMotor.Value,'-',app.FigVoltage.Value,'-',app.FigType.Value,datestr(now,'-yymmdd_HHMMSS'),'.txt'];

            end

            % FigVoltage value changed function
            function FigVoltageValueChanged(app)
                app.EditField.Value = [app.FigMotor.Value,'-',app.FigVoltage.Value,'-',app.FigType.Value,datestr(now,'-yymmdd_HHMMSS'),'.txt'];
            end

            % FigType value changed function
            function FigTypeValueChanged(app)
                app.EditField.Value = [app.FigMotor.Value,'-',app.FigVoltage.Value,'-',app.FigType.Value,datestr(now,'-yymmdd_HHMMSS'),'.txt'];
            end

            % FigNew button pushed function
            function FigNewButtonPushed(app)
                createFigNew(app);
                app.new = 1;
            end

            % FigContinue button pushed function
            function FigContinueButtonPushed(app)
                createFigContinue(app);
                app.new = 0;
            end

            % FigSTART button pushed function
            function FigSTARTButtonPushed(app)
                app.Motor.Value = app.FigMotor.Value;
                app.Voltage.Value = app.FigVoltage.Value;
                MotorValueChanged(app);
                app.Type.Value = app.FigType.Value;

                %exist(['RECdata\',app.EditField.Value], 'file') &&

                if(app.new)
                    app.fileID = fopen(['RECdata\',app.EditField.Value], 'w');          %creats new document
                    if(strcmp(app.Motor.Value,'DC'))
                        if(strcmp(app.Type.Value,'With_encoder'))
                            fprintf(app.fileID, '%15s %15s %20s %20s  Encoder voltage level channel A: %10s %10s  Encoder voltage level channel B: %10s %10s %20s', ...
                                'Current CCW', 'Current CW', 'Encoder ticks CCW', 'Encoder ticks CW', 'LOW', 'HIGH', 'LOW', 'HIGH', 'YYMMDD_hhmmss');
                        elseif(strcmp(app.Type.Value,'Without_encoder'))
                            fprintf(app.fileID, '%15s %15s %20s','Current CCW', 'Current CW', 'YYMMDD_hhmmss');
                        end
                    elseif(strcmp(app.Motor.Value,'ACTUATOR'))
                        if(strcmp(app.Type.Value,'With_potentiometr'))
                            fprintf(app.fileID, '%17s %18s %20s %20s Potentiometr:  MIN      MAX %20s',...
                                'Current extending', 'Current contracting', 'Time extending', 'Time retracting', 'YYMMDD_hhmmss');
                        elseif(strcmp(app.Type.Value,'With_Hall_sensor'))
                            fprintf(app.fileID, '%15s %15s %20s %20s  Channel A:%8s %8s  Channel B:%8s %8s %20s',...
                                'Current extending', 'Current contracting', 'Time extending', 'Time retracting', 'LOW', 'HIGH', 'LOW', 'HIGH', 'YYMMDD_hhmmss');
                        elseif(strcmp(app.Type.Value,'Without_encoder'))
                            fprintf(app.fileID, '%15s %15s %20s %20s %20s', 'Current extending', 'Current retracting', 'Time extending', 'Time contracting', 'YYMMDD_hhmmss');
                        end
                    elseif(strcmp(app.Motor.Value,'BLDC'))

                        if (~strcmp(app.Type.Value,'Without_FG'))
                            if(app.REC.Value)
                                fprintf(app.fileID, '%15s %15s %15s %15s %20s','Current CCW', 'Current CW', 'FG CCW', 'FG CW', 'YYMMDD_hhmmss');               %information writen to corresponding .txt file while recording
                            end
                        else
                            if(app.REC.Value)
                                fprintf(app.fileID, '%15s %15s %20s','Current CCW', 'Current CW', 'YYMMDD_hhmmss');               %information writen to corresponding .txt file while recording
                            end
                        end
    %                     if(strcmp(app.Type.Value,'3_wire'))
    %                         fprintf(app.fileID, 'Top line.');
    %                     elseif(strcmp(app.Type.Value,'5_wire'))
    %                         fprintf(app.fileID, 'Top line.');
    %                     elseif(strcmp(app.Type.Value,'6_wire'))
    %                         fprintf(app.fileID, 'Top line.');
    %                     end
                    end
                else
                    app.fileID = fopen(['RECdata\',app.EditField.Value], 'a');              %opens document and (append)set cursor to the end
                end
    %             app.EditField.Value = [app.FigMotor.Value,'-',app.FigVoltage.Value,'-',app.FigType.Value,datestr(now,'-yymmdd_HHMMSS'),'.txt'];

                if(~app.new)
                    i = 1;
                    while(app.EditField.Value(i) ~= '-')
                        i = i + 1;
                    end
                    motor = app.EditField.Value(1:(i-1));
                    ii = i+1;
                    while(app.EditField.Value(ii) ~= '-')
                        ii = ii + 1;
                    end
                    voltage = app.EditField.Value((i+1):(ii-1));
                    iii = ii+1;
                    while(~xor(app.EditField.Value(iii) ~= '-', app.EditField.Value(iii) ~= '.')) %    (app.EditField.Value(iii) ~= '-')) || (app.EditField.Value(iii) ~= '.'))
                        iii = iii + 1;
                    end
                    type = app.EditField.Value((ii+1):(iii-1));

                    app.Motor.Value = motor;
                    app.Voltage.Value = voltage;
                    MotorValueChanged(app);
                    app.Type.Value = type;
                end

                app.REC.BackgroundColor = [1 0 0];
                app.RECFigure.Visible = 'off';
                app.Set;
            end

            % FigCancel button pushed function
            function FigCancelButtonPushed(app)
                RECFigClosing(app);
                app.REC.Value = 0;
            end

            function createFigNew(app)
    %             app.FigLabelListBox4.Visible = 'off';
    %             app.FigFile.Visible = 'off';
    %
    %             app.RECFigure.Position = [100 100 244 338];
    %
    %             % Create FigSTART
    % %             app.FigSTART = uibutton(app.RECFigure, 'push');
    %             app.FigSTART.ButtonPushedFcn = createCallbackFcn(app, @FigSTARTButtonPushed);
    %             app.FigSTART.Position = [132 58 100 22];
    %             app.FigSTART.Text = 'START';
    %
    %             % Create FigCancel
    % %             app.FigCancel = uibutton(app.RECFigure, 'push');
    %             app.FigCancel.ButtonPushedFcn = createCallbackFcn(app, @FigCancelButtonPushed);
    %             app.FigCancel.Position = [132 20 100 22];
    %             app.FigCancel.Text = 'Cancel';
    %
    %             % Create FigNew
    % %             app.FigNew = uibutton(app.RECFigure, 'push');
    %             app.FigNew.ButtonPushedFcn = createCallbackFcn(app, @FigNewButtonPushed);
    %             app.FigNew.Position = [132 284 100 22];
    %             app.FigNew.Text = 'NEW';
    %
    %             % Create FigContinue
    % %             app.FigContinue = uibutton(app.RECFigure, 'push');
    %             app.FigContinue.ButtonPushedFcn = createCallbackFcn(app, @FigContinueButtonPushed);
    %             app.FigContinue.Position = [132 248 100 22];
    %             app.FigContinue.Text = 'Continue';
    %
    %             % Create LabelListBox
    % %             app.FigLabelListBox = uilabel(app.RECFigure);
    %             app.FigLabelListBox.HorizontalAlignment = 'center';
    %             app.FigLabelListBox.VerticalAlignment = 'center';
    %             app.FigLabelListBox.FontName = 'Agency FB';
    %             app.FigLabelListBox.FontSize = 20;
    %             app.FigLabelListBox.FontWeight = 'bold';
    %             app.FigLabelListBox.Position = [41 301 41 26];
    %             app.FigLabelListBox.Text = 'Motor';
    %
    %             % Create FigMotor
    % %             app.FigMotor = uilistbox(app.RECFigure);
    %             app.FigMotor.ValueChangedFcn = createCallbackFcn(app, @FigMotorValueChanged);
    %             app.FigMotor.Position = [11 230 100 74];
    %
    %             % Create Label
    % %             app.FigLabel = uilabel(app.RECFigure);
    %             app.FigLabel.HorizontalAlignment = 'center';
    %             app.FigLabel.VerticalAlignment = 'center';
    %             app.FigLabel.FontName = 'Agency FB';
    %             app.FigLabel.FontSize = 20;
    %             app.FigLabel.FontWeight = 'bold';
    %             app.FigLabel.Position = [44.5 90 32 26];
    %             app.FigLabel.Text = 'Type';
    %
    %             % Create FigType
    % %             app.FigType = uilistbox(app.RECFigure);
    %             app.FigType.ValueChangedFcn = createCallbackFcn(app, @FigTypeValueChanged);
    %             app.FigType.Position = [11 18 100 74];
    %
    %             % Create Label2
    % %             app.FigLabel2 = uilabel(app.RECFigure);
    %             app.FigLabel2.HorizontalAlignment = 'center';
    %             app.FigLabel2.VerticalAlignment = 'center';
    %             app.FigLabel2.FontName = 'Agency FB';
    %             app.FigLabel2.FontSize = 20;
    %             app.FigLabel2.FontWeight = 'bold';
    %             app.FigLabel2.Position = [35.5 194 51 26];
    %             app.FigLabel2.Text = 'Voltage';
    %
    %             % Create FigVoltage
    % %             app.FigVoltage = uilistbox(app.RECFigure);
    %             app.FigVoltage.ValueChangedFcn = createCallbackFcn(app, @FigVoltageValueChanged);
    %             app.FigVoltage.Position = [11 122 100 74];
                app.RECFigure.Position = [200 200 582 341];

                % Create FigSTART
    %             app.FigSTART = uibutton(app.RECFigure, 'push');
                app.FigSTART.ButtonPushedFcn = createCallbackFcn(app, @FigSTARTButtonPushed);
                app.FigSTART.Position = [468 159 100 22];
                app.FigSTART.Text = 'START';

                % Create FigCancel
    %             app.FigCancel = uibutton(app.RECFigure, 'push');
                app.FigCancel.ButtonPushedFcn = createCallbackFcn(app, @FigCancelButtonPushed);
                app.FigCancel.Position = [468 121 100 22];
                app.FigCancel.Text = 'Cancel';

                % Create LabelListBox
    %             app.FigLabelListBox = uilabel(app.RECFigure);
                app.FigLabelListBox.HorizontalAlignment = 'center';
                app.FigLabelListBox.VerticalAlignment = 'center';
                app.FigLabelListBox.FontName = 'Agency FB';
                app.FigLabelListBox.FontSize = 20;
                app.FigLabelListBox.FontWeight = 'bold';
                app.FigLabelListBox.Position = [44 300 41 26];
                app.FigLabelListBox.Text = 'Motor';

                % Create FigMotor
    %             app.FigMotor = uilistbox(app.RECFigure);
                app.FigMotor.ValueChangedFcn = createCallbackFcn(app, @FigMotorValueChanged);
                app.FigMotor.Enable = 'on';
                app.FigMotor.Position = [14 229 100 74];

                % Create Label
    %             app.FigLabel = uilabel(app.RECFigure);
                app.FigLabel.HorizontalAlignment = 'center';
                app.FigLabel.VerticalAlignment = 'center';
                app.FigLabel.FontName = 'Agency FB';
                app.FigLabel.FontSize = 20;
                app.FigLabel.FontWeight = 'bold';
                app.FigLabel.Position = [47.5 89 32 26];
                app.FigLabel.Text = 'Type';

                % Create FigType
    %             app.FigType = uilistbox(app.RECFigure);
                app.FigType.ValueChangedFcn = createCallbackFcn(app, @FigTypeValueChanged);
                app.FigType.Position = [14 17 100 74];
                app.FigType.Enable = 'on';

                % Create FigNew
    %             app.FigNew = uibutton(app.RECFigure, 'push');
                app.FigNew.ButtonPushedFcn = createCallbackFcn(app, @FigNewButtonPushed);
                app.FigNew.Position = [468 281 100 22];
                app.FigNew.BackgroundColor = [0.8 0.8 0.8];
                app.FigNew.Text = 'NEW';

                % Create FigContinue
    %             app.FigContinue = uibutton(app.RECFigure, 'push');
                app.FigContinue.ButtonPushedFcn = createCallbackFcn(app, @FigContinueButtonPushed);
                app.FigContinue.Position = [468 245 100 22];
                app.FigContinue.BackgroundColor = [1 1 1];
                app.FigContinue.Text = 'Continue';

                % Create Label2
    %             app.FigLabel2 = uilabel(app.RECFigure);
                app.FigLabel2.HorizontalAlignment = 'center';
                app.FigLabel2.VerticalAlignment = 'center';
                app.FigLabel2.FontName = 'Agency FB';
                app.FigLabel2.FontSize = 20;
                app.FigLabel2.FontWeight = 'bold';
                app.FigLabel2.Position = [38.5 193 51 26];
                app.FigLabel2.Text = 'Voltage';

                % Create FigVoltage
    %             app.FigVoltage = uilistbox(app.RECFigure);
                app.FigVoltage.ValueChangedFcn = createCallbackFcn(app, @FigVoltageValueChanged);
                app.FigVoltage.Enable = 'on';
                app.FigVoltage.Position = [14 121 100 74];

                % Create LabelListBox4
    %             app.FigLabelListBox4 = uilabel(app.RECFigure);
                app.FigLabelListBox4.Visible = 'on';
                app.FigLabelListBox4.HorizontalAlignment = 'center';
                app.FigLabelListBox4.VerticalAlignment = 'center';
                app.FigLabelListBox4.FontName = 'Agency FB';
                app.FigLabelListBox4.FontSize = 20;
                app.FigLabelListBox4.FontWeight = 'bold';
                app.FigLabelListBox4.Position = [267 301 44 26];
                app.FigLabelListBox4.Text = 'File';

                % Create FigFile
    %             app.FigFile = uilistbox(app.RECFigure);
                app.FigFile.Visible = 'on';
                app.FigFile.ValueChangedFcn = createCallbackFcn(app, @FigFileValueChanged);
                app.FigFile.Position = [130 123 318 180];
                app.FigFile.Enable = 'off';

                MyFolderInfo = dir('RECdata');
                app.FigFile.Items = {};
                if(~strcmp(MyFolderInfo(end).name,'..'))
                    i = 3;
                    while(~strcmp(MyFolderInfo(i).name,MyFolderInfo(end).name))
                        app.FigFile.Items = [app.FigFile.Items, MyFolderInfo(i).name];
                        i = i + 1;
                    end
                        app.FigFile.Items = [app.FigFile.Items, MyFolderInfo(i).name];
                else

                end

                % Create LabelEditField
    %             app.LabelEditField = uilabel(app.UIFigure);
                app.LabelEditField.Visible = 'on';
                app.LabelEditField.HorizontalAlignment = 'right';
                app.LabelEditField.Position = [130 95 56 15];
                app.LabelEditField.Text = 'File Name';

                % Create EditField
    %             app.EditField = uieditfield(app.UIFigure, 'text');
                app.EditField.Visible = 'on';
                app.EditField.Position = [130 67 438 22];

                app.EditField.Value = [app.FigMotor.Value,'-',app.FigVoltage.Value,'-',app.FigType.Value,datestr(now,'-yymmdd_HHMMSS'),'.txt'];
            end

            function createFigContinue(app)
                app.RECFigure.Position = [200 200 582 341];

                app.LabelEditField.Visible = 'off';
                app.EditField.Visible = 'off';

                % Create FigSTART
    %             app.FigSTART = uibutton(app.RECFigure, 'push');
                app.FigSTART.ButtonPushedFcn = createCallbackFcn(app, @FigSTARTButtonPushed);
                app.FigSTART.Position = [468 159 100 22];
                app.FigSTART.Text = 'START';

                % Create FigCancel
    %             app.FigCancel = uibutton(app.RECFigure, 'push');
                app.FigCancel.ButtonPushedFcn = createCallbackFcn(app, @FigCancelButtonPushed);
                app.FigCancel.Position = [468 121 100 22];
                app.FigCancel.Text = 'Cancel';

                % Create LabelListBox
    %             app.FigLabelListBox = uilabel(app.RECFigure);
                app.FigLabelListBox.HorizontalAlignment = 'center';
                app.FigLabelListBox.VerticalAlignment = 'center';
                app.FigLabelListBox.FontName = 'Agency FB';
                app.FigLabelListBox.FontSize = 20;
                app.FigLabelListBox.FontWeight = 'bold';
                app.FigLabelListBox.Position = [44 300 41 26];
                app.FigLabelListBox.Text = 'Motor';

                % Create FigMotor
    %             app.FigMotor = uilistbox(app.RECFigure);
                app.FigMotor.ValueChangedFcn = createCallbackFcn(app, @FigMotorValueChanged);
                app.FigMotor.Position = [14 229 100 74];
                app.FigMotor.Enable = 'off';

                % Create Label
    %             app.FigLabel = uilabel(app.RECFigure);
                app.FigLabel.HorizontalAlignment = 'center';
                app.FigLabel.VerticalAlignment = 'center';
                app.FigLabel.FontName = 'Agency FB';
                app.FigLabel.FontSize = 20;
                app.FigLabel.FontWeight = 'bold';
                app.FigLabel.Position = [47.5 89 32 26];
                app.FigLabel.Text = 'Type';

                % Create FigType
    %             app.FigType = uilistbox(app.RECFigure);
                app.FigType.ValueChangedFcn = createCallbackFcn(app, @FigTypeValueChanged);
                app.FigType.Position = [14 17 100 74];
                app.FigType.Enable = 'off';

                % Create FigNew
    %             app.FigNew = uibutton(app.RECFigure, 'push');
                app.FigNew.ButtonPushedFcn = createCallbackFcn(app, @FigNewButtonPushed);
                app.FigNew.Position = [468 281 100 22];
                app.FigNew.BackgroundColor = [1 1 1];
                app.FigNew.Text = 'NEW';

                % Create FigContinue
    %             app.FigContinue = uibutton(app.RECFigure, 'push');
                app.FigContinue.ButtonPushedFcn = createCallbackFcn(app, @FigContinueButtonPushed);
                app.FigContinue.Position = [468 245 100 22];
                app.FigContinue.BackgroundColor = [0.8 0.8 0.8];
                app.FigContinue.Text = 'Continue';

                % Create Label2
    %             app.FigLabel2 = uilabel(app.RECFigure);
                app.FigLabel2.HorizontalAlignment = 'center';
                app.FigLabel2.VerticalAlignment = 'center';
                app.FigLabel2.FontName = 'Agency FB';
                app.FigLabel2.FontSize = 20;
                app.FigLabel2.FontWeight = 'bold';
                app.FigLabel2.Position = [38.5 193 51 26];
                app.FigLabel2.Text = 'Voltage';

                % Create FigVoltage
    %             app.FigVoltage = uilistbox(app.RECFigure);
                app.FigVoltage.ValueChangedFcn = createCallbackFcn(app, @FigVoltageValueChanged);
                app.FigVoltage.Position = [14 121 100 74];
                app.FigVoltage.Enable = 'off';

                % Create LabelListBox4
    %             app.FigLabelListBox4 = uilabel(app.RECFigure);
                app.FigLabelListBox4.Visible = 'on';
                app.FigLabelListBox4.HorizontalAlignment = 'center';
                app.FigLabelListBox4.VerticalAlignment = 'center';
                app.FigLabelListBox4.FontName = 'Agency FB';
                app.FigLabelListBox4.FontSize = 20;
                app.FigLabelListBox4.FontWeight = 'bold';
                app.FigLabelListBox4.Position = [267 301 44 26];
                app.FigLabelListBox4.Text = 'File';

                % Create FigFile
    %             app.FigFile = uilistbox(app.RECFigure);
                app.FigFile.Visible = 'on';
                app.FigFile.ValueChangedFcn = createCallbackFcn(app, @FigFileValueChanged);
                app.FigFile.Position = [130 17 318 286];
                app.FigFile.Enable = 'on';

                MyFolderInfo = dir('RECdata');
                app.FigFile.Items = {};

                i = 3;
                while(~strcmp(MyFolderInfo(i).name,MyFolderInfo(end).name))
                    app.FigFile.Items = [app.FigFile.Items, MyFolderInfo(i).name];
                    i = i + 1;
                end
                    app.FigFile.Items = [app.FigFile.Items, MyFolderInfo(i).name];

            end

        end

        % App initialization and construction
        methods (Access = private)

            % Create UIFigure and components
            % Create UIFigure and components
            function createComponents(app)

                % Create UIFigure
                app.UIFigure = uifigure;
                app.UIFigure.Position = [100 100 673 535]; %497
                app.UIFigure.Name = 'STAND';
    %             app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigClosing);
                setAutoResize(app, app.UIFigure, true)

                % Create UIFigure
                app.RECFigure = uifigure;
                app.RECFigure.CloseRequestFcn = createCallbackFcn(app, @RECFigClosing);
                app.RECFigure.Visible = 'off';
                app.RECFigure.Position = [100 100 244 338];
                app.RECFigure.Name = 'RECording setting';
                setAutoResize(app, app.RECFigure, true)

                % Create Plot
                app.Plot = uiaxes(app.UIFigure);
                title(app.Plot, 'Current measuring');
                xlabel(app.Plot, 'time [s]');
                ylabel(app.Plot, 'current [A]');
                app.Plot.Box = 'on';
                app.Plot.XGrid = 'on';
                app.Plot.YGrid = 'on';
                %app.Plot.Position = [9 163 445 320];
                app.Plot.Position = [10 250 445 275];
                app.Plot.XLim = [0 2];
                app.Error = uilabel(app.UIFigure);
                app.Error.HorizontalAlignment = 'center';
                app.ResN1 = uilabel(app.UIFigure);
                app.ResN1.HorizontalAlignment = 'center';
                app.ResV1 = uilabel(app.UIFigure);
                app.ResV1.HorizontalAlignment = 'center';
                app.ResN2 = uilabel(app.UIFigure);
                app.ResN2.HorizontalAlignment = 'center';
                app.ResV2 = uilabel(app.UIFigure);
                app.ResV2.HorizontalAlignment = 'center';
                app.ResN3 = uilabel(app.UIFigure);
                app.ResN3.HorizontalAlignment = 'center';
                app.ResV3 = uilabel(app.UIFigure);
                app.ResV3.HorizontalAlignment = 'center';
                app.ResN4 = uilabel(app.UIFigure);
                app.ResN4.HorizontalAlignment = 'center';
                app.ResV4 = uilabel(app.UIFigure);
                app.ResV4.HorizontalAlignment = 'center';
                app.Error.Position = [14 233 70 15];
                app.Error.Text = 'ERROR';
                app.ResN1.Position = [80 233 31 15];
                app.ResN1.Text = 'Curr:';
                app.ResV1.Position = [100 233 70 15];
                app.ResV1.Text = 'NOT_OK';
                app.ResN2.Position = [180 233 31 15];
                app.ResN2.Text = 'Time:';
                app.ResV2.Position = [200 233 70 15];
                app.ResV2.Text = 'NOT_OK';
                app.ResN3.Position = [80 214 31 15];
                app.ResN3.Text = 'ENC:';
                app.ResV3.Position = [100 214 70 15];
                app.ResV3.Text = 'NOT_OK';
                app.ResN4.Position = [180 214 31 15];
                app.ResN4.Text = '  FG:';
                app.ResV4.Position = [200 214 70 15];
                app.ResV4.Text = 'NOT_OK';
                app.Error.Visible = 'off';
                app.ResN1.Visible = 'off';
                app.ResV1.Visible = 'off';
                app.ResN2.Visible = 'off';
                app.ResV2.Visible = 'off';
                app.ResN3.Visible = 'off';
                app.ResV3.Visible = 'off';
                app.ResN4.Visible = 'off';
                app.ResV4.Visible = 'off';

                % Create LabelDropDown
                app.LabelDropDown = uilabel(app.UIFigure);
                app.LabelDropDown.HorizontalAlignment = 'center';
                app.LabelDropDown.VerticalAlignment = 'center';
                app.LabelDropDown.FontName = 'Agency FB';
                app.LabelDropDown.FontSize = 20;
                app.LabelDropDown.FontWeight = 'bold';
                app.LabelDropDown.Position = [462 330 77 26];
                app.LabelDropDown.Text = 'Motor Type';

                % Create Type
                app.Type = uidropdown(app.UIFigure);
                app.Type.Items = {'With_encoder', 'Without_encoder'};
                app.Type.Value = 'With_encoder';
                app.Type.ValueChangedFcn = createCallbackFcn(app, @TypeValueChanged, true);
                app.Type.Position = [554 314 100 59];

                % Create LabelDiscreteKnob
                app.LabelDiscreteKnob = uilabel(app.UIFigure);
                app.LabelDiscreteKnob.HorizontalAlignment = 'center';
                app.LabelDiscreteKnob.Position = [549.5 390 31 15];
                app.LabelDiscreteKnob.Text = 'Motor';

                % Create Motor
                app.Motor = uiknob(app.UIFigure, 'discrete');
                app.Motor.Items = {'BLDC', 'ACTUATOR', 'DC'};
                app.Motor.ValueChangedFcn = createCallbackFcn(app, @MotorValueChanged);
                app.Motor.FontName = 'Agency FB';
                app.Motor.FontSize = 20;
                app.Motor.FontWeight = 'bold';
                app.Motor.Position = [535 418 60 60];
                app.Motor.Value = 'DC';

                % Create Label
                app.Label = uilabel(app.UIFigure);
                app.Label.HorizontalAlignment = 'center';
                app.Label.Position = [540 173 42 15];
                app.Label.Text = 'Voltage';

                % Create Voltage
                app.Voltage = uiknob(app.UIFigure, 'discrete');
                app.Voltage.Items = {'36V', '24V', '12V', '6V'};
                app.Voltage.ValueChangedFcn = createCallbackFcn(app, @VoltageValueChanged);
                app.Voltage.FontName = 'Agency FB';
                app.Voltage.FontSize = 20;
                app.Voltage.FontWeight = 'bold';
                app.Voltage.Position = [531 203 60 60];
                app.Voltage.Value = '6V';

                % Create LabelSwitch
                app.LabelSwitch = uilabel(app.UIFigure);
                app.LabelSwitch.HorizontalAlignment = 'center';
                app.LabelSwitch.Position = [96.5 18 52 42];
                app.LabelSwitch.Text = {'Direction '; 'of '; 'rotation'};

                % Create Direction
                app.Direction = uiswitch(app.UIFigure, 'slider');
                app.Direction.Items = {'CW', 'CCW'};
                app.Direction.Orientation = 'vertical';
                app.Direction.ValueChangedFcn = createCallbackFcn(app, @DirectionValueChanged);
                app.Direction.FontName = 'Agency FB';
                app.Direction.FontSize = 20;
                app.Direction.FontWeight = 'bold';
                app.Direction.Position = [113 92 20 45];
                app.Direction.Value = 'CW';

                % Create LabelSwitch2
                app.LabelSwitch2 = uilabel(app.UIFigure);
                app.LabelSwitch2.HorizontalAlignment = 'center';
                app.LabelSwitch2.VerticalAlignment = 'center';
                app.LabelSwitch2.Position = [16.5 17 52 42];
                app.LabelSwitch2.Text = {'Mode '; 'of '; 'operation'};

                % Create Mode
                app.Mode = uiswitch(app.UIFigure, 'slider');
                app.Mode.Items = {'Automatic', 'Manual'};
                app.Mode.Orientation = 'vertical';
                app.Mode.ValueChangedFcn = createCallbackFcn(app, @ModeValueChanged);
                app.Mode.FontName = 'Agency FB';
                app.Mode.FontSize = 20;
                app.Mode.FontWeight = 'bold';
                app.Mode.Position = [33 93 20 45];
                app.Mode.Value = 'Automatic';

                % Create LabelKnob
                app.LabelKnob = uilabel(app.UIFigure);
                app.LabelKnob.HorizontalAlignment = 'center';
                app.LabelKnob.Position = [329 22 125 15];
                app.LabelKnob.Text = 'Speed [50%]';

                % Create Speed
                app.Speed = uiknob(app.UIFigure, 'continuous');
                app.Speed.ValueChangedFcn = createCallbackFcn(app, @SpeedValueChanged);
                app.Speed.ValueChangingFcn = createCallbackFcn(app, @SpeedValueChanging, true);
                app.Speed.FontName = 'Agency FB';
                app.Speed.FontSize = 20;
                app.Speed.FontWeight = 'bold';
                app.Speed.Position = [362 71 60 60];
                app.Speed.Value = 50;

                % Create Start
                app.Start = uibutton(app.UIFigure, 'state');
                app.Start.ValueChangedFcn = createCallbackFcn(app, @StartValueChanged);
                app.Start.Text = 'START';
                app.Start.FontName = 'Agency FB';
                app.Start.FontSize = 20;
                app.Start.FontWeight = 'bold';
                app.Start.Position = [476.5 104 111 38];

                % Create LabelLamp
                app.LabelLamp = uilabel(app.UIFigure);
                app.LabelLamp.HorizontalAlignment = 'right';
                app.LabelLamp.Position = [608 127 47 15];
                app.LabelLamp.Text = 'Running';

                % Create LED
                app.LED = uilamp(app.UIFigure);
                app.LED.Position = [622 105 20 20];
                app.LED.Color = [1 0 0];

                % Create Connect
                app.Connect = uibutton(app.UIFigure, 'state');
                app.Connect.ValueChangedFcn = createCallbackFcn(app, @ConnectValueChanged);
                app.Connect.Text = 'CONNECT';
                app.Connect.FontName = 'Agency FB';
                app.Connect.FontSize = 20;
                app.Connect.FontWeight = 'bold';
                app.Connect.Position = [476.5 51 111 38];

                % Create Label3
                app.Label3 = uilabel(app.UIFigure);
                app.Label3.HorizontalAlignment = 'center';
                app.Label3.VerticalAlignment = 'center';
                app.Label3.Position = [590 74 83 15];
                app.Label3.Text = 'Connected';

                % Create LED2
                app.LED2 = uilamp(app.UIFigure);
                app.LED2.Position = [622 52 20 20];
                app.LED2.Color = [1 0 0];

                % Create Label4
                app.Label4 = uilabel(app.UIFigure);
                app.Label4.Position = [179.5 22 102 15];
                app.Label4.HorizontalAlignment = 'center';
                app.Label4.Text = 'Time [5s]';

                % Create Time
                app.Time = uiknob(app.UIFigure, 'continuous');
                app.Time.MajorTicks = [2 10 20 30 40 50 60 70 80 90 99];
                app.Time.ValueChangedFcn = createCallbackFcn(app, @TimeValueChanged);
                app.Time.ValueChangingFcn = createCallbackFcn(app, @TimeValueChanging, true);
                app.Time.FontName = 'Agency FB';
                app.Time.FontSize = 20;
                app.Time.FontWeight = 'bold';
                app.Time.Position = [202 71 60 60];
                app.Time.Value = 5;
                app.Plot.XLim = [0 app.Time.Value];
                app.PlotTime = (0:0.2:app.Time.Value);
                app.PlotLength = length(app.PlotTime);
                app.PlotCurrCCW = zeros(1,app.PlotLength);
                app.PlotCurrCW = zeros(1,app.PlotLength);

                % Create Label5
                app.LTN = uilabel(app.UIFigure);
                app.LTN.Position = [14 195 76 15];
                app.LTN.Text = 'Current CCW:';

                % Create Label6
                app.LBN = uilabel(app.UIFigure);
                app.LBN.Position = [14 176 76 15];
                app.LBN.Text = 'Current CW:';

                % Create Label7
                app.LTV = uilabel(app.UIFigure);
                app.LTV.Position = [104 195 60 15];
                app.LTV.Text = 'Value1';

                % Create Label8
                app.LBV = uilabel(app.UIFigure);
                app.LBV.Position = [104 176 60 15];
                app.LBV.Text = 'Value';

                % Create Label9
                app.RTN = uilabel(app.UIFigure);
                app.RTN.Position = [330 195 61 15];
                app.RTN.Text = 'Channel A:';

                % Create Label10
                app.RBN = uilabel(app.UIFigure);
                app.RBN.Position = [330 176 81 15];
                app.RBN.Text = 'Channel B:';

                % Create Label11
                app.LOW = uilabel(app.UIFigure);
                app.LOW.Position = [413 214 35 15];
                app.LOW.Text = 'Low';

                % Create Label12
                app.HIGH = uilabel(app.UIFigure);
                app.HIGH.Position = [449 214 41 15];
                app.HIGH.Text = 'High';

                % Create Label13
                app.V1 = uilabel(app.UIFigure);
                app.V1.Position = [410 195 40 15];
                app.V1.Text = '1';

                % Create Label14
                app.V2 = uilabel(app.UIFigure);
                app.V2.Position = [410 176 40 15];
                app.V2.Text = '2';

                % Create Label15
                app.V4 = uilabel(app.UIFigure);
                app.V4.Position = [450 176 40 15];
                app.V4.Text = '4';

                % Create Label16
                app.V3 = uilabel(app.UIFigure);
                app.V3.Position = [450 195 40 15];
                app.V3.Text = '3';

                % Create Label17
                app.MBV = uilabel(app.UIFigure);
                app.MBV.Position = [261 176 60 15];
                app.MBV.Text = 'ECW';

                % Create Label18
                app.MTV = uilabel(app.UIFigure);
                app.MTV.Position = [261 195 60 15];
                app.MTV.Text = 'ECCW';

                % Create Label19
                app.MTN = uilabel(app.UIFigure);
                app.MTN.Position = [171 195 81 15];
                app.MTN.Text = 'Encoder CCW:';

                % Create Label20
                app.MBN = uilabel(app.UIFigure);
                app.MBN.Position = [171 176 81 15];
                app.MBN.Text = 'Encoder CW:';

                % Create Label17
                app.MBBV = uilabel(app.UIFigure);
                app.MBBV.Position = [261 138 60 15];
                app.MBBV.Text = '---';
                app.MBBV.Visible = 'off';

                % Create Label18
                app.MTTV = uilabel(app.UIFigure);
                app.MTTV.Position = [261 157 60 15];
                app.MTTV.Text = '---';
                app.MTTV.Visible = 'off';

                % Create Label19
                app.MTTN = uilabel(app.UIFigure);
                app.MTTN.Position = [171 157 81 15];
                app.MTTN.Text = 'Encoder CCW:';
                app.MTTN.Visible = 'off';

                % Create Label20
                app.MBBN = uilabel(app.UIFigure);
                app.MBBN.Position = [171 138 81 15];
                app.MBBN.Text = 'Encoder CW:';
                app.MBBN.Visible = 'off';

                % Create REC
                app.REC = uibutton(app.UIFigure, 'state');
                app.REC.ValueChangedFcn = createCallbackFcn(app, @RECValueChanged);
                app.REC.Text = 'REC';
                app.REC.BackgroundColor = [1 1 1];
                app.REC.Position = [641 507 32 28];

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

                app.FigSTART = uibutton(app.RECFigure, 'push');
                app.FigCancel = uibutton(app.RECFigure, 'push');
                app.FigLabelListBox = uilabel(app.RECFigure);
                app.FigMotor = uilistbox(app.RECFigure);
                app.FigLabel = uilabel(app.RECFigure);
                app.FigType = uilistbox(app.RECFigure);
                app.FigNew = uibutton(app.RECFigure, 'push');
                app.FigContinue = uibutton(app.RECFigure, 'push');
                app.FigLabel2 = uilabel(app.RECFigure);
                app.FigVoltage = uilistbox(app.RECFigure);
                app.FigLabelListBox4 = uilabel(app.RECFigure);
                app.FigFile = uilistbox(app.RECFigure);
                app.EditField = uieditfield(app.RECFigure, 'text');
                app.LabelEditField = uilabel(app.RECFigure);

            end
        end

        methods (Access = public)

            % Construct app
            function app = Stand_SW()

                delete(timerfindall);
                app.Timer = timer('Period', app.timerStep, ...
                'ExecutionMode','fixedDelay','TimerFcn',{@app.TimingFcn});   %,'fixedSpacing'
                %start(app.Timer);

                % Create and configure components
                createComponents(app)

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

                app.com=STAND.createD2xx;
                [~,isOK]=STAND.open(app.com);
                if isOK
                    app.LED2.Color = [0, 1, 0];
                    app.Label3.Text = 'Connected';
                    STAND.Send(app.com,'X');
                    app.Connect.Value = 1;
                else
                    app.LED2.Color = [1, 0, 0];
                    app.Label3.Text = 'Disconnected';
                    app.Connect.Value = 0;
                end

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

                if nargout == 0
                    clear app
                end
            end

            % Code that executes before app deletion
            function delete(app)
    %             pause(0.1);
    %             if(app.Connect.Value)
    %                 app.ConnectValueChanged;
    %             end
                if(app.Connect.Value)
                    STAND.Send(app.com,'Y');
                    [~,isOK]=STAND.close(app.com);
                end

                fclose(app.fileID);
                delete(app.Timer);
                % Delete UIFigure when app is deleted
                delete(app.RECFigure);
                delete(app.UIFigure)
            end
        end
    end