function fig = max_fig(fig,sub_row,sub_col)
% figure最大化,边框靠边
% sub_row : subplot的行数
% sub_col : subplot的列数
if nargin < 1
fig = gcf;
if isempty(fig)
fig = figure;
end
end
set(fig, 'WindowState', 'maximized');
set(fig, 'PaperPositionMode', 'auto');
drawnow();
ax = fig.Children;
if isempty(ax)
ax = axes;
end
real_ax = [];
for i = length(ax):-1:1
if contains(class(ax(i)),'Axes')
real_ax = [real_ax; ax(i)]; %#ok<AGROW>
end
end
if length(real_ax) > 1
for current_row = 1:sub_row
for current_col = 1:sub_col
ind = (current_row-1)*sub_col+current_col; % 子图的顺序
% 设置OuterPosition
sub_axes_x = current_col*1/sub_col - 1/sub_col;
sub_axes_y = 1-current_row*1/sub_row; % y是从上往下的
sub_axes_w = 1/sub_col;
sub_axes_h = 1/sub_row;
set(real_ax(ind), 'OuterPosition', [sub_axes_x, sub_axes_y, sub_axes_w, sub_axes_h]); % 重设OuterPosition
% TightInset的位置
inset_vectior = get(real_ax(ind), 'TightInset');
inset_x = inset_vectior(1);
inset_y = inset_vectior(2);
inset_w = inset_vectior(3);
inset_h = inset_vectior(4);
% OuterPosition的位置
outer_vector = get(real_ax(ind), 'OuterPosition');
pos_new_x = outer_vector(1) + inset_x; % 将Position的原点移到到TightInset的原点
pos_new_y = outer_vector(2) + inset_y;
pos_new_w = outer_vector(3) - inset_w - inset_x; % 重设Position的宽
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重设Position的高
% 重设Position
set(real_ax(ind), 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);
end
end
else
set(real_ax,'LooseInset', max(get(real_ax,'TightInset'), 0.02));
end