classdef TableDataAppExample < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        BloodPressureAnalyisisUIFigure  matlab.ui.Figure
        UITable                         matlab.ui.control.Table
        UIAxes2                         matlab.ui.control.UIAxes
        UIAxes                          matlab.ui.control.UIAxes
    end


    methods (Access = private)
    
        function updateplot(app)
            % Get Table UI component data
            t = app.UITable.DisplayData;            
            
            % Plot modified data 
            x2 = t.Age;
            y2 = t.BloodPressure(:,2);
            plot(app.UIAxes2,x2,y2,'-o');
        end
        
    end


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

        % Code that executes after component creation
        function startupFcn(app)
            % Read table array from file
            t = readtable('patients.xls');
            vars = {'Age','Systolic','Diastolic','SelfAssessedHealthStatus','Smoker'};
            
            % Select a subset of the table array
            t = t(1:20,vars);
            
            % Sort the data by age
            t = sortrows(t,'Age');
            
            % Combine Systolic and Diastolic into one variable
            t.BloodPressure = [t.Systolic t.Diastolic];
            t.Systolic = [];
            t.Diastolic = [];
            
            % Convert SelfAssessedHealthStatus to categorical
            cats = categorical(t.SelfAssessedHealthStatus,{'Poor','Fair','Good','Excellent'});
            t.SelfAssessedHealthStatus = cats;
            
            % Rearrange columns
            t = t(:,[1 4 3 2]);
            
            % Add data to the Table UI Component
            app.UITable.Data = t;
            
            % Plot the original data
            x1 = app.UITable.Data.Age;
            y1 = app.UITable.Data.BloodPressure(:,2);
            plot(app.UIAxes,x1,y1,'o-');
            
            % Plot the data
            updateplot(app);
        end

        % Display data changed function: UITable
        function UITableDisplayDataChanged(app, event)
            % Update the plots when user sorts the columns of the table
            updateplot(app);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create BloodPressureAnalyisisUIFigure and hide until all components are created
            app.BloodPressureAnalyisisUIFigure = uifigure('Visible', 'off');
            app.BloodPressureAnalyisisUIFigure.Position = [100 100 733 478];
            app.BloodPressureAnalyisisUIFigure.Name = 'Blood Pressure Analyisis';

            % Create UIAxes
            app.UIAxes = uiaxes(app.BloodPressureAnalyisisUIFigure);
            title(app.UIAxes, 'Original Data')
            xlabel(app.UIAxes, 'Age')
            ylabel(app.UIAxes, 'Diastolic Blood Pressure')
            app.UIAxes.Toolbar.Visible = 'off';
            app.UIAxes.Position = [28 259 297 189];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.BloodPressureAnalyisisUIFigure);
            title(app.UIAxes2, 'Updated Data')
            xlabel(app.UIAxes2, 'Age')
            ylabel(app.UIAxes2, 'Diastolic Blood Pressure')
            app.UIAxes2.Toolbar.Visible = 'off';
            app.UIAxes2.Position = [373 263 300 185];

            % Create UITable
            app.UITable = uitable(app.BloodPressureAnalyisisUIFigure);
            app.UITable.ColumnName = {'Age'; 'Blood Pressure'; 'Smoker'; 'Health Status'};
            app.UITable.RowName = {};
            app.UITable.ColumnSortable = [true false true true];
            app.UITable.ColumnEditable = [true false true true];
            app.UITable.DisplayDataChangedFcn = createCallbackFcn(app, @UITableDisplayDataChanged, true);
            app.UITable.Position = [36 46 637 182];

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

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = TableDataAppExample

            % Create UIFigure and components
            createComponents(app)

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

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