uifigure vector export size/resolution
조회 수: 16 (최근 30일)
이전 댓글 표시
Suppose I want to export a uifigure to a vector file (*.pdf) at a specific size. The exportgraphics command has a resolution option, but that only works for bitmap files (*.png, etc.). Is there are way to explicitly control either the vector file size and/or resolution?
I'm trying to replicate what was once a useful feature of MATLAB figures. Traditional (Java based) figures use the print function, where the physical size of the figure could be replicated in that file. For example, I could make a 4"x6" figure on my screen that generated a 4"x6" PDF file. This process relied on the root ScreenPixelsPerInch setting, which stopped working in 2015b. For more information, see https://undocumentedmatlab.com/articles/graphic-sizing-in-matlab-r2015b.
Since (Javascropt) uifigures are sized purely on pixels, I was thinking that one could scale the window so that it appeared correctly on the screen. This usually requires a local calibration, e.g. adjusting a figure's width to match an 8.5" wide sheet of paper, and using that calibration to generate/control new uifigures. The problem is that those figures cannot be exported to the correct size in a vector format; bitmap files seem OK.
댓글 수: 2
Avni
2023년 12월 15일
Are you trying to implement something like this?
% Define desired physical size in inches and resolution in DPI
desired_width_in = 2; % Width in inches
desired_height_in = 3; % Height in inches
DPI = 300; % Desired dots per inch
% Calculate the size in pixels
width_px = desired_width_in * DPI;
height_px = desired_height_in * DPI;
% Create the uifigure with the calculated pixel dimensions
fig = uifigure('Position', [100, 100, width_px, height_px]);
% Add an axes component
ax = uiaxes('Parent', fig, 'Position', [0, 0, width_px, height_px]);
% Plot some data in the axes
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(ax, x, y);
% Export the figure to a PDF file
% The size of the PDF will match the size of the uifigure
exportgraphics(ax, 'output_figure.pdf', 'ContentType', 'vector');
% Close the figure if no longer needed
close(fig);
채택된 답변
Yair Altman
2023년 12월 21일
편집: Yair Altman
2023년 12월 21일
As already noted above, neither print nor the exportgraphics functions support setting the output page size, and ignore the figure's PaperSize property.
An alternative: use the export_fig utility with the 'preserve_size' parameter, to indicate that the figure's PaperSize should be used in the generated output (the exported contents will start from the bottom-left corner of the page). This should work well for both legacy (Java-based) figures and newer (web-based) uifigures:
export_fig(fig, 'figure.pdf', '-preserve_size');
Note that the PaperSize property works with the PaperUnits property to set the actual physical size. By default, a new figure's PaperSize is set to [8.5,11] and PaperUnits to 'inches', which corresponds to US letter size. These default values might be different based on your specific installation/setup, and can always be modified or set ad-hoc to a new value independently for each figure.
추가 답변 (1개)
Sujay
2023년 12월 11일
Hi Daniel,
There is no direct way to export uifigure to a vector file (*.pdf) at a specific size using the exportgraphics command in MATLAB. The resolution option only works for bitmap files like PNGs. This creates a challenge for replicating the functionality of the print function used for traditional Java-based figures.
However, there are some workarounds you can try to achieve your desired outcome:
1) Manually set figure size: Use the uifigure.Position property to set the figure size in pixels. You can then export the figure using exportgraphics and use the Resolution option to ensure the exported image is of sufficient quality.
fig = uifigure('Position', [100 100 400 600]);
% Add your UI elements to the figure
exportgraphics(fig, 'figure.pdf', 'Resolution', 300);
2) Use the paperposition property: Set the fig.PaperPosition property to specify the desired size in units like centimeters or inches. This will determine the size of the exported PDF file.
fig = uifigure;
% Add your UI elements to the figure
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 8.4 5];
exportgraphics(fig, 'figure.pdf');
3) Export to SVG and then to PDF:
- Export the uifigure to an SVG file using the exportgraphics command.
- Use a suitable vector graphics editor (Ex: Inkscape) to open the SVG file and resize it to your desired dimensions.
- Save the file as a PDF.
The best approach will depend on your specific needs and preferences. Experiment with different methods and choose the one that best suits your workflow and desired output.
Regards,
Sujay
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!