Exporting figure maintaining given pixel size

I want to save my figure in tiff format with given dimensions i.e 2464 * 2056 pixels.
However, when I use saveas(gcf,['locationr' num2str(j) '.tif']); I get this error ie
Error using print (line 83):
Unable to create output using specified size and resolution. Specify a smaller value for the PaperPosition
property of the figure or specify a smaller resolution value.
My code is as follows:
figure;
xlim([0 2500]);
ylim([0 2056]);
set(gca,'YDir','reverse');
set(gca,'Units','normalized','Position',[0 0 1 1]);
rez = [r c]; %set desired [horizontal vertical] resolution
set(gcf,'PaperPosition',[0 0 rez]);
hold on; x1=[]; y1=[]; x2=[]; y2=[]; u=[]; v=[];
x1=D4(:,1)
y1=D4(:,2)
x2=D4(:,3)
y2=D4(:,4)
u=x2-x1;
v=y2-y1;
q = quiver(x1,y1,u,v,'k', 'linewidth',5);
q.ShowArrowHead = 'off';
q.Marker = 'none';
saveas(gcf,['location' num2str(j) '.tif']);

 채택된 답변

Toshia M
Toshia M 2025년 7월 9일
Starting in R2025a, you can export the contents of a figure with specific output dimensions using the exportgraphics function.
You can specify the length of one dimension and MATLAB will adjust the other dimension to preserve the original aspect ratio. For example, create a bar chart and export it as a TIFF file that is 2464 pixels wide.
figure
bar([1 2 3 4 5])
exportgraphics(gcf,"BarWidthOnly.tif",Width=2464)
You can also specify both dimensions, and MATLAB will stretch the output to match your specifications.
figure
bar([1 2 3 4 5])
exportgraphics(gcf,"BarWidthAndHeight.tif",Width=2464,Height=2056)
For a full list of details and options for fine tuning your output, see exportgraphics.

추가 답변 (1개)

Askic V
Askic V 2022년 12월 9일

0 개 추천

The preferred way to save figures is to use builtin function called exportgraphics
Usually, if you need finer/better resolution for printing you specify DPI (dots per inch).
Please have a look at the following code:
figure;
xlim([0 2500]);
ylim([0 2056]);
set(gca,'YDir','reverse');
set(gca,'Units','normalized','Position',[0 0 1 1]);
hold on; x1=[]; y1=[]; x2=[]; y2=[]; u=[]; v=[];
x1=D4(:,1)
y1=D4(:,2)
x2=D4(:,3)
y2=D4(:,4)
u=x2-x1;
v=y2-y1;
q = quiver(x1,y1,u,v,'k', 'linewidth',5);
q.ShowArrowHead = 'off';
q.Marker = 'none';
% The preferred way is to use exportgraphics
exportgraphics(gcf,'test.tiff','Resolution',600)
In my case, it will produce test.tiff image with a resolution of 3501x2626

댓글 수: 3

Thankyou for your reply, is there a way that I can get precisely 2464 * 2056 pixels. How do I modify the above code?
Askic V
Askic V 2022년 12월 9일
편집: Askic V 2022년 12월 9일
If you really need a specific resolution, I guess this is the way to go:
figure;
xlim([0 2500]);
ylim([0 2056]);
set(gca,'YDir','reverse');
set(gca,'Units','normalized','Position',[0 0 1 1]);
hold on; x1=[]; y1=[]; x2=[]; y2=[]; u=[]; v=[];
x1=D4(:,1)
y1=D4(:,2)
x2=D4(:,3)
y2=D4(:,4)
u=x2-x1;
v=y2-y1;
q = quiver(x1,y1,u,v,'k', 'linewidth',5);
q.ShowArrowHead = 'off';
q.Marker = 'none';
% The preferred way is to use exportgraphics
output_size = [2464, 2056];%Size in pixels
resolution = 300;%Resolution in DPI
set(gcf,'paperunits','inches','paperposition',[0 0 output_size/resolution]);
% use 300 DPI
print('test','-dtiff',['-r' num2str(resolution)]);
Thankyou it works well.

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

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 12월 9일

답변:

2025년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by