Exporting figure maintaining given pixel size

19 views (last 30 days)
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']);

Accepted Answer

Toshia M
Toshia M on 9 Jul 2025
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.

More Answers (1)

Askic V
Askic V on 9 Dec 2022
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 Comments
Askic V
Askic V on 9 Dec 2022
Edited: Askic V on 9 Dec 2022
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)]);

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!