Matlab 保存具有预定义大小的图形
我有一个图,上面有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OuterPosition
figure 属性 仅在以下位置发生变化:屏幕上有图形窗口;它不会改变打印方式。Matlab 在“打印”图形时使用
PaperSize
、PaperUnits
、PaperPosition
和类似的图形属性,即使它们实际上没有意义,例如生成位图文件时。 (将PaperUnits
设置为像素
是合乎逻辑的,但它不起作用。)获取以像素为单位的特定图像大小的过程是:将
PaperPosition
设置为某个尺寸,以英寸(或其他物理单位)为单位,然后使用- 以每英寸点数指定所需的分辨率 r
选项print
:其中一些特性在
打印
功能。您还可以尝试使用
-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. (SettingsPaperUnits
topixels
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 toprint
: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.按照@nibot的示例,我编写了以下函数:
Following @nibot's example, I wrote the following function: