classdef GUI2 < matlab.apps.AppBase
    properties
        UIFigure;
        TabGroup;
        FotoapartButtonGroup;
        KalibrovatButton;
        RamButtonGroup;
        Px_mmButt;
        Method; %1 - kalibrace, 2 - px/mm
        Tab1;
        Tab2;
        Tab3;
        PoznTextFotak;
        PoznTextRozmery;
        RefPointsGroup;
        ImageAxes;
        ImageAxes1;
        ImLHPointAx;
        ImPHPointAx;
        ImPDPointAx;
        ImLDPointAx;
        ImLHPoint;
        ImPHPoint;
        ImPDPoint;
        ImLDPoint;
        LHRect;
        PHRect;
        LDRect;
        PDRect;
        LHCirc;
        LDCirc;
        PHCirc;
        PDCirc;
        Angle;
        ControlButtsGroup;
        SelectedPoitsToFix;
        SelectPointBtn;
        Image1;
        ActPictureGroup;
        RotIm;
        PlotTops1;
        Tops1;
        XTops;
        YTops;
        CellOfTops;
        ImCounter;
        SelectFolderBtn;
        ListOfImageNames;
        FileName;
        FileNameText;
        PathToFolder;
        PathToImage;
        StartButt;
        ActCell;
        ActPos;
        ImRead;
        ImFiles;
        TextFotak;
        ImageFotak;
        FotakAxes;
        ImreadDim;
        ImageDim;
        DimAxes;
        ImReadFotak;
        LumixButt;
        AnotherCameraButt;
        IntristicsParams;
        TextDimens;
        ImReadDimes;
        EditWidth;
        EditHeigh;
        TextWidth;
        TextHeigh;
        TextCalibOK;
        Width;
        Height;
        ExportFile;
        OrangePoints;
        FinalExportBtn;
        IndicationText;
        PlochaButtsGroup;
        PlotCurvesGroup;
        ImportFirstCurveBtn;
        ImportSecondCurveBtn;
        PlotCurvesAxes;
        PlotCurve1;
        PlotCurve2;
        CellOfPoints
        SeqOfFirstImages
        SeqOfSecImages
        ComputeAreaBtn
        Legend;
        AreaText;
        AreaEditField;
        PoznTextObsah;

    end
    methods
        
        function startupFcn(app)
            % Configure image axes
           app.Width = app.EditWidth.Value;
           app.Height = app.EditHeigh.Value;
           app.Method = 2;
            %app.ImageAxes1.Visible = 'off';
            app.ImageAxes1.Colormap = gray(256);
            axis(app.ImageAxes1, 'image');
            app.StartButt.Enable = 0;
            app.FinalExportBtn.Enable = 0;
            app.SelectPointBtn.Enable = 0;

         end

         function LumixButtPushed(app,event)
             load('cameraParams_19_4.mat');
             app.IntristicsParams = cameraParams_19_4;
             if~isempty(app.IntristicsParams)
                 app.TextCalibOK.Visible = 1;
                   app.TextCalibOK.FontColor = [0 1 0];
                   app.TextCalibOK.Text = '✔ Zkalibrováno';
                   app.Method = 1;
             end
              if ~isempty(app.IntristicsParams)&&~isempty(app.Width)&&~isempty(app.Height) &&~isempty(app.ExportFile)
                   app.NextTabBtn.Visible = 1;
              end

         end
         
         function AnotherCameraButtPushed(app,event)
            prompt = {'Zadejte délku strany čtverečku šachovnice (mm):'};
            dlgtitle = 'Input';
            dims = [1 35];
            definput = {'19'};
            size = inputdlg(prompt,dlgtitle,dims,definput);
            if ~isempty(size)
                 filterspec = {'*.jpg;*.tif;*.png;*.gif','All Image Files'};
                [f, p] = uigetfile(filterspec,'MultiSelect','on');
                 if (ischar(p))
                    app.LumixButt.Enable = 0;
                    app.AnotherCameraButt.Enable = 0;
                    app.Px_mmButt.Enable = 0;
                    app.UIFigure.Pointer = 'watch';
                    app.TextCalibOK.Text = 'Calibrating...';
                    waitfor(app.TextCalibOK,'Text','Calibrating...');
                    app.TextCalibOK.Visible = 1;
                   try
                   
                    app.IntristicsParams = camCalibrationFunction(f,p,size);
                    app.UIFigure.Pointer = 'arrow';
                    app.TextCalibOK.Text = '';
                    app.LumixButt.Enable = 1;
                    app.AnotherCameraButt.Enable = 1;
                    app.Px_mmButt.Enable = 1;
                   catch
                       app.TextCalibOK.Text = 'Špatně, zkuste znovu.';
                       app.TextCalibOK.FontColor = [1 0 0];
                       app.TextCalibOK.Visible = 1;
                        app.UIFigure.Pointer = 'arrow';
                   end
                   if ~isempty(app.IntristicsParams)
                       app.TextCalibOK.Visible = 1;
                       app.TextCalibOK.FontColor = [0 1 0];
                       app.TextCalibOK.Text = '✔ Zkalibrováno';
                       app.Method = 1;
                   end
                   if ~isempty(app.IntristicsParams)&&~isempty(app.Width)&&~isempty(app.Height) &&~isempty(app.ExportFile)
                       app.NextTabBtn.Visible = 1;
                   end
                  
                 end
            end    
         end

         function SelectFolderBtnPressed(app, event)
             cleanImages(app);
             app.IndicationText.Text = '';  
             app.IndicationText.Text = '';
             try
              [app.CellOfTops,app.PathToFolder,app.ListOfImageNames, hasChanged] = getEmptyCellOfTops(app);
              hasChanged = logical(hasChanged);
             catch
                  string1 = 'Error, zkontolujte zda soubor obsahuje pouze správně';
                  string2 = ' pojmenované fotografie erozního mostu.';
                  app.IndicationText.Text = sprintf('%s\n%s', string1, string2);
                  app.IndicationText.FontSize = 15;
                  app.IndicationText.FontColor = [1 0 0];
                  app.StartButt.Enable = 0;
             end
             if ischar(app.PathToFolder) && ~isempty(app.CellOfTops)  && ~isempty(app.ListOfImageNames) && hasChanged ==1
                 hold(app.ImageAxes1, 'off');
                 cleanImages(app);
                 app.SelectPointBtn.Enable = 0;
                 app.FinalExportBtn.Enable = 0;
                 try
                 im = imread(strcat(app.PathToFolder,'\',app.ListOfImageNames{1}));
                 imshow(im, 'Parent', app.ImageAxes1);
                 app.FileNameText.Text = app.PathToFolder;
                 app.StartButt.Enable = 1;
                 catch
                  string1 = 'Error, zkontolujte, zda soubor obsahuje pouze správně';
                  string2 = ' pojmenované fotografie erozního mostu.';
                  app.IndicationText.Text = sprintf('%s\n%s', string1, string2);
                  app.IndicationText.FontSize = 15;
                  app.IndicationText.FontColor = [1 0 0];

                 end
                 
             end

         end
        
         function updateimage(app,path)
                try
                    app.ImRead = imread(path);
                    hold(app.ImageAxes1, 'off');
                    %app.ImFiles= strcat(p, f);
                    %app.Image1 = imshow(app.ImRead, 'Parent', app.ImageAxes1,'InitialMagnification','fit');  %image creation
                    app.Image1 = imagesc(app.ImageAxes1,app.ImRead);
                    hold(app.ImageAxes1, 'on');
                    drawnow;

                catch ME
                    % If problem reading image, display error message
                    uialert(app.UIFigure, ME.message, 'Image Error');
                    return;
                end                
         end
         
         function PlotTopsButtDwn1(app,event)
             if event.Button == 1 %levý klik = odstraneni bodu
                 Plot = [];
                 Plot(:,1) = round(app.PlotTops1.YData,2);
                 Plot(:,2) = round(app.PlotTops1.XData,2);
                 app.Tops1 = round(app.Tops1,2);
                theTop = [event.IntersectionPoint(1), event.IntersectionPoint(2)]; 
                theTop = flip(theTop);
                theTop = round(theTop, 2);
                app.Tops1(ismember(app.Tops1,theTop,'rows'),:)=[];
                app.CellOfTops{1,app.ActCell}{app.ActPos}(ismember(app.CellOfTops{1,app.ActCell}{app.ActPos},theTop,'rows'),:)=[];
                Plot(ismember(Plot,theTop,'rows'),:)=[];
                app.PlotTops1.YData = Plot(:,1);
                app.PlotTops1.XData = Plot(:,2);
                app.XTops = app.Tops1(:,2);
                app.YTops = app.Tops1(:,1);
                
             end
 
         end

         function StartButtPressed(app,event)
             try
                   app.FileName = char(regexp(app.ListOfImageNames{1},'\d*','Match'));
                   [status, msg, msgID] = mkdir(strcat(app.PathToFolder,'-dxf'));
                   app.IndicationText.FontSize = 30;
                   app.IndicationText.FontColor = [0 0 0];
                     app.IndicationText.Text = 'Processing...';
                     app.UIFigure.Pointer = 'watch';
                     enableWhileComputing(app,0);
                     app.PathToImage = char(strcat(app.PathToFolder,'\',app.ListOfImageNames{1}));
                       app.FileNameText.Text = app.PathToImage;
                        updateimage(app,app.PathToImage); 
                        [Tops, im2,app.RotIm,LD,PH,LH,PD,app.Angle] = ImageProcessing(app,app.PathToImage);
                        showOrangePointsImages(app,app.RotIm,LD,PH,LH,PD);
                        app.Tops1 = Tops;
                        app.XTops = Tops(:,2);
                        app.YTops = Tops(:,1);
                        [app.ActCell, app.ActPos] = findCellForTopsByName(app.ListOfImageNames{1});
                        app.CellOfTops{1,app.ActCell}{app.ActPos} = Tops;
                        app.Image1.CData = im2;
                        app.Image1 = imshow(im2, 'Parent', app.ImageAxes1);  %image creation
                        hold(app.ImageAxes1, 'on');
                        app.PlotTops1= plot(app.ImageAxes1,app.Tops1(:,2),app.Tops1(:,1), 'r+', 'MarkerSize', 10, 'LineWidth', 1);
                        app.PlotTops1.ButtonDownFcn = @(txt,event)PlotTopsButtDwn1(app,event);
                        app.Image1.ButtonDownFcn = @(txt,event)ImageButtonDown1(app,event);
                        enableWhileComputing(app,1);
                        app.IndicationText.Text = '';
                        app.UIFigure.Pointer = 'arrow';
             catch
                 app.ListOfImageNames = app.ListOfImageNames(2:end);
                 StartButtPressed(app);
             end
             
         end

         function ImLHPointBtnDwn(app,event)
             if ismember('LH', app.SelectedPoitsToFix{1,1})
                 app.SelectedPoitsToFix{1,1}(ismember(app.SelectedPoitsToFix{1,1},'LH')) = [];
                 app.LHRect.Visible = 0;
                 
             else
                app.SelectedPoitsToFix{1}{end+1} = 'LH';
                app.LHRect.Visible = 1;
             end
             if isempty(app.SelectedPoitsToFix{1,1})
                 app.SelectPointBtn.Enable = 0;
             else
                 app.SelectPointBtn.Enable = 1;
             end
         end

         function ImLDPointBtnDwn(app,event)
             if ismember('LD', app.SelectedPoitsToFix{1,1})
                 app.SelectedPoitsToFix{1,1}(ismember(app.SelectedPoitsToFix{1,1},'LD')) = [];
                 app.LDRect.Visible = 0;
                 
             else
                app.SelectedPoitsToFix{1}{end+1} = 'LD';
                app.LDRect.Visible = 1;
             end
             if isempty(app.SelectedPoitsToFix{1,1})
                 app.SelectPointBtn.Enable = 0;
             else
                 app.SelectPointBtn.Enable = 1;
             end

         end

         function ImPHPointBtnDwn(app,event)
             if ismember('PH', app.SelectedPoitsToFix{1,1})
                 app.SelectedPoitsToFix{1,1}(ismember(app.SelectedPoitsToFix{1,1},'PH')) = [];
                 app.PHRect.Visible = 0;
                 
             else
                app.SelectedPoitsToFix{1}{end+1} = 'PH';
                app.PHRect.Visible = 1;
             end

             if isempty(app.SelectedPoitsToFix{1,1})
                 app.SelectPointBtn.Enable = 0;
             else
                 app.SelectPointBtn.Enable = 1;
             end

         end


         function ImPDPointBtnDwn(app,event)
             if ismember('PD', app.SelectedPoitsToFix{1,1})
                 app.SelectedPoitsToFix{1,1}(ismember(app.SelectedPoitsToFix{1,1},'PD')) = [];
                 app.PDRect.Visible = 0;
                 
             else
                app.SelectedPoitsToFix{1}{end+1} = 'PD';
                app.PDRect.Visible = 1;
             end

             if isempty(app.SelectedPoitsToFix{1,1})
                 app.SelectPointBtn.Enable = 0;
             else
                 app.SelectPointBtn.Enable = 1;
             end

         end

         function SelectPoitnBtnPressed(app)
                enableWhileComputing(app,0);
                app.UIFigure.Pointer = 'watch';
                app.IndicationText.Text = 'Please wait...';
                cleanImages(app);
                updateimage(app,app.PathToImage);   
                app.IndicationText.Text = 'Processing...';
                [Tops, im2,app.RotIm,LD,PH,LH,PD] = ImageProcessing2(app,app.RotIm);
                showOrangePointsImages(app,app.RotIm,LD,PH,LH,PD);
                app.Tops1 = Tops;
                app.XTops = Tops(:,2);
                app.YTops = Tops(:,1);
                app.CellOfTops{app.ActCell}{app.ActPos} = Tops;
                app.Image1.CData = im2;
                app.Image1 = imshow(im2, 'Parent', app.ImageAxes1);  %image creation
                hold(app.ImageAxes1, 'on');
                app.PlotTops1= plot(app.ImageAxes1,app.Tops1(:,2),app.Tops1(:,1), 'r+', 'MarkerSize', 10, 'LineWidth', 1);
                app.PlotTops1.ButtonDownFcn = @(txt,event)PlotTopsButtDwn1(app,event);
                app.Image1.ButtonDownFcn = @(txt,event)ImageButtonDown1(app,event); 
                enableWhileComputing(app,1);
                app.IndicationText.Text = '';
                app.UIFigure.Pointer = 'arrow';

         end

         function ImageButtonDown1(app,event)
             if event.Button == 3 %pravý klik = přidáváme bod
                temp = app.ImageAxes1.CurrentPoint; % Returns 2x3 array of points
                loc = [temp(1,1) temp(1,2)];
                app.PlotTops1.XData(end+1) = loc(1);
                app.PlotTops1.YData(end+1) = loc(2);
                app.Tops1(end+1,2) = loc(1);
                app.Tops1(end,1) = loc(2);
                app.XTops = app.Tops1(:,2);
                app.YTops = app.Tops1(:,1);
                app.CellOfTops{1,app.ActCell}{app.ActPos}(end+1,2) = loc(1);
                app.CellOfTops{1,app.ActCell}{app.ActPos}(end,1) = loc(2);
                

             end
             
         end

          function widthChanged(app,event)
              app.Width = event.Value;
              if ~isempty(app.IntristicsParams)&&~isempty(app.Width)&&~isempty(app.Height) &&~isempty(app.ExportFile)
                   app.NextTabBtn.Visible = 1;
               end
          end
          
         
          function heighChanged(app,event)
              app.Height = event.Value;
              if ~isempty(app.IntristicsParams)&&~isempty(app.Width)&&~isempty(app.Height) &&~isempty(app.ExportFile)
                   app.NextTabBtn.Visible = 1;
               end
          end
          
          function Px_mmButtPushed(app,event)
              app.Method = 2;
              app.TextCalibOK.Text = '✔ V pořádku';
              app.TextCalibOK.FontColor = [0 1 0];
          end

          function FinalExportBtnPressed(app,event)
                app.IndicationText.Text = '';
                idxOfDot = strfind(app.ListOfImageNames{1},'.');
                fileName = app.ListOfImageNames{1}(1:idxOfDot-1);
                app.FileName = strcat(fileName, '.dxf');
                fid = fopen(fullfile(strcat(app.PathToFolder,'-dxf'),app.FileName),'wt');
                fclose(fid);
                FID = dxf_open(strcat(strcat(app.PathToFolder,'-dxf'),'\',app.FileName));  
                Tops = [app.XTops,app.YTops];
                Tops = sortrows(Tops,1);
                LH = app.OrangePoints(3,:);
                PH =  app.OrangePoints(2,:);
                Tops(:,1) = Tops(:,1)+(LH(1)+20);
                Tops(:,2) = Tops(:,2)+(LH(2)-50);
               Tops = [app.OrangePoints(3,:); Tops; app.OrangePoints(2,:)];
                if app.Method == 1
                    Tops = points2WorldGUI(app,app.OrangePoints,app.Width, app.Height, Tops);
                   % Tops = [app.OrangePoints(3,:); Tops; app.OrangePoints(2,:)];
                else
                    if app.Width ~=0
                    ratio = (sqrt((PH(1)-LH(1))^2+(PH(2)-LH(2))^2))/app.Width; %px/mm
                     Tops = Tops/ratio;
                    else
                        app.IndicationText.FontSize = 20;
                        app.IndicationText.FontColor = [1 0 0];
                        app.IndicationText.Text = 'Zadejte rozměry rámu';
                        return;
                    end
                end
               Tops = Tops/1000;
                Tops(:,1) = Tops(:,1) - Tops(1,1);
                Tops(:,2) = Tops(:,2) - Tops(1,2);
               % Tops = rotatePoints(Tops,app.Angle);
                FID = dxf_polyline(FID,Tops(:,1),0-Tops(:,2),zeros(length(Tops),1));
                dxf_close(FID);
                app.StartButt.Enable = 0;
                app.FinalExportBtn.Enable = 0;        
                app.SelectPointBtn.Enable = 0;
                 hold(app.ImageAxes1, 'off');
                cleanImages(app);
                if length(app.ListOfImageNames)>=2
                    app.ListOfImageNames = app.ListOfImageNames(2:end);
                    StartButtPressed(app);
                else
                    app.ListOfImageNames = [];
                    app.IndicationText.Text = 'Done';
                end

          end

          function ImportFirstCurveBtnPressed(app,event)
             hold(app.PlotCurvesAxes, 'off');
             app.PlotCurve1 = [];
             app.PlotCurve2 = [];
             filename = '.dxf';
             [file,path] = uigetfile(filename);
             if file ~=0
                 app.ImportFirstCurveBtn.Enable = 0;
                 app.ImportSecondCurveBtn.Enable = 0;
                 app.UIFigure.Pointer = 'watch';
                 
                 [x,y] = getPtsFromPolyline(file, path);
                x = x*1000;
                 y = y*1000;
                  %[x,y] = removeSameX(x,y);
               
                app.PlotCurve1 = plot(app.PlotCurvesAxes, x, y,"Color",'b');
                hold(app.PlotCurvesAxes);
                legend(app.PlotCurvesAxes,'off');
                app.ImportFirstCurveBtn.Enable = 1;
                 app.ImportSecondCurveBtn.Enable = 1;
                 app.UIFigure.Pointer = 'arrow';
             end
             
          end

          function ImportSecondCurveBtnPressed(app,event)
            hold(app.PlotCurvesAxes,"off");
               app.PlotCurve2 = [];
             filename = '.dxf';
             [file,path] = uigetfile(filename);
             if file ~=0
                 app.ImportFirstCurveBtn.Enable = 0;
                 app.ImportSecondCurveBtn.Enable = 0;
                 app.UIFigure.Pointer = 'watch';

                  [x2,y2] = getPtsFromPolyline(file, path);
                 % [x2,y2] = removeSameX(x2,y2);   
                x2 = x2*1000;
                 y2 = y2*1000;
%                
                [area, x1,y1, x2, y2] = computeArea(app.PlotCurve1.XData,app.PlotCurve1.YData,x2,y2);
                area = area/100; %na cm2
                app.PlotCurve1 = plot(app.PlotCurvesAxes, x1, y1,"Color",'b');
                hold(app.PlotCurvesAxes,'on');
                grid(app.PlotCurvesAxes,'on');
                app.PlotCurve2 = plot(app.PlotCurvesAxes, x2, y2, 'Color','r');
                legend(app.PlotCurvesAxes,{'1. křivka','2. křivka'})
                hold(app.PlotCurvesAxes,'on');
                area = num2str(area);
               % area = round(area);
                
%                 app.AreaText.Text = strcat('S = ', num2str(area), ' mm^2');
%                 app.AreaText.Visible = 1;
                app.AreaEditField.Value = area ;
                 app.ImportFirstCurveBtn.Enable = 1;
                 app.ImportSecondCurveBtn.Enable = 1;
                 app.UIFigure.Pointer = 'arrow';
             end

          end

    end
    
    methods
         function createComponents(app)
           app.UIFigure = uifigure('Visible', 'on');
            app.UIFigure.AutoResizeChildren = 'off';
            app.UIFigure.Units = 'pixels';
            app.UIFigure.Position = [10 60 1350 650];
           
            app.UIFigure.Name = 'Erozni most';
            app.UIFigure.Resize = 'off';

            app.TabGroup = uitabgroup(app.UIFigure);
            app.TabGroup.Units = 'pixels';
            app.TabGroup.Position = [0 0 1350 650];

            app.Tab1 = uitab(app.TabGroup);
            app.Tab1.Title = 'Kalibrace';

            app.FotoapartButtonGroup = uibuttongroup(app.Tab1);
            app.FotoapartButtonGroup.Title = 'Metoda vyhodnocení';
            app.FotoapartButtonGroup.Position = [50 400 600 200];

            app.LumixButt = uibutton(app.FotoapartButtonGroup,'Text','Panasonic Lumix','Position',[50 130 150 30], ...
            'ButtonPushedFcn',{@(btn,event) LumixButtPushed(app,event)});

            app.AnotherCameraButt = uibutton(app.FotoapartButtonGroup,'Text','Jiný','Position',[50 80 150 30], ...
            'ButtonPushedFcn',{@(btn,event) AnotherCameraButtPushed(app,event)});

             app.Px_mmButt = uibutton(app.FotoapartButtonGroup,'Text','px/mm','Position',[50 30 150 30], ...
            'ButtonPushedFcn',{@(btn,event) Px_mmButtPushed(app,event)});

            app.TextCalibOK = uilabel(app.FotoapartButtonGroup);
            app.TextCalibOK.HorizontalAlignment = 'left';
            app.TextCalibOK.FontSize = 20;
            app.TextCalibOK.Position = [300 90 150 30];
            app.TextCalibOK.Text = '';
            app.TextCalibOK.Visible = 1;


            app.PoznTextFotak = uitextarea(app.Tab1);
            app.PoznTextFotak.Position = [50 310 600 90];
            app.PoznTextFotak.Value = ['Vybráním možnosti Panasonic Lumix bude při vyhodnocování použita metoda zahrnující vnitřní parametry tohoto fotoaparátu. Při fotografování mějte zapnutý AF-Lock. Pokud chcete použít jiný fotoaparát, musíte pomocí něho vyfotit alespoň 15 fotografií šachovnice z různých pohledů. ' ...
             'Nejprve zadáte délku strany čtverce, poté vyberete fotografie šachovnice ze složky. Následně se provede automatická kalibrace a metoda bude použita stejná, jako v předchozím případě. Při použití metody px/mm nezálěží na používaném fotoaparátu a způsob vyhodnocování je stejný, jako při ručním zpracování.'];
            app.PoznTextFotak.Editable = 0;

            app.RamButtonGroup = uibuttongroup(app.Tab1);
            app.RamButtonGroup.Title = 'Rám';
            app.RamButtonGroup.Position = [700 400 600 200];

           app.ImageDim = uiimage(app.Tab1);
           app.ImageDim.Position = [700 20 600 400];
           app.ImageDim.ImageSource = 'ramDim.PNG';

            app.TextHeigh = uilabel(app.RamButtonGroup);
            app.TextHeigh.HorizontalAlignment = 'left';
            app.TextHeigh.FontSize = 15;
            app.TextHeigh.Position = [100 100 150 30];
            app.TextHeigh.Text = 'výška (mm)';

            app.EditHeigh = uieditfield(app.RamButtonGroup,'numeric');
            app.EditHeigh.Position = [300 100 150 30];
            app.EditHeigh.ValueChangedFcn = @(txt,event)heighChanged(app,event);
            app.EditHeigh.Value = 758;

            app.TextWidth = uilabel(app.RamButtonGroup);
            app.TextWidth.HorizontalAlignment = 'left';
            app.TextWidth.FontSize = 15;
            app.TextWidth.Position = [100 50 150 30];
            app.TextWidth.Text = 'šířka (mm)';

            app.EditWidth = uieditfield(app.RamButtonGroup,'numeric');
            app.EditWidth.Position = [300 50 150 30];
            app.EditWidth.ValueChangedFcn = @(txt,event) widthChanged(app,event);
            app.EditWidth.Value = 1050;

            app.Tab2 = uitab(app.TabGroup);
            app.Tab2.Title = 'Zpracovani';

            app.SelectedPoitsToFix = cell(1);

            app.RefPointsGroup = uibuttongroup(app.Tab2);
            app.RefPointsGroup.Title = 'Referenční body';
            app.RefPointsGroup.Position = [1000 230 340 380];

            app.ImLHPointAx = uiaxes(app.RefPointsGroup);
            app.ImLHPointAx.XTick = [];
            app.ImLHPointAx.XTickLabel = {'[ ]'};
            app.ImLHPointAx.YTick = [];
            app.ImLHPointAx.Position = [10 200 150 150];
            app.ImLHPointAx.Toolbar.Visible = 0;

            app.ImPHPointAx = uiaxes(app.RefPointsGroup);
            app.ImPHPointAx.XTick = [];
            app.ImPHPointAx.XTickLabel = {'[ ]'};
            app.ImPHPointAx.YTick = [];
            app.ImPHPointAx.Position = [170 200 150 150];
            app.ImPHPointAx.Toolbar.Visible = 0;

            app.ImLDPointAx = uiaxes(app.RefPointsGroup);
            app.ImLDPointAx.XTick = [];
            app.ImLDPointAx.XTickLabel = {'[ ]'};
            app.ImLDPointAx.YTick = [];
            app.ImLDPointAx.Position = [10 50 150 150];
            app.ImLDPointAx.Toolbar.Visible = 0;

            app.ImPDPointAx = uiaxes(app.RefPointsGroup);
            app.ImPDPointAx.XTick = [];
            app.ImPDPointAx.XTickLabel = {'[ ]'};
            app.ImPDPointAx.YTick = [];
            app.ImPDPointAx.Position = [170 50 150 150];
            app.ImPDPointAx.Toolbar.Visible = 0;

             app.SelectPointBtn = uibutton(app.RefPointsGroup,'Text','Vybrat chybné body','Position',[80 20 200 22], ...
            'ButtonPushedFcn',{@(btn,event) SelectPoitnBtnPressed(app)});

            app.ControlButtsGroup = uibuttongroup(app.Tab2);
            app.ControlButtsGroup.Title = '';
            app.ControlButtsGroup.Position = [1000 10 340 200];
            
            app.SelectFolderBtn = uibutton(app.ControlButtsGroup, 'push');
            app.SelectFolderBtn.ButtonPushedFcn = createCallbackFcn(app, @SelectFolderBtnPressed, true);
            app.SelectFolderBtn.Position = [80 130 150 30];
            app.SelectFolderBtn.Text = 'Vybrat složku';
            
             app.StartButt = uibutton(app.ControlButtsGroup,'Text','Start','Position',[80 80 150 30], ...
            'ButtonPushedFcn',{@(btn,event) StartButtPressed(app,event)});
        
            app.FinalExportBtn= uibutton(app.ControlButtsGroup,'Text','Next/Export','Position',[80 30 150 30], ...
            'ButtonPushedFcn',{@(btn,event)FinalExportBtnPressed(app,event)});

            app.ActPictureGroup = uibuttongroup(app.Tab2);
            app.ActPictureGroup.Title = 'Aktuální fotografie';
            app.ActPictureGroup.Position = [10 10 970 600];

            app.ImageAxes1 = uiaxes(app.ActPictureGroup);
            app.ImageAxes1.XTick = [];
            app.ImageAxes1.XTickLabel = {'[ ]'};
            app.ImageAxes1.YTick = [];
            app.ImageAxes1.Units = 'pixels';
            app.ImageAxes1.Position = [10 0 920 600];

            app.FileNameText = uilabel(app.ActPictureGroup);
            app.FileNameText.HorizontalAlignment = 'left';
            app.FileNameText.FontSize = 12;
            app.FileNameText.Position = [10 545 1200 50];
            app.FileNameText.Text = '';
            
            app.IndicationText = uilabel(app.ActPictureGroup);
            app.IndicationText.HorizontalAlignment = 'left';
            app.IndicationText.FontSize = 30;
            app.IndicationText.Position = [400 0 500 50];
            app.IndicationText.Text = '';

            app.Tab3 = uitab(app.TabGroup);
            app.Tab3.Title = 'Vypocet plochy';

            app.PlochaButtsGroup = uibuttongroup(app.Tab3);
            app.PlochaButtsGroup.Title = '';
            app.PlochaButtsGroup.Position = [10 230 370 380];

            app.ImportFirstCurveBtn= uibutton(app.PlochaButtsGroup,'Text','Nahrát první křivku','Position',[50 300 250 30], ...
            'ButtonPushedFcn',{@(btn,event)ImportFirstCurveBtnPressed(app,event)});

             app.ImportSecondCurveBtn= uibutton(app.PlochaButtsGroup,'Text','Nahrát druhou křivku','Position',[50 260 250 30], ...
            'ButtonPushedFcn',{@(btn,event)ImportSecondCurveBtnPressed(app,event)});

              
            app.AreaEditField = uitextarea(app.PlochaButtsGroup);
            app.AreaEditField.Position = [130 200 150 30];
            app.AreaEditField.Editable = 0;

%             app.AreaEditField = uieditfield(app.PlochaButtsGroup,'numeric');
%             app.AreaEditField.Position = [130 200 150 30];

            app.AreaText = uilabel(app.PlochaButtsGroup);
            app.AreaText.HorizontalAlignment = 'left';
            app.AreaText.FontSize = 15;
            app.AreaText.Position = [10 200 120 30];
            app.AreaText.Text = 'S [cm^2]';
            
            app.PlotCurvesGroup = uibuttongroup(app.Tab3);
            app.PlotCurvesGroup.Title = 'Porovnání křivek';
            app.PlotCurvesGroup.Position = [400 10 930 600];
             
            app.PlotCurvesAxes = uiaxes(app.PlotCurvesGroup);
            app.PlotCurvesAxes.XTickMode = 'auto';
            app.PlotCurvesAxes.XTickLabelMode = 'auto';
            app.PlotCurvesAxes.YTickMode = 'auto';
            app.PlotCurvesAxes.YTickLabelMode = 'auto';
            app.PlotCurvesAxes.Position = [60 200 800 300];
            app.PlotCurvesAxes.XGrid = 'on';
            app.PlotCurvesAxes.YGrid = 'on';
            app.PlotCurvesAxes.XLabel.String = 'x [mm]';
            app.PlotCurvesAxes.YLabel.String = 'y [mm]';

            app.PoznTextObsah = uilabel(app.PlotCurvesGroup);
            app.PoznTextObsah.HorizontalAlignment = 'left';
            app.PoznTextObsah.FontSize = 14;
            app.PoznTextObsah.Position = [60 150 600 30];
            app.PoznTextObsah.Text = 'Pozn.: S = (plocha pod 1. křivkou - plocha pod 2. křivkou)';
           

         end
    end
    
    methods
         function app = GUI2
              createComponents(app);
               
               registerApp(app, app.UIFigure);
               runStartupFcn(app, @startupFcn);

            % Execute the startup function
            %runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
         end
    end
end