classdef SmoothPlot < handle
    
    % User data
    properties (SetAccess = private, GetAccess = public)
        xData (:,1) double {mustBeReal}
        yData (:,1) double {mustBeReal}
    end

    % Internal properties for handling interface creation
    properties (Access = private)
        outData
        hndlFig
        hndlAxes
        hndlPlot
        hndlPlotResult
        hndlBtn
    end
    
    methods
        % Class constructor
        function obj = SmoothPlot(xData, yData)
            obj.xData = xData;
            obj.yData = yData;
            setInterface(obj);
        end

        % Method for getting resulting data
        function yRes = getResult(obj)
            yRes = obj.outData;
        end
    end

    % Methods that not modify class properties
    methods (Static)
        % Class destructor
        function delete()
        closereq;
        end
    end

    % Internal methods called only by class itself
    methods (Access = private)
        % Initialize interface
        function setInterface(obj)
            obj.hndlFig = uifigure;
            obj.hndlFig.Name = 'Smoothie';

            obj.hndlAxes = uiaxes(obj.hndlFig);
            obj.hndlAxes.Position = [10 100 540 300];
            obj.hndlAxes.XGrid = 'on';
            obj.hndlAxes.YGrid = 'on';
            obj.hndlAxes.XLim = [min(obj.xData) max(obj.xData)];
            obj.hndlAxes.YLim = [min(obj.yData) max(obj.yData)];

            obj.hndlPlot = line(obj.hndlAxes);
            obj.hndlPlot.XData = obj.xData;
            obj.hndlPlot.YData = obj.yData;
            obj.hndlPlot.LineWidth = 0.25;
            
            obj.hndlBtn = uibutton(obj.hndlFig);
            obj.hndlBtn.Position = [200 25 160 50];
            obj.hndlBtn.Text = 'Smooth';

            % Overriden function smooth
            obj.hndlBtn.ButtonPushedFcn = @(src, event) smooth(obj);
        end

        % Button callback
        function smooth(obj)
            obj.hndlBtn.Enable = 'off';
            obj.outData = smooth(obj.yData, 30);
            
            obj.hndlPlotResult = line(obj.hndlAxes);
            obj.hndlPlotResult.XData = obj.xData;
            obj.hndlPlotResult.YData = obj.outData;
            obj.hndlPlotResult.Color = [1 0 0];
            obj.hndlPlotResult.LineWidth = 1;
        end        
    end
end