export_fig crop to specific dimensions
35 次查看(过去 30 天)
显示 更早的评论
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
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
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
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1581296/image.png)
Hope this helps you to address the query.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!