classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                matlab.ui.Figure
        SaveButton              matlab.ui.control.Button
        HighEditField           matlab.ui.control.NumericEditField
        HighEditFieldLabel      matlab.ui.control.Label
        AgeEditField            matlab.ui.control.NumericEditField
        AgeEditFieldLabel       matlab.ui.control.Label
        SurenameEditField       matlab.ui.control.EditField
        SurenameEditFieldLabel  matlab.ui.control.Label
        NameEditField           matlab.ui.control.EditField
        NameEditFieldLabel      matlab.ui.control.Label
        LoadButton              matlab.ui.control.Button
        AmplifierSlider         matlab.ui.control.Slider
        AmplifierSliderLabel    matlab.ui.control.Label
        PlotButton              matlab.ui.control.Button
        ExitButton              matlab.ui.control.Button
        UIAxes                  matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        xData=1:10;
        yData=[1:10].^2;
        name
        surename
        age
        high
    end
    
    methods (Access = private)
        
        function draw(app)
            plot(app.UIAxes,app.xData,app.yData);
        end

        
        function saveEnable(app)
            if (~isempty(app.NameEditField.Value) & ...
               ~isempty(app.SurenameEditField.Value) & ...
               app.AgeEditField.Value && ...
               app.HighEditField.Value) % jeśli są spełnione opcje
             app.SaveButton.Enable="on";
            else
                app.SaveButton.Enable="off";
            end
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            grid(app.UIAxes,"on");
            T=readtable("Nowa_baza.txt");
            app.age=T.Wiek;
            app.name=T.Imie;
            app.surename=T.Nazwisko;
            app.high=T.Wzrost;
        end

        % Button pushed function: ExitButton
        function ExitButtonPushed(app, event)
            % Zapisanie bazy danych
            Imie=app.name;
            Nazwisko=app.surename;
            Wiek=app.age;
            Wzrost=app.high;
            T=table(Imie,Nazwisko,Wiek,Wzrost);
            writetable(T,'Nowa_baza.txt')
            close(app.UIFigure)
        end

        % Button pushed function: PlotButton
        function PlotButtonPushed(app, event)
            draw(app);
            
        end

        % Value changing function: AmplifierSlider
        function AmplifierSliderValueChanging(app, event)
            changingValue = event.Value;
            app.yData=changingValue*[1:10].^2; %wzmacniamy wykres
            app.UIAxes.YLim=[0,1000];
            draw(app)
        end

        % Button pushed function: LoadButton
        function LoadButtonPushed(app, event)
            
            wiek=app.age;
            n=length(wiek); % liczba ludzi w bazie
            plot(app.UIAxes,wiek)
            app.UIAxes.YLim=[0,100]; % zmiana zakresu.
            app.UIAxes.XLim=[1,n];
            app.UIAxes.XTick=1:n; % utworzy linie pomocnicze co 1 do n  
            app.UIAxes.XTickLabel=app.name+" "+app.surename;
            app.UIAxes.XTickLabelRotation=30; % obrót podpisów osi x

        end

        % Button pushed function: SaveButton
        function SaveButtonPushed(app, event)
            imie=app.NameEditField.Value;
            nazwisko=app.SurenameEditField.Value;
            wiek=app.AgeEditField.Value;
            wzrost=app.HighEditField.Value;
% Aktualizacja bazy danych
        app.name=[app.name;imie];
        app.surename=[app.surename;nazwisko];
        app.age=[app.age;wiek];
        app.high=[app.high;wzrost];
        % Czyszczenie pól edycji
        app.NameEditField.Value="";
        app.SurenameEditField.Value="";
        app.AgeEditField.Value=0;
        app.HighEditField.Value=0;
        app.SaveButton.Enable="off"; % wyłączamy możliwość wprowadzania danych
        end

        % Value changed function: NameEditField
        function NameEditFieldValueChanged(app, event)
            saveEnable(app)
            
        end

        % Value changed function: SurenameEditField
        function SurenameEditFieldValueChanged(app, event)
            saveEnable(app)
            
        end

        % Value changed function: AgeEditField
        function AgeEditFieldValueChanged(app, event)
            saveEnable(app)
            
        end

        % Value changed function: HighEditField
        function HighEditFieldValueChanged(app, event)
            saveEnable(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.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Title')
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.YLim = [0 1000];
            app.UIAxes.Position = [205 84 436 397];

            % Create ExitButton
            app.ExitButton = uibutton(app.UIFigure, 'push');
            app.ExitButton.ButtonPushedFcn = createCallbackFcn(app, @ExitButtonPushed, true);
            app.ExitButton.Position = [520 16 100 23];
            app.ExitButton.Text = 'Exit';

            % Create PlotButton
            app.PlotButton = uibutton(app.UIFigure, 'push');
            app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
            app.PlotButton.Position = [408 15 100 23];
            app.PlotButton.Text = 'Plot';

            % Create AmplifierSliderLabel
            app.AmplifierSliderLabel = uilabel(app.UIFigure);
            app.AmplifierSliderLabel.HorizontalAlignment = 'right';
            app.AmplifierSliderLabel.Position = [189 37 52 22];
            app.AmplifierSliderLabel.Text = 'Amplifier';

            % Create AmplifierSlider
            app.AmplifierSlider = uislider(app.UIFigure);
            app.AmplifierSlider.Limits = [0.1 10];
            app.AmplifierSlider.ValueChangingFcn = createCallbackFcn(app, @AmplifierSliderValueChanging, true);
            app.AmplifierSlider.Position = [262 46 127 3];
            app.AmplifierSlider.Value = 1;

            % Create LoadButton
            app.LoadButton = uibutton(app.UIFigure, 'push');
            app.LoadButton.ButtonPushedFcn = createCallbackFcn(app, @LoadButtonPushed, true);
            app.LoadButton.Position = [409 48 100 23];
            app.LoadButton.Text = 'Load';

            % Create NameEditFieldLabel
            app.NameEditFieldLabel = uilabel(app.UIFigure);
            app.NameEditFieldLabel.HorizontalAlignment = 'right';
            app.NameEditFieldLabel.Position = [33 444 37 22];
            app.NameEditFieldLabel.Text = 'Name';

            % Create NameEditField
            app.NameEditField = uieditfield(app.UIFigure, 'text');
            app.NameEditField.ValueChangedFcn = createCallbackFcn(app, @NameEditFieldValueChanged, true);
            app.NameEditField.Position = [85 444 100 22];

            % Create SurenameEditFieldLabel
            app.SurenameEditFieldLabel = uilabel(app.UIFigure);
            app.SurenameEditFieldLabel.HorizontalAlignment = 'right';
            app.SurenameEditFieldLabel.Position = [11 406 59 22];
            app.SurenameEditFieldLabel.Text = 'Surename';

            % Create SurenameEditField
            app.SurenameEditField = uieditfield(app.UIFigure, 'text');
            app.SurenameEditField.ValueChangedFcn = createCallbackFcn(app, @SurenameEditFieldValueChanged, true);
            app.SurenameEditField.Position = [85 406 100 22];

            % Create AgeEditFieldLabel
            app.AgeEditFieldLabel = uilabel(app.UIFigure);
            app.AgeEditFieldLabel.HorizontalAlignment = 'right';
            app.AgeEditFieldLabel.Position = [44 366 26 22];
            app.AgeEditFieldLabel.Text = 'Age';

            % Create AgeEditField
            app.AgeEditField = uieditfield(app.UIFigure, 'numeric');
            app.AgeEditField.ValueChangedFcn = createCallbackFcn(app, @AgeEditFieldValueChanged, true);
            app.AgeEditField.Position = [85 366 100 22];

            % Create HighEditFieldLabel
            app.HighEditFieldLabel = uilabel(app.UIFigure);
            app.HighEditFieldLabel.HorizontalAlignment = 'right';
            app.HighEditFieldLabel.Position = [40 323 30 22];
            app.HighEditFieldLabel.Text = 'High';

            % Create HighEditField
            app.HighEditField = uieditfield(app.UIFigure, 'numeric');
            app.HighEditField.ValueChangedFcn = createCallbackFcn(app, @HighEditFieldValueChanged, true);
            app.HighEditField.Position = [85 323 100 22];

            % Create SaveButton
            app.SaveButton = uibutton(app.UIFigure, 'push');
            app.SaveButton.ButtonPushedFcn = createCallbackFcn(app, @SaveButtonPushed, true);
            app.SaveButton.Enable = 'off';
            app.SaveButton.Position = [91 279 100 23];
            app.SaveButton.Text = 'Save';

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

            % 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