필터 지우기
필터 지우기

export_fig crop to specific dimensions

조회 수: 31 (최근 30일)
Jessica Yorzinski
Jessica Yorzinski 2022년 7월 30일
편집: Yash 2024년 1월 7일
I would like to save a figure with a specific DPI and size. In particular, I would like the photo to be 1080 px wide x 270 px high with a dpi of 350.
I have tried using the crop part of export_fig (and changing the NaN to specific numbers) but it has not produced what I need.
export_fig('test','-png','-c[NaN,NaN,NaN,NaN]', '-r350');
I'd appreciate any tips!
  댓글 수: 2
Rik
Rik 2022년 8월 1일
If you first export the pixels, you can write it to a file that allows setting a dpi. Personally I think DPI hardly ever makes sense to specify, since the pixels define the actual image data.
Walter Roberson
Walter Roberson 2022년 8월 1일
편집: Walter Roberson 2022년 8월 1일
In order to get a resolution stored inside a png file, there are two possibilities:
  • png files natively support storing a Resolution Unit, and x and y resolutions per unit. If you imwrite() then the option names are ResolutionUnit and XResolution and YResolution. It would not astonish me at all if export_fig does not know to write the fields.
  • since 2017 the revised png specification have supported using an Exif chunk to write Exif attributes. MATLAB itself does not support adding Exif to any file format, and I suspect that export_fig does not do so either .
If neither of these happens then resolution information will not appear in the png file .
These days you would use newer matlab routines to copy the graphics, and then you would imwrite the data file passing in the resolution information .

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

채택된 답변

Yash
Yash 2024년 1월 3일
편집: Yash 2024년 1월 7일
Hi Jessica,
I undertsand that you are interested in saving a MATLAB figure with specific DPI and size.
While the "export_fig" function from the File Exchange platform might come to mind, it's preferable to utilize MATLAB's built-in capabilities for such tasks. You can use the "print" function to export the figure with the desired properties.
To achieve a 1080x270 px image at 350 dpi, you need to calculate the figure's width and height in inches because MATLAB handles figure sizes in inches. Here's how you can do it:
1. Calculate the size in inches using the desired pixel dimensions and DPI (dots per inch):
  • Width in inches = width in pixels / DPI
  • Height in inches = height in pixels / DPI
2. Set the figure's 'PaperPositionMode' to 'manual' to allow custom figure sizes.
3. Set the figure's 'PaperUnits' to 'inches' to specify the size in inches.
4. Set the figure's 'PaperPosition' to define the location and size of the figure.
5. Finally, use "print" function to save the figure with the specified DPI.
Here's an example code snippet to illustrate the process:
% Desired pixel dimensions and DPI
width_px = 1080;
height_px = 270;
dpi = 350;
% Convert pixel dimensions to inches
width_in = width_px / dpi;
height_in = height_px / dpi;
% Create a new figure or get the handle of the current figure
fig = figure;
% Set figure properties
set(fig, 'PaperPositionMode', 'manual');
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0, 0, width_in, height_in]);
% Your plotting code here
plot(sin(0:0.001:2*pi))
% Save the figure with export_fig
print(fig, 'test.png', '-dpng', ['-r' num2str(dpi)]);
% Close the figure if it's no longer needed
close(fig);
I have verified that this method produces an image with the specified dimensions and resolution.
Hope this helps you to address the query.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by