Matlab 保存具有预定义大小的图形

发布于 2024-12-01 21:36:47 字数 698 浏览 0 评论 0原文

我有一个图,上面有 2 个图。我正在尝试将图形保存为宽度较长的 png。

%%%%%%%%%%%%First%%%%%%%%%%%%%%%%%%
a=figure('Name','First Structure');
load C:\Users\William\workspace\P5\FirstAdd.txt
n=FirstAdd(:,1);
t=FirstAdd(:,2);
subplot(1,2,1);
plot(n,t);
xlabel('n');
ylabel('Time');
title('First Structure''s Add');
grid on

load C:\Users\William\workspace\P5\FirstContains.txt
n=FirstContains(:,1);
t=FirstContains(:,2);
subplot(1,2,2);
plot(n,t);
xlabel('n');
ylabel('Time');
title('First Structure''s Contains');
grid on

rect=[250,250,1080,480];
set(a, 'OuterPosition',rect);
print(a,'-dpng','First Structure.png');

在最后 3 行中,我设置了图形窗口,使 2 个图足够宽。但是,当我尝试保存图形时,图像是其默认大小,其中绘图被压扁。

我缺少什么?

I have a figure with 2 plots on it. I am trying to save the figure as a png with a longer width.

%%%%%%%%%%%%First%%%%%%%%%%%%%%%%%%
a=figure('Name','First Structure');
load C:\Users\William\workspace\P5\FirstAdd.txt
n=FirstAdd(:,1);
t=FirstAdd(:,2);
subplot(1,2,1);
plot(n,t);
xlabel('n');
ylabel('Time');
title('First Structure''s Add');
grid on

load C:\Users\William\workspace\P5\FirstContains.txt
n=FirstContains(:,1);
t=FirstContains(:,2);
subplot(1,2,2);
plot(n,t);
xlabel('n');
ylabel('Time');
title('First Structure''s Contains');
grid on

rect=[250,250,1080,480];
set(a, 'OuterPosition',rect);
print(a,'-dpng','First Structure.png');

In the last 3 lines I set the figure window such that the 2 plots are wide enough. However, when I try to save the figure, the image is its default size in which the plots are squished.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-12-08 21:36:47

OuterPosition figure 属性 仅在以下位置发生变化:屏幕上有图形窗口;它不会改变打印方式。

Matlab 在“打印”图形时使用 PaperSizePaperUnitsPaperPosition 和类似的图形属性,即使它们实际上没有意义,例如生成位图文件时。 (将 PaperUnits 设置为 像素 是合乎逻辑的,但它不起作用。)

获取以像素为单位的特定图像大小的过程是:将 PaperPosition 设置为某个尺寸,以英寸(或其他物理单位)为单位,然后使用 - 以每英寸点数指定所需的分辨率 r 选项print

r = 150; % pixels per inch
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 1080 480]/r);
print(gcf,'-dpng',sprintf('-r%d',r), 'bar.png');

其中一些特性在 打印 功能。

您还可以尝试使用 -r0 选项来告诉 Matlab 使用显示分辨率。

The OuterPosition figure property only changes where the figure window is on the screen; it doesn't change how it will print.

Matlab uses the PaperSize, PaperUnits, PaperPosition and similar figure properties when "printing" a figure, even when they don't really make sense, such as when producing a bitmap file. (Settings PaperUnits to pixels would be logical, but it doesn't work.)

The procedure for getting a particular image size in pixels is to set PaperPosition to some size in inches (or another physical unit) and then specify the desired resolution in dots per inch using the -r option to print:

r = 150; % pixels per inch
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 1080 480]/r);
print(gcf,'-dpng',sprintf('-r%d',r), 'bar.png');

Some of these peculiarities are discussed in the help for the print function.

You could also try the -r0 option which tells Matlab to use the display resolution.

蘑菇王子 2024-12-08 21:36:47

按照@nibot的示例,我编写了以下函数:

function save_as_png(handle, filename, dpi, width, height);
    set(handle, 'PaperUnits', 'inches', 'PaperPosition', [0 0 width height] / dpi);
    print(handle, '-dpng', ['-r' num2str(dpi)], filename);
end

Following @nibot's example, I wrote the following function:

function save_as_png(handle, filename, dpi, width, height);
    set(handle, 'PaperUnits', 'inches', 'PaperPosition', [0 0 width height] / dpi);
    print(handle, '-dpng', ['-r' num2str(dpi)], filename);
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文