I'm trying to change the figure size. In the example below, I expected figure(2) to have a different size. In order to achieve this, I added:
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperSize', [4 2]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 4 2]);
Any idea why this isn't working?
clc; clear all;
t = 0:.1:4*pi; y = sin(t);
figure(1)
set(gcf, 'renderer', 'painters');
plot(t,y)
xlabel('Time(s)') ylabel('y(t)')
title('Sin function')
legend('y=sin(t)')
axis([0 t(end) -1.5 1.5])
set(gca,...
'Units','normalized',...
'YTick',-1.5:.5:1.5,...
'XTick',0:t(end)/4:t(end),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
figure(2)
set(gcf, 'renderer', 'painters');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperSize', [4 2]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 4 2]);
plot(t,y)
xlabel('Time(s)')
ylabel('y(t)')
title('Sin function')
legend('y=sin(t)')
axis([0 t(end) -1.5 1.5])
set(gca,...
'Units','normalized',...
'YTick',-1.5:.5:1.5,...
'XTick',0:t(end)/4:t(end),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);

 채택된 답변

Chad Greene
Chad Greene 2025년 11월 10일
편집: MathWorks Support Team 2025년 11월 10일

38 개 추천

The paper size options are for printing, so they don’t change the size of the figure.
The ‘Position’ property sets the size of the figure (in pixels by default). Specify the property as a vector of the form [x y width height], where x and y define the distance from the lower-left corner of the screen to the lower-left corner of the figure. Also note that you can set several properties at once without calling set(gcf,… multiple times. You can even include them when you create the figure:
 
figure('Renderer', 'painters', 'Position', [10 10 900 600])
You can also save a handle to your figure and set the Position property using dot notation:
f = figure;
f.Position = [100 100 540 400];
For an example of changing figure size programmatically, see this example in the documentation.
For more information on figure properties, refer to the following: https://www.mathworks.com/help/matlab/ref/matlab.ui.figure.html

댓글 수: 6

John
John 2015년 1월 30일
I related the figure size to paper because somehow printing seems to work better than exporting and, by doing this, I can choose the size of the output without having to scale afterwards (scaling would mess up the legend size).
But your comment made me think more about this and I noticed the following:
1-) The paper size change only affects the size of the output file (the one I'd get by print(gcf, '-dpdf', 'my-figure.pdf')) and not the output figure you see on the screen after running the code.
2-) It does not affect the output file if instead of "print" I use some other scripts, like "plot2svg". This is what I was trying to use and so I could not get the result I intended.
So, since I'll keep using "plot2svg", I'd better do something like the following:
set(gcf,'Units','inches',...
'Position',[0 0 4 2])
Like you mentioned, the first two coordinates refer to (x0,y0). I don't wanna change the default value for this; do you know how I can get them?
Initialize figure with units of inches:
figure('units','inches')
Get coordinates of figure:
pos = get(gcf,'pos');
Adjust width and height of figure:
set(gcf,'pos',[pos(1) pos(2) 4 2])
John
John 2015년 1월 31일
Thanks.
Ebrahim Akbari
Ebrahim Akbari 2017년 10월 22일
Thank you dear Chad. It was useful for me.
To maximize the figure window in Windows, you can use the attached function.
Otherwise you can also use code like this, to take up most of the screen except for the task bar at the bottom.
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
The latter code just resizes - it does not do an official "maximization" like the attached function does.
Steven Lord
Steven Lord 2020년 2월 20일
This is a bit late, but if you're using release R2018a or later you can use the WindowState property of figure objects to maximize or minimize the figure or display it in full-screen mode instead of using the function Image Analyst attached to the comment above.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

9 개 추천

First, there seems to be some confusion as to what your refer by size, which can be a) on the screen or b) printed or c) in pixel size (as in a jpg).
If you want to have the two figures in the screen with identical sizes, then the important parameter is 'Position' OF THE FIGURE, i.e.
figure(1)
plot(t,y)
set(gcf,'Position',[100 100 500 500])
figure(2)
plot(t2,y2)
set(gcf,'Position',[100 100 500 500])
That will have the two figures with exactly the same size, and in the same position. You can change where each is placed and the dimensions. Actually, it is better to use handles than gcf as gcf uses the last figure that was addressed, example
h1=figure(1);
plot(t,y)
set(h1,'Position',[10 10 500 500])
h2=figure(2);
plot(t2,y2)
set(h2,'Position',[510 10 500 500])
Notice that gcf refers to figures, if you change with gca, you will be moving the axis INSIDE the figure but the figure size will not change.
If you want to change in print or in an external image let me know and I will expand.

댓글 수: 2

Jan Cagan
Jan Cagan 2018년 7월 23일
Dear Constantino,
Thank you for the detailed answer. As regards "Printing and Exporting" properties of a figure, I am dealing with this properties correctly I think. Currently, I am observing that the problem is not about exporting. Also, other graphical objects have problems with size consistency. For example, "msgbox" with the same text has different size sometimes...
J.
Henri Skinner
Henri Skinner 2020년 9월 9일
This was very helpful for my problem, thanks Constantino!

댓글을 달려면 로그인하십시오.

Chad Greene
Chad Greene 2015년 1월 30일

8 개 추천

For full-screen figures, you can use fullfig.
Jan Cagan
Jan Cagan 2018년 6월 5일

3 개 추천

Hi,
I am using this figure initialization and export:
fig = figure('units','inch','position',[0,0,3.3,2*3.3/3]);
print(fig,name,'-r800','-dpng');
In most cases, the output has the same size, but sometimes the size is a bit different. Why? How to avoid this behavior? I am using Matlab in Ubuntu 16.04 with -nodesktop -softwareopengl
Thank you in advance. J.

댓글 수: 1

Ethan Duckworth
Ethan Duckworth 2022년 10월 10일
It's hard to know without seeing your examples why they come out slightly different. But I would guess it's the labels and/or numbers on the axes.

댓글을 달려면 로그인하십시오.

karim botros
karim botros 2023년 6월 12일

0 개 추천

It is simple in matlab, this feature is integrated in position property of Figure function.
All you have to do is to replace the values of startingX startingY Width Height, like the following:
figure('Position',[startingX startingY Width Height]);
% Example with numerical values.
figure('Position',[600 100 1500 1000]);
It is a bit not very intuitive in matlab since they combine size and position in one property but now you know to set them both.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 1월 30일

편집:

2025년 11월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by