classdef PolarPlotAppExample < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        aEditFieldLabel  matlab.ui.control.Label
        aEditField       matlab.ui.control.NumericEditField
        bEditFieldLabel  matlab.ui.control.Label
        bEditField       matlab.ui.control.NumericEditField
        DropDown         matlab.ui.control.DropDown
        BaseLabel        matlab.ui.control.Label
        ExponentLabel    matlab.ui.control.Label
    end

    
    properties (Access = private)
        Pax % Polar axes
    end
    
    methods (Access = private)
        
        function updateplot(app)
            
            % Define theta. Get a and b.
            theta = 0:pi/1800:2*pi;
            a = app.aEditField.Value;
            b = app.bEditField.Value;
            
            % Calculate r and plot it
            r = (1 + sin(a.*theta)).^(1/b);
            plotline = polarplot(app.Pax, theta, r);
            
            % Set line color
            plotline.Color = app.DropDown.Value;
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            % Replace 0 in equation with unicode theta
            app.BaseLabel.Text = ['r = ( 1 + sin(a' char(00952) ') )'];
            
            % Create polar axes and position it in pixels
            app.Pax = polaraxes(app.UIFigure);
            app.Pax.Units = 'pixels';
            app.Pax.Position = [260 55 230 230];
            
            % Plot the polar function
            updateplot(app);
        end

        % Value changed function: DropDown, aEditField, bEditField
        function ComponentValueChanged(app, event)
            % Plot the polar function
            updateplot(app);
        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.AutoResizeChildren = 'off';
            app.UIFigure.Position = [100 100 580 380];
            app.UIFigure.Name = 'Polar Plot';
            app.UIFigure.Resize = 'off';

            % Create aEditFieldLabel
            app.aEditFieldLabel = uilabel(app.UIFigure);
            app.aEditFieldLabel.HorizontalAlignment = 'right';
            app.aEditFieldLabel.Position = [49 209 25 22];
            app.aEditFieldLabel.Text = 'a';

            % Create aEditField
            app.aEditField = uieditfield(app.UIFigure, 'numeric');
            app.aEditField.ValueChangedFcn = createCallbackFcn(app, @ComponentValueChanged, true);
            app.aEditField.Position = [89 209 70 22];
            app.aEditField.Value = 5;

            % Create bEditFieldLabel
            app.bEditFieldLabel = uilabel(app.UIFigure);
            app.bEditFieldLabel.HorizontalAlignment = 'right';
            app.bEditFieldLabel.Position = [49 156 25 22];
            app.bEditFieldLabel.Text = 'b';

            % Create bEditField
            app.bEditField = uieditfield(app.UIFigure, 'numeric');
            app.bEditField.Limits = [2.22044604925031e-16 Inf];
            app.bEditField.ValueChangedFcn = createCallbackFcn(app, @ComponentValueChanged, true);
            app.bEditField.Position = [89 156 70 22];
            app.bEditField.Value = 2;

            % Create DropDown
            app.DropDown = uidropdown(app.UIFigure);
            app.DropDown.Items = {'Blue Line', 'Red Line', 'Green Line', 'Black Line'};
            app.DropDown.ItemsData = {'b', 'r', 'g', 'k'};
            app.DropDown.ValueChangedFcn = createCallbackFcn(app, @ComponentValueChanged, true);
            app.DropDown.Position = [65 103 94 22];
            app.DropDown.Value = 'b';

            % Create BaseLabel
            app.BaseLabel = uilabel(app.UIFigure);
            app.BaseLabel.FontSize = 30;
            app.BaseLabel.Position = [263 317 231 36];
            app.BaseLabel.Text = 'r = ( 1 + sin(a0) )';

            % Create ExponentLabel
            app.ExponentLabel = uilabel(app.UIFigure);
            app.ExponentLabel.FontSize = 20;
            app.ExponentLabel.Position = [484 341 33 24];
            app.ExponentLabel.Text = '1/b';

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

            % Create UIFigure and components
            createComponents(app)

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

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

            if nargout == 0
                clear app
            end
        end

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

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