function resiz2dfig

% Function to resize the main window of the mat2dcos toolbox
% last revision: December 20, 2024 (Peter Lasch)
% Copyright Peter Lasch 2000-2025. CC-BY-NC license

% get the size of the current screen
h.scrsz                             = get(0,'ScreenSize');
h.Cen.All                           = round([h.scrsz(3)/2 h.scrsz(4)/2 0 0]);

% get figure and uicontrol handles
h.Figure.CorrMatrix                 = findobj('Type','figure','Tag','CorrMatrixWindow');
h.Axis.Correlation                  = findobj(h.Figure.CorrMatrix,'Type','axes','Tag','AxisCorrelation');
h.Axis.XSpec                        = findobj(h.Figure.CorrMatrix,'Type','axes','Tag','AxisXSpec');
h.Axis.YSpec                        = findobj(h.Figure.CorrMatrix,'Type','axes','Tag','AxisYSpec');
h.Axis.HSpec                        = findobj(h.Figure.CorrMatrix,'Type','axes','Tag','AxisHSpec');
h.Text.ActCursorPos                 = findobj(h.Figure.CorrMatrix,'Style','Text','Tag','ActCursorPos');
h.Text.ActCorrCoeff                 = findobj(h.Figure.CorrMatrix,'Style','Text','Tag','ActCorrCoeff');
h.Text.Xaxis                        = findobj(h.Figure.CorrMatrix,'Style','Text','Tag','TextXaxis');
h.Text.Yaxis                        = findobj(h.Figure.CorrMatrix,'Style','Text','Tag','TextYaxis');

% get the position properties of gui elements
pos.Figure.CorrMatrix               = get(h.Figure.CorrMatrix,'Position');
pos.Axis.Correlation                = get(h.Axis.Correlation,'Position');
pos.Axis.XSpec                      = get(h.Axis.XSpec,'Position'); 
pos.Axis.YSpec                      = get(h.Axis.YSpec,'Position');
pos.Axis.HSpec                      = get(h.Axis.HSpec,'Position');
pos.Text.ActCursorPos               = get(h.Text.ActCursorPos,'Position');
pos.Text.ActCorrCoeff               = get(h.Text.ActCorrCoeff,'Position');
pos.Text.Xaxis                      = get(h.Text.Xaxis,'Position');
pos.Text.Yaxis                      = get(h.Text.Yaxis,'Position');

% Determine the new size of the main mat2dcorr window
mfigwidth                           = pos.Figure.CorrMatrix(3);             % actual width of the main window
mfigheight                          = pos.Figure.CorrMatrix(4);             % actual height of the main window
recenter                            = 0;

%    the height should not be smaller than three times the height of the panel to display the mean x-spectrum (pos.Axis.XSpec(4) = 130) plus
%    the left margin (10) = 400 AND not larger than h.scrsz(4)-100
if mfigwidth < 400
    mfigwidth                       = 400;
    recenter                        = 1;
end
if mfigwidth > h.scrsz(4)-100
    mfigwidth                       = h.scrsz(4)-100;
    recenter                        = 1;
end
%    the width should not be smaller than three times the height of the panel to display the mean x-spectrum (pos.Axis.XSpec(4) = 130) plus
%    the bottom margin (25) = 415 AND not larger than h.scrsz(4)-100
if mfigheight < 415
    mfigheight                      = 415;
    recenter                        = 1;    
end
if mfigheight > h.scrsz(4)-100
    mfigheight                      = h.scrsz(4)-100;
    recenter                        = 1;    
end
pos.Figure.CorrMatrix(3:4)          = [mfigwidth mfigheight];

if recenter == 1
    mfigxorig                       = h.Cen.All(1)-mfigwidth/2;
    mfigyorig                       = h.Cen.All(2)-mfigheight/2-15;
    pos.Figure.CorrMatrix(1:2)      = [mfigxorig mfigyorig];
end

xavrgwidth                          = mfigwidth - 200;
yavrgheight                         = mfigheight - 190;

pos.Axis.Correlation(3:4)           = [xavrgwidth yavrgheight];
pos.Axis.XSpec(2)                   = yavrgheight + 30;
pos.Axis.XSpec(3)                   = xavrgwidth;
pos.Axis.YSpec(4)                   = yavrgheight;
pos.Axis.HSpec(2)                   = yavrgheight + 25;

xtxtcoo                             = 145;                                  % x-coordinate of text field "actual [x,y] coordinates: ..." (pos.Text.ActCursorPos(1))
ytxtcoo                             = yavrgheight + 160;                    % y-coordinate of text field "actual [x,y] coordinates: ..." (pos.Text.ActCursorPos(2))
pos.Text.ActCursorPos(1:2)          = [xtxtcoo ytxtcoo];
xtxtint                             = 145 + pos.Text.ActCursorPos(3) + 10;  % x-coordinate of text field displaying 2D-COS intensities (pos.Text.ActCursorPos(1))
ytxtint                             = ytxtcoo;                              % y-coordinate of text field displaying 2D-COS intensities (pos.Text.ActCursorPos(2))
pos.Text.ActCorrCoeff(1:2)          = [xtxtint ytxtint];
xtxtxax                             = xavrgwidth + 135 - pos.Text.Xaxis(3); % x-coordinate of text field description mean x-spectrum
ytxtxax                             = yavrgheight + 140;                    % y-coordinate of text field description mean x-spectrum 
pos.Text.Xaxis(1:2)                 = [xtxtxax ytxtxax];
xtxtyax                             = 5;                                    % x-coordinate of text field description mean y-spectrum (relative to pos.Axis.HSpec!)
ytxtyax                             = -140;                                 % y-coordinate of text field description mean y-spectrum (relative to pos.Axis.HSpec!) 
pos.Text.Yaxis(1:2)                 = [xtxtyax ytxtyax];

set(h.Axis.Correlation,'Position',pos.Axis.Correlation);
set(h.Axis.XSpec,'Position',pos.Axis.XSpec)
set(h.Axis.YSpec,'Position',pos.Axis.YSpec)
set(h.Axis.HSpec,'Position',pos.Axis.HSpec)
set(h.Text.ActCursorPos,'Position',pos.Text.ActCursorPos)
set(h.Text.ActCorrCoeff,'Position',pos.Text.ActCorrCoeff)
set(h.Text.Xaxis,'Position',pos.Text.Xaxis)
set(h.Text.Yaxis,'Position',pos.Text.Yaxis)
set(h.Figure.CorrMatrix,'Position',pos.Figure.CorrMatrix);