classdef BloodPressure < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure          matlab.ui.Figure
        Label             matlab.ui.control.Label
        axBloodPressure   matlab.ui.control.UIAxes
        btgSize           matlab.ui.container.ButtonGroup
        rdbHeight         matlab.ui.control.RadioButton
        rdbWeight         matlab.ui.control.RadioButton
        btgBloodPressure  matlab.ui.container.ButtonGroup
        rdbSystolic       matlab.ui.control.RadioButton
        rdbDiastolic      matlab.ui.control.RadioButton
        pnlGender         matlab.ui.container.Panel
        cbxFemale         matlab.ui.control.CheckBox
        cbxMale           matlab.ui.control.CheckBox
    end


    properties (Access = private)
        patients % Description
    end

    methods (Access = private)
        
        function updatePlot(app)
            male = app.cbxMale.Value;
            female = app.cbxFemale.Value;
            if ~male & ~female
                delete(get(app.axBloodPressure,'Children'))
                return
            end
            xvar = app.btgSize.SelectedObject.Text;
            yvar = app.btgBloodPressure.SelectedObject.Text;
            x = app.patients.(xvar);
            y = app.patients.(yvar);
            app.axBloodPressure.XLabel.String = xvar;
            app.axBloodPressure.YLabel.String = yvar;
            if male
                idx = ~cellfun(@isempty, regexp(app.patients.Gender, 'Male'));
                scatter(app.axBloodPressure, x(idx), y(idx),'blue')
                hold(app.axBloodPressure, 'on')
            end
            if female
                idx = ~cellfun(@isempty, regexp(app.patients.Gender, 'Female'));
                scatter(app.axBloodPressure, x(idx), y(idx), 'red')
            end
            hold(app.axBloodPressure, 'off')
        end

    end


    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            load patients.mat
            app.patients.Gender = Gender;
            app.patients.Height = Height;
            app.patients.Weight = Weight;
            app.patients.Diastolic = Diastolic;
            app.patients.Systolic = Systolic;
            app.updatePlot
        end

        % Value changed function: cbxMale
        function cbxMaleValueChanged(app, event)
            app.updatePlot
        end

        % Value changed function: cbxFemale
        function cbxFemaleValueChanged(app, event)
            app.updatePlot
        end

        % Selection changed function: btgBloodPressure
        function btgBloodPressureSelectionChanged(app, event)
            app.updatePlot
        end

        % Selection changed function: btgSize
        function btgSizeSelectionChanged(app, event)
            app.updatePlot
            
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 608 440];
            app.UIFigure.Name = 'Patient Data';
            setAutoResize(app, app.UIFigure, true)

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.HorizontalAlignment = 'center';
            app.Label.FontSize = 14;
            app.Label.FontWeight = 'bold';
            app.Label.Position = [234.5 412 140 18];
            app.Label.Text = 'Blood Pressure Data';

            % Create axBloodPressure
            app.axBloodPressure = uiaxes(app.UIFigure);
            xlabel(app.axBloodPressure, 'Weight');
            ylabel(app.axBloodPressure, 'Diastolic');
            app.axBloodPressure.Position = [154 15 440 389];

            % Create btgSize
            app.btgSize = uibuttongroup(app.UIFigure);
            app.btgSize.SelectionChangedFcn = createCallbackFcn(app, @btgSizeSelectionChanged, true);
            app.btgSize.Title = 'Size';
            app.btgSize.Position = [36 35 97 80];

            % Create rdbHeight
            app.rdbHeight = uiradiobutton(app.btgSize);
            app.rdbHeight.Text = 'Height';
            app.rdbHeight.Position = [10 11 55 16];

            % Create rdbWeight
            app.rdbWeight = uiradiobutton(app.btgSize);
            app.rdbWeight.Text = 'Weight';
            app.rdbWeight.Position = [10 33 57 16];
            app.rdbWeight.Value = true;

            % Create btgBloodPressure
            app.btgBloodPressure = uibuttongroup(app.UIFigure);
            app.btgBloodPressure.SelectionChangedFcn = createCallbackFcn(app, @btgBloodPressureSelectionChanged, true);
            app.btgBloodPressure.Title = 'Blood Pressure';
            app.btgBloodPressure.Position = [36 178 97 80];

            % Create rdbSystolic
            app.rdbSystolic = uiradiobutton(app.btgBloodPressure);
            app.rdbSystolic.Text = 'Systolic';
            app.rdbSystolic.Position = [10 11 61 16];

            % Create rdbDiastolic
            app.rdbDiastolic = uiradiobutton(app.btgBloodPressure);
            app.rdbDiastolic.Text = 'Diastolic';
            app.rdbDiastolic.Position = [10 33 66 16];
            app.rdbDiastolic.Value = true;

            % Create pnlGender
            app.pnlGender = uipanel(app.UIFigure);
            app.pnlGender.Title = 'Gender';
            app.pnlGender.Position = [36 322 97 82];

            % Create cbxFemale
            app.cbxFemale = uicheckbox(app.pnlGender);
            app.cbxFemale.ValueChangedFcn = createCallbackFcn(app, @cbxFemaleValueChanged, true);
            app.cbxFemale.Text = 'Female';
            app.cbxFemale.Position = [11 10 60 16];

            % Create cbxMale
            app.cbxMale = uicheckbox(app.pnlGender);
            app.cbxMale.ValueChangedFcn = createCallbackFcn(app, @cbxMaleValueChanged, true);
            app.cbxMale.Text = 'Male';
            app.cbxMale.Position = [11 37 46 16];
            app.cbxMale.Value = true;
        end
    end

    methods (Access = public)

        % Construct app
        function app = BloodPressure()

            % Create and configure 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