Exporting figure maintaining given pixel size

28 ビュー (過去 30 日間)
PRAGYA ARORA
PRAGYA ARORA 2022 年 12 月 9 日
回答済み: Toshia M 2025 年 7 月 9 日
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']);
  2 件のコメント
Eric Delgado
Eric Delgado 2022 年 12 月 9 日
編集済み: Eric Delgado 2022 年 12 月 9 日
PRAGYA ARORA
PRAGYA ARORA 2022 年 12 月 9 日
Thankyou

サインインしてコメントする。

採用された回答

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 日
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 件のコメント
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)]);
PRAGYA ARORA
PRAGYA ARORA 2022 年 12 月 9 日
Thankyou it works well.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by