export_fig crop to specific dimensions

42 vues (au cours des 30 derniers jours)
Jessica Yorzinski
Jessica Yorzinski le 30 Juil 2022
Modifié(e) : Yash le 7 Jan 2024
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 commentaires
Rik
Rik le 1 Août 2022
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 le 1 Août 2022
Modifié(e) : Walter Roberson le 1 Août 2022
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 .

Connectez-vous pour commenter.

Réponse acceptée

Yash
Yash le 3 Jan 2024
Modifié(e) : Yash le 7 Jan 2024
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.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by