Matlab平铺所有打开的绘图窗口
直接上代码:
这是跟ChatGPT battle了多轮之后的结果(但是还得手动改掉一部分内容)
1function arrangefigs
2drawnow;
3% Get screen size
4screenSize = get(0, 'ScreenSize');
5screenWidth = screenSize(3);
6screenHeight = screenSize(4);
7
8% Get handles to all open figures
9figures = flip(findobj('Type', 'figure'));
10numFigures = length(figures);
11
12% Retrieve original sizes of the figures
13originalSizes = zeros(numFigures, 4);
14
15for i = 1:numFigures
16 originalSizes(i, :) = get(figures(i), 'OuterPosition');
17end
18
19% Calculate the number of columns and rows based on the number of figures
20averageFigureWidth = mean(originalSizes(:, 3));
21averageFigureHeight = mean(originalSizes(:, 4));
22numRows = max(1, floor(screenHeight / averageFigureHeight));
23numColumns = max(1, ceil(numFigures / numRows));
24
25if (numColumns * averageFigureWidth > screenWidth)
26 numRows = numRows + 1;
27 numColumns = max(1, ceil(numFigures / numRows));
28end
29
30% Calculate spacing between figures
31spacingX = max((screenWidth - numColumns * averageFigureWidth) / (numColumns + 1), 0);
32spacingY = max((screenHeight - numRows * averageFigureHeight) / (numRows + 1), 0);
33
34% Set initial position
35x = spacingX; % initial x-coordinate
36y = screenHeight - spacingY - averageFigureHeight; % initial y-coordinate
37
38% Iterate through figures and move each figure to the grid position
39for i = 1:numFigures
40 % Retrieve original size of the current figure
41 originalSize = originalSizes(i, :);
42
43 % Customize the position of each figure
44 set(figures(i), 'OuterPosition', [x, y, originalSize(3), originalSize(4)]);
45
46 % Bring the figure to the front
47 figure(figures(i));
48
49 % Update position for the next figure
50 x = x + min(originalSize(3) + spacingX, (screenWidth - averageFigureWidth) / (numColumns - 1));
51
52 % Check if the next column needs to start in a new row
53 if mod(i, numColumns) == 0
54 x = spacingX;
55 y = y - min(spacingY + averageFigureHeight, screenHeight / numRows);
56 end
57end
58end