Alexander
Alexander

Reputation: 1

App designer generated app freezes when I run the code

I'm trying to find normal depth and critical depth using flow parameters with app designer.

First, I defined the variables globally and set the parameters equal to their abbreviations. Then, I defined the all variables and defined function CALCULATEButtonPushed(app, event) for finding normal depth and critical depth.

When I enter the parameters and run the "Calculate" button, I want to see the normal depth and critical depth in the box.

When I first ran the code by entering the parameters (BB, ZZ, S0, N, QQ), it was working fine, but today, when I run the "Calculate" button, the system is stuck with a bug. If there is something I missed in the code, I would be glad if you could help me. appdesigner image

classdef example < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        TabGroup                       matlab.ui.container.TabGroup
        SteadyTab                      matlab.ui.container.Tab
        CALCULATEButton                matlab.ui.control.Button
        INPUTPARAMETERSLabel           matlab.ui.control.Label
        CRITICALDEPTHEditField         matlab.ui.control.NumericEditField
        CRITICALDEPTHEditFieldLabel    matlab.ui.control.Label
        NORMALDEPTHEditField           matlab.ui.control.NumericEditField
        NORMALDEPTHEditFieldLabel      matlab.ui.control.Label
        STEADYSTATEDISCHARGEEditField  matlab.ui.control.NumericEditField
        STEADYSTATEDISCHARGEEditFieldLabel  matlab.ui.control.Label
        MANNINGsVALUEEditField         matlab.ui.control.NumericEditField
        MANNINGsVALUEEditFieldLabel    matlab.ui.control.Label
        BEDSLOPEEditField              matlab.ui.control.NumericEditField
        BEDSLOPEEditFieldLabel         matlab.ui.control.Label
        SIDEINCLINATIONEditField       matlab.ui.control.NumericEditField
        SIDEINCLINATIONEditFieldLabel  matlab.ui.control.Label
        BOTTOMWIDTHmEditField          matlab.ui.control.NumericEditField
        BOTTOMWIDTHmEditFieldLabel     matlab.ui.control.Label
        Unsteady2DTab                  matlab.ui.container.Tab
    end

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

        % Value changed function: BOTTOMWIDTHmEditField
        function BOTTOMWIDTHmEditFieldValueChanged(app, event)
        global BB
            BB = app.BOTTOMWIDTHmEditField.Value;
            
        end

        % Value changed function: SIDEINCLINATIONEditField
        function SIDEINCLINATIONEditFieldValueChanged(app, event)
        global ZZ
            ZZ = app.SIDEINCLINATIONEditField.Value;
            
        end

        % Value changed function: BEDSLOPEEditField
        function BEDSLOPEEditFieldValueChanged(app, event)
        global S0
            S0 = app.BEDSLOPEEditField.Value;
            
        end

        % Value changed function: MANNINGsVALUEEditField
        function MANNINGsVALUEEditFieldValueChanged(app, event)
        global N   
            N = app.MANNINGsVALUEEditField.Value;
            
        end

        % Value changed function: STEADYSTATEDISCHARGEEditField
        function STEADYSTATEDISCHARGEEditFieldValueChanged(app, event)
         global QQ 
            QQ = app.STEADYSTATEDISCHARGEEditField.Value;
            
        end

        % Button pushed function: CALCULATEButton
        function CALCULATEButtonPushed(app, event)
        global YNN BB ZZ QQ S0 N YC
       

          % function A = AREA(B, YY, Z)
          %       A = (B + Z * YY) * YY;
          % end
         
          AREA = @(B, YY, Z) (B + Z*YY) * YY;
          PERI = @(B, YY, Z) B + 2*YY*(1 + Z^2)^0.5;  
          


             % Calculate normal depth
         YNN = 0.01;
         while true
             ETOTAL = (QQ*N)/S0^0.5;
             ECHECK = AREA(BB, YNN, ZZ)^(5/3) / PERI(BB, YNN, ZZ)^(2/3);
             if ETOTAL < ECHECK
                 break;
             end
             YNN = YNN + 0.01;
         end

                % Calculate critical depth
        W = QQ / (3.132 * BB^2.5);
        if ZZ == 0
            YCOB = W^0.672717 * 1.022785;
        elseif ZZ == 0.5
            YCOB = W^0.602684 * 0.748173;
        elseif ZZ == 1
            YCOB = W^0.574105 * 0.657397;
        elseif ZZ == 1.5
            YCOB = W^0.564345 * 0.625201;
        elseif ZZ == 2
            YCOB = W^0.552851 * 0.584043;
        elseif ZZ == 2.5
            YCOB = W^0.545379 * 0.552333;
        elseif ZZ == 3
            YCOB = W^0.537298 * 0.523298;
        elseif ZZ == 4
            YCOB = W^0.524667 * 0.487638;
        end
        YC = YCOB * BB;
        
        app.NORMALDEPTHEditField.Value = YNN;
        app.CRITICALDEPTHEditField.Value = YC;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

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

            % Create TabGroup
            app.TabGroup = uitabgroup(app.UIFigure);
            app.TabGroup.Position = [3 79 1014 492];

            % Create SteadyTab
            app.SteadyTab = uitab(app.TabGroup);
            app.SteadyTab.Title = 'Steady';

            % Create BOTTOMWIDTHmEditFieldLabel
            app.BOTTOMWIDTHmEditFieldLabel = uilabel(app.SteadyTab);
            app.BOTTOMWIDTHmEditFieldLabel.HorizontalAlignment = 'right';
            app.BOTTOMWIDTHmEditFieldLabel.Position = [21 377 120 22];
            app.BOTTOMWIDTHmEditFieldLabel.Text = 'BOTTOM WIDTH (m)';

            % Create BOTTOMWIDTHmEditField
            app.BOTTOMWIDTHmEditField = uieditfield(app.SteadyTab, 'numeric');
            app.BOTTOMWIDTHmEditField.ValueChangedFcn = createCallbackFcn(app, @BOTTOMWIDTHmEditFieldValueChanged, true);
            app.BOTTOMWIDTHmEditField.Position = [156 377 40 22];

            % Create SIDEINCLINATIONEditFieldLabel
            app.SIDEINCLINATIONEditFieldLabel = uilabel(app.SteadyTab);
            app.SIDEINCLINATIONEditFieldLabel.HorizontalAlignment = 'right';
            app.SIDEINCLINATIONEditFieldLabel.Position = [30 340 111 22];
            app.SIDEINCLINATIONEditFieldLabel.Text = 'SIDE INCLINATION';

            % Create SIDEINCLINATIONEditField
            app.SIDEINCLINATIONEditField = uieditfield(app.SteadyTab, 'numeric');
            app.SIDEINCLINATIONEditField.ValueChangedFcn = createCallbackFcn(app, @SIDEINCLINATIONEditFieldValueChanged, true);
            app.SIDEINCLINATIONEditField.Position = [156 340 40 22];

            % Create BEDSLOPEEditFieldLabel
            app.BEDSLOPEEditFieldLabel = uilabel(app.SteadyTab);
            app.BEDSLOPEEditFieldLabel.HorizontalAlignment = 'right';
            app.BEDSLOPEEditFieldLabel.Position = [68 303 73 22];
            app.BEDSLOPEEditFieldLabel.Text = 'BED SLOPE';

            % Create BEDSLOPEEditField
            app.BEDSLOPEEditField = uieditfield(app.SteadyTab, 'numeric');
            app.BEDSLOPEEditField.ValueChangedFcn = createCallbackFcn(app, @BEDSLOPEEditFieldValueChanged, true);
            app.BEDSLOPEEditField.Position = [156 303 40 22];

            % Create MANNINGsVALUEEditFieldLabel
            app.MANNINGsVALUEEditFieldLabel = uilabel(app.SteadyTab);
            app.MANNINGsVALUEEditFieldLabel.HorizontalAlignment = 'right';
            app.MANNINGsVALUEEditFieldLabel.Position = [29 266 112 22];
            app.MANNINGsVALUEEditFieldLabel.Text = 'MANNING''s VALUE';

            % Create MANNINGsVALUEEditField
            app.MANNINGsVALUEEditField = uieditfield(app.SteadyTab, 'numeric');
            app.MANNINGsVALUEEditField.ValueChangedFcn = createCallbackFcn(app, @MANNINGsVALUEEditFieldValueChanged, true);
            app.MANNINGsVALUEEditField.Position = [156 266 40 22];

            % Create STEADYSTATEDISCHARGEEditFieldLabel
            app.STEADYSTATEDISCHARGEEditFieldLabel = uilabel(app.SteadyTab);
            app.STEADYSTATEDISCHARGEEditFieldLabel.HorizontalAlignment = 'right';
            app.STEADYSTATEDISCHARGEEditFieldLabel.Position = [253 372 168 22];
            app.STEADYSTATEDISCHARGEEditFieldLabel.Text = 'STEADY STATE DISCHARGE';

            % Create STEADYSTATEDISCHARGEEditField
            app.STEADYSTATEDISCHARGEEditField = uieditfield(app.SteadyTab, 'numeric');
            app.STEADYSTATEDISCHARGEEditField.ValueChangedFcn = createCallbackFcn(app, @STEADYSTATEDISCHARGEEditFieldValueChanged, true);
            app.STEADYSTATEDISCHARGEEditField.Position = [436 372 40 22];

            % Create NORMALDEPTHEditFieldLabel
            app.NORMALDEPTHEditFieldLabel = uilabel(app.SteadyTab);
            app.NORMALDEPTHEditFieldLabel.HorizontalAlignment = 'right';
            app.NORMALDEPTHEditFieldLabel.Position = [319 295 100 22];
            app.NORMALDEPTHEditFieldLabel.Text = 'NORMAL DEPTH';

            % Create NORMALDEPTHEditField
            app.NORMALDEPTHEditField = uieditfield(app.SteadyTab, 'numeric');
            app.NORMALDEPTHEditField.Position = [434 295 40 22];

            % Create CRITICALDEPTHEditFieldLabel
            app.CRITICALDEPTHEditFieldLabel = uilabel(app.SteadyTab);
            app.CRITICALDEPTHEditFieldLabel.HorizontalAlignment = 'right';
            app.CRITICALDEPTHEditFieldLabel.Position = [316 258 103 22];
            app.CRITICALDEPTHEditFieldLabel.Text = 'CRITICAL DEPTH';

            % Create CRITICALDEPTHEditField
            app.CRITICALDEPTHEditField = uieditfield(app.SteadyTab, 'numeric');
            app.CRITICALDEPTHEditField.Position = [434 258 40 22];

            % Create INPUTPARAMETERSLabel
            app.INPUTPARAMETERSLabel = uilabel(app.SteadyTab);
            app.INPUTPARAMETERSLabel.FontWeight = 'bold';
            app.INPUTPARAMETERSLabel.Position = [44 421 127 22];
            app.INPUTPARAMETERSLabel.Text = 'INPUT PARAMETERS';

            % Create CALCULATEButton
            app.CALCULATEButton = uibutton(app.SteadyTab, 'push');
            app.CALCULATEButton.ButtonPushedFcn = createCallbackFcn(app, @CALCULATEButtonPushed, true);
            app.CALCULATEButton.Position = [360 224 100 23];
            app.CALCULATEButton.Text = 'CALCULATE';

            % Create Unsteady2DTab
            app.Unsteady2DTab = uitab(app.TabGroup);
            app.Unsteady2DTab.Title = 'Unsteady 2D';

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

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = example

            % Create UIFigure and components
            createComponents(app)

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

            if nargout == 0
                clear app
            end
        end

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

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

Upvotes: 0

Views: 46

Answers (0)

Related Questions