Exporting figure keeping in given pixel size

26 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

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.

More Answers (0)

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!