classdef ConfigurePlotAppExample < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        ConfigurePlotUIFigure     matlab.ui.Figure
        SampleSizeEditField       matlab.ui.control.NumericEditField
        SampleSizeEditFieldLabel  matlab.ui.control.Label
        UpdatePlotButton          matlab.ui.control.Button
        ColormapDropDown          matlab.ui.control.DropDown
        ColormapDropDownLabel     matlab.ui.control.Label
        UIAxes                    matlab.ui.control.UIAxes
    end


    properties (Access = private)
        Z = peaks(35);  % Surface data to plot
    end

    methods (Access = private)
    
        function plotsurface(app)
            % Plot Z
            surf(app.UIAxes,app.Z);
            
            % Set the colormap
            cmap = app.ColormapDropDown.Value;
            colormap(app.UIAxes,cmap);    
        end
        
    end

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

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

        % Button pushed function: UpdatePlotButton
        function UpdatePlotButtonPushed(app, event)
            plotsurface(app);
        end

        % Value changed function: SampleSizeEditField
        function SampleSizeEditFieldValueChanged(app, event)
            sampleSize = app.SampleSizeEditField.Value;
            
            % Update the Z property
            app.Z = peaks(sampleSize);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create ConfigurePlotUIFigure and hide until all components are created
            app.ConfigurePlotUIFigure = uifigure('Visible', 'off');
            app.ConfigurePlotUIFigure.Position = [100 100 495 365];
            app.ConfigurePlotUIFigure.Name = 'Configure Plot';

            % Create UIAxes
            app.UIAxes = uiaxes(app.ConfigurePlotUIFigure);
            app.UIAxes.Position = [150 32 323 287];

            % Create ColormapDropDownLabel
            app.ColormapDropDownLabel = uilabel(app.ConfigurePlotUIFigure);
            app.ColormapDropDownLabel.HorizontalAlignment = 'right';
            app.ColormapDropDownLabel.VerticalAlignment = 'top';
            app.ColormapDropDownLabel.Position = [44 202 59 15];
            app.ColormapDropDownLabel.Text = 'Colormap';

            % Create ColormapDropDown
            app.ColormapDropDown = uidropdown(app.ConfigurePlotUIFigure);
            app.ColormapDropDown.Items = {'Parula', 'Jet', 'Winter', 'Cool'};
            app.ColormapDropDown.Position = [24 175 100 22];
            app.ColormapDropDown.Value = 'Parula';

            % Create UpdatePlotButton
            app.UpdatePlotButton = uibutton(app.ConfigurePlotUIFigure, 'push');
            app.UpdatePlotButton.ButtonPushedFcn = createCallbackFcn(app, @UpdatePlotButtonPushed, true);
            app.UpdatePlotButton.Position = [24 122 100 22];
            app.UpdatePlotButton.Text = 'Update Plot';

            % Create SampleSizeEditFieldLabel
            app.SampleSizeEditFieldLabel = uilabel(app.ConfigurePlotUIFigure);
            app.SampleSizeEditFieldLabel.HorizontalAlignment = 'right';
            app.SampleSizeEditFieldLabel.VerticalAlignment = 'top';
            app.SampleSizeEditFieldLabel.Position = [37 257 74 15];
            app.SampleSizeEditFieldLabel.Text = 'Sample Size';

            % Create SampleSizeEditField
            app.SampleSizeEditField = uieditfield(app.ConfigurePlotUIFigure, 'numeric');
            app.SampleSizeEditField.Limits = [2 1000];
            app.SampleSizeEditField.ValueChangedFcn = createCallbackFcn(app, @SampleSizeEditFieldValueChanged, true);
            app.SampleSizeEditField.Position = [24 230 100 22];
            app.SampleSizeEditField.Value = 35;

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

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = ConfigurePlotAppExample

            % Create UIFigure and components
            createComponents(app)

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

            % 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.ConfigurePlotUIFigure)
        end
    end
end