matlab figure最大化 去掉边框白边

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

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W | Z | H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值