Matlab GUI绘制区域大小规范

问题描述 投票:0回答:1

我用它来获取并保存图形:

function sketch(cmd)
  if nargin == 0
      cmd = 'init';
  end

switch cmd
case 'init'
    fig = figure('DoubleBuffer','on','back','off');
    info.ax = axes('XLim',[0 1],'YLim',[0 1]);
    info.drawing = [];
    info.x = [];
    info.y = [];
    set(fig,'UserData',info,...
            'WindowButtonDownFcn',[mfilename,' down'])

case 'down'
    myname = mfilename;
    fig = gcbf;
    info = get(fig,'UserData');
    curpos = get(info.ax,'CurrentPoint');
    info.x = curpos(1,1);
    info.y = curpos(1,2);
    info.drawing = line(info.x,info.y,'Color','k');
    set(fig,'UserData',info,...
            'WindowButtonMotionFcn',[myname,' move'],...
            'WindowButtonUpFcn',[myname,' up'])

case 'move'
    fig = gcbf;
    info = get(fig,'UserData');
    curpos = get(info.ax,'CurrentPoint');
    info.x = [info.x;curpos(1,1)];
    info.y = [info.y;curpos(1,2)];
    set(info.drawing,'XData',info.x,'YData',info.y)
    set(fig,'UserData',info)

case 'up'
    fig = gcbf;
    set(fig,'WindowButtonMotionFcn','',...
            'WindowButtonUpFcn','')
saveas(gcf, 'test.png');
export_fig test2.png
end    

我想将区域大小固定为50x50(50像素,50pix),并且没有x-y轴。

我尝试过:

x0=500;
y0=500;
width=50;
height=50;
set(gcf,'position',[x0,y0,width,height])

但是,它看起来并不好,并且将图像保存为120x50像素。如何将图形另存为50x50像素?

matlab matlab-figure
1个回答
0
投票

检查我编写的此函数,它完全符合您的要求-绘制图形并将图像导出为指定的大小

https://github.com/fangq/iso2mesh/blob/v1.9.2/img2mesh.m#L516-L535

关键是使用此行设置轴

ax=axes('parent',gcf,'Units','pixels','position',[1, 1, imsize(1), imsize(2)]);

其中单位是像素,并且轴宽通过position调整为所需的像素长度。完成此操作后,getframe将获得图像。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.