export_fig crop to specific dimensions
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jessica Yorzinski
el 30 de Jul. de 2022
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 comentarios
Rik
el 1 de Ag. de 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
el 1 de Ag. de 2022
Editada: Walter Roberson
el 1 de Ag. de 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 .
Respuesta aceptada
Yash
el 3 de En. de 2024
Editada: Yash
el 7 de En. de 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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!