2

我不知道如何在 MATLAB 中完成以下操作。我有一个看起来像这样的图:

在此处输入图像描述


在图中,我有一个带有许多子图的面板和一个允许我查看面板的一部分的滚动条。

我想将面板的全部内容保存到 PNG 图像文件(不仅仅是可见部分),即我想要一个高矩形文件,并且不需要滚动。

生成图的代码如下:

function draw(obj)
       figure;
       panel1 = uipanel('Parent',1);
       panel2 = uipanel('Parent',panel1);
       panelheight = obj.nIterations / 2;
       set(panel1,'Position',[0 0 0.97 1]);
       set(panel2,'Position',[0 1-panelheight 1 panelheight]); %%
       nPlot = 1;
       for i=1:obj.nIterations
            models = obj.iterations{i};
            for nModel=1:length(models)
                subplot(obj.nIterations,length(models)*2,nPlot);
                nPlot = nPlot + 1;
                drawTransitions(models{nModel});
                set(gca,'Parent',panel2);
                subplot(obj.nIterations,length(models)*2,nPlot);
                nPlot = nPlot + 1;
                drawRewards(models{nModel});
                set(gca,'Parent',panel2);
            end
       end
       s = uicontrol('Style','Slider','Parent',1,...
        'Units','normalized','Position',[0.97 0 0.03 1],...
        'Value',1,'Callback',{@slider_callback1,panel2,panelheight});
end

我尝试了以下方法,但没有成功。

  1. saveas功能可以保存整个图形,而不仅仅是面板。此外,它会剪辑面板的不可见部分。
  2. export_fig(panel2.'file.png')只给出一个纯灰色图像。
4

2 回答 2

0
  • 您可以摆脱 ui 元素,只需使用所有子图制作一个图形,然后使用 eg 导出该图形print -dpng ...

  • saveas将句柄作为第一个参数。也许这不必是图形或模型句柄,但可以是对面板内容的引用。

于 2013-10-10T19:37:49.360 回答
0

为什么不滚动面板并抓取框架并将它们连接在一起?这是一些基本上可以做到这一点的代码。我会发布图片,但我想我没有足够的积分。您可能需要摆弄滚动,并且可能使滑块不可见,但它可以工作。

function printPanel(pnl,filename)




fig  = figure(ancestor(pnl,'figure'));

pnl_units = get(pnl,'units');
fig_units = get(fig,'units');

set(pnl,'units','pixels')
set(fig,'units','pixels')

pnl_rect = getpixelposition(pnl);
fig_rect = getpixelposition(fig);

pnl_height = pnl_rect(4);
fig_height = fig_rect(4);

pnl_rect(2) = -pnl_height;
set(pnl,'position',pnl_rect)


N = ceil(pnl_height/fig_height);

CDATA = cell(N,1);

for i = 1:N
    F = getframe(fig);
    CDATA{i} = F.cdata;
    pnl_rect(2) = pnl_rect(2)+fig_height;
    set(pnl,'position',pnl_rect)
    drawnow
end


set(pnl,'units',pnl_units)
set(fig,'units',fig_units)

imwrite(cat(1,CDATA{:}),filename)
end
于 2013-08-13T05:48:13.890 回答