classdef aplikacja < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                matlab.ui.Figure
        WzrostEditField         matlab.ui.control.EditField
        WzrostEditFieldLabel    matlab.ui.control.Label
        WiekEditField           matlab.ui.control.EditField
        WiekEditFieldLabel      matlab.ui.control.Label
        NazwiskoEditField       matlab.ui.control.EditField
        NazwiskoEditFieldLabel  matlab.ui.control.Label
        ImieEditField           matlab.ui.control.EditField
        ImieEditFieldLabel      matlab.ui.control.Label
        TestButton              matlab.ui.control.Button
        SaveButton              matlab.ui.control.Button
        ExitButton              matlab.ui.control.Button
        ClearButton             matlab.ui.control.Button
        PlotButton              matlab.ui.control.Button
        UIAxes                  matlab.ui.control.UIAxes
        ContextMenu             matlab.ui.container.ContextMenu
        Menu                    matlab.ui.container.Menu
        Menu2                   matlab.ui.container.Menu
    end

    
    properties (Access = private)
        X=1:6;
        Dane=[1,2,5,-3,4,3]; % Description
        Wiek
        Wzrost
        Imie
        Nazwisko

    end
    
    methods (Access = private)

        
        function chacking_edit_field(app)
            if isempty(app.ImieEditField.Value) || isempty(app.NazwiskoEditField.Value) ...
                    || isempty(app.WiekEditField.Value) ...
                    || isempty(app.WzrostEditField.Value) % || OR (LUB)
                app.SaveButton.Enable="off";
            else
                app.SaveButton.Enable="on";
            end
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            T=readtable("Moja_baza.txt");
            T.Properties.VariableNames
            app.Wiek=T.Wiek;
            app.Wzrost=T.Wzrost;   
            app.Imie=T.Imie;
            app.Nazwisko=T.Nazwisko;
            
        end

        % Button pushed function: PlotButton
        function PlotButtonPushed(app, event)
            
            Y=app.Wiek;
            plot(app.UIAxes,Y)
            n=length(app.Wiek);
            app.UIAxes.XTick=1:n;
            app.UIAxes.XTickLabel=[app.Imie+" "+app.Nazwisko];
            app.UIAxes.XTickLabelRotation=30; % obrócenie podpisu XTick o 90 stopni
            app.UIAxes.YLabel.String="Wiek [lata]";

        end

        % Button pushed function: ClearButton
        function ClearButtonPushed(app, event)
            cla(app.UIAxes) % czyszczenie wykresów na osiach
        end

        % Button pushed function: ExitButton
        function ExitButtonPushed(app, event)
            Imie=app.Imie;
            Nazwisko=app.Nazwisko;
            Wiek=app.Wiek;
            Wzrost=app.Wzrost;
            
            T=table(Imie,Nazwisko,Wiek,Wzrost);
            writetable(T,"Moja_baza.txt")
            close(app.UIFigure)
        end

        % Button pushed function: SaveButton
        function SaveButtonPushed(app, event)
            imie=app.ImieEditField.Value;
            nazwisko=app.NazwiskoEditField.Value;
            wiek=str2double(app.WiekEditField.Value);
            wzrost=str2double(app.WzrostEditField.Value);
            app.Imie=[app.Imie;imie];
            app.Nazwisko=[app.Nazwisko;nazwisko];
            app.Wiek=[app.Wiek;wiek];
            app.Wzrost=[app.Wzrost;wzrost];

            app.ImieEditField.Value="";
            app.NazwiskoEditField.Value="";
            app.WiekEditField.Value="";
            app.WzrostEditField.Value="";
            
        end

        % Value changed function: ImieEditField
        function ImieEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Value changed function: NazwiskoEditField
        function NazwiskoEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Value changed function: WiekEditField
        function WiekEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Value changed function: WzrostEditField
        function WzrostEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Button pushed function: TestButton
        function TestButtonPushed(app, event)
            clc
            app.Imie
            app.Nazwisko
            app.Wiek
            app.Wzrost
        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 920 578];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            app.UIAxes.Position = [348 125 573 454];

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

            % Create ClearButton
            app.ClearButton = uibutton(app.UIFigure, 'push');
            app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
            app.ClearButton.Position = [679 28 100 23];
            app.ClearButton.Text = 'Clear';

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

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

            % Create TestButton
            app.TestButton = uibutton(app.UIFigure, 'push');
            app.TestButton.ButtonPushedFcn = createCallbackFcn(app, @TestButtonPushed, true);
            app.TestButton.Position = [20 28 100 23];
            app.TestButton.Text = 'Test';

            % Create ImieEditFieldLabel
            app.ImieEditFieldLabel = uilabel(app.UIFigure);
            app.ImieEditFieldLabel.HorizontalAlignment = 'right';
            app.ImieEditFieldLabel.Position = [49 531 27 22];
            app.ImieEditFieldLabel.Text = 'Imie';

            % Create ImieEditField
            app.ImieEditField = uieditfield(app.UIFigure, 'text');
            app.ImieEditField.ValueChangedFcn = createCallbackFcn(app, @ImieEditFieldValueChanged, true);
            app.ImieEditField.Position = [91 531 100 22];

            % Create NazwiskoEditFieldLabel
            app.NazwiskoEditFieldLabel = uilabel(app.UIFigure);
            app.NazwiskoEditFieldLabel.HorizontalAlignment = 'right';
            app.NazwiskoEditFieldLabel.Position = [20 501 57 22];
            app.NazwiskoEditFieldLabel.Text = 'Nazwisko';

            % Create NazwiskoEditField
            app.NazwiskoEditField = uieditfield(app.UIFigure, 'text');
            app.NazwiskoEditField.ValueChangedFcn = createCallbackFcn(app, @NazwiskoEditFieldValueChanged, true);
            app.NazwiskoEditField.Position = [91 501 100 22];

            % Create WiekEditFieldLabel
            app.WiekEditFieldLabel = uilabel(app.UIFigure);
            app.WiekEditFieldLabel.HorizontalAlignment = 'right';
            app.WiekEditFieldLabel.Position = [44 468 31 22];
            app.WiekEditFieldLabel.Text = 'Wiek';

            % Create WiekEditField
            app.WiekEditField = uieditfield(app.UIFigure, 'text');
            app.WiekEditField.ValueChangedFcn = createCallbackFcn(app, @WiekEditFieldValueChanged, true);
            app.WiekEditField.Position = [91 468 100 22];

            % Create WzrostEditFieldLabel
            app.WzrostEditFieldLabel = uilabel(app.UIFigure);
            app.WzrostEditFieldLabel.HorizontalAlignment = 'right';
            app.WzrostEditFieldLabel.Position = [34 435 42 22];
            app.WzrostEditFieldLabel.Text = 'Wzrost';

            % Create WzrostEditField
            app.WzrostEditField = uieditfield(app.UIFigure, 'text');
            app.WzrostEditField.ValueChangedFcn = createCallbackFcn(app, @WzrostEditFieldValueChanged, true);
            app.WzrostEditField.Position = [91 435 100 22];

            % Create ContextMenu
            app.ContextMenu = uicontextmenu(app.UIFigure);

            % Create Menu
            app.Menu = uimenu(app.ContextMenu);
            app.Menu.Text = 'Menu';

            % Create Menu2
            app.Menu2 = uimenu(app.ContextMenu);
            app.Menu2.Text = 'Menu2';
            
            % Assign app.ContextMenu
            app.UIFigure.ContextMenu = app.ContextMenu;

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

            % 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