↓

Matlab平铺所有打开的绘图窗口

··

直接上代码:

这是跟ChatGPT battle了多轮之后的结果(但是还得手动改掉一部分内容)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function arrangefigs(yUseRatio, mode)

% arrangefigs - 自动排列屏幕上的绘图窗口
% 输入:
%   yUseRatio - 屏幕Y方向占据的比例,默认为0.95
%   mode - 指定为1时,横向排列窗口
%

% Get screen size
screenSize = get(0, 'ScreenSize');
screenWidth = screenSize(3);
screenHeight = screenSize(4);

if nargin < 2
    mode = 0;
end

if nargin < 1
    yUseRatio = 0.95;
else
    assert(yUseRatio > 0 && yUseRatio <= 1, "yUseRatio需要在(0, 1]范围内")
end

% Get handles to all open figures
figures = flip(findobj('Type', 'figure'));
numFigures = length(figures);

% Retrieve original sizes of the figures
originalSizes = zeros(numFigures, 4);

for i = 1:numFigures
    originalSizes(i, :) = get(figures(i), 'OuterPosition');
end

% Calculate the number of columns and rows based on the number of figures
averageFigureWidth = mean(originalSizes(:, 3));
averageFigureHeight = mean(originalSizes(:, 4));
if mode == 1
    numRows = 1;
else
    numRows = max(1, floor(screenHeight * yUseRatio / averageFigureHeight));
end
numColumns = max(1, ceil(numFigures / numRows));

% if (numColumns * averageFigureWidth > screenWidth)
%     numRows = numRows + 1;
%     numColumns = max(1, ceil(numFigures / numRows));
% end

% Calculate spacing between figures
spacingX = max((screenWidth - numColumns * averageFigureWidth) / (numColumns + 1), 0);
spacingY = max((screenHeight * yUseRatio - numRows * averageFigureHeight) / (numRows + 1), 0);

% Set initial position
x = spacingX;  % initial x-coordinate
y = screenHeight - spacingY - averageFigureHeight;  % initial y-coordinate

% Iterate through figures and move each figure to the grid position
for i = 1:numFigures
    % Retrieve original size of the current figure
    originalSize = originalSizes(i, :);
    
    % Customize the position of each figure
    set(figures(i), 'OuterPosition', [x, y, originalSize(3), originalSize(4)]);

    % Bring the figure to the front
    figure(figures(i));

    % Update position for the next figure
    x = x + min(originalSize(3) + spacingX, (screenWidth - averageFigureWidth) / (numColumns - 1));
    
    % Check if the next column needs to start in a new row
    if mod(i, numColumns) == 0
        x = spacingX;
        y = y - min(spacingY + averageFigureHeight, screenHeight / numRows);
    end
end
end