【问题标题】:Print figure when plot finishes, works on debug mode but not in runtime绘图完成时打印图形,在调试模式下工作,但在运行时不工作
【发布时间】:2018-04-11 03:19:54
【问题描述】:

我使用 GUIDE 开发了一个 Matlab 应用程序,该应用程序允许加载图像并对其进行分析。简而言之,我使用 imshow 显示图像并添加标记和文本(分别使用 plottext 函数)。我想将图形打印到文件中,包括标记和文本,但奇怪的是它只打印原始图像。另一方面,当我在打印命令之前放置断点时,它可以完美运行,所以在我看来这是一个“时间”的问题。但是,我尝试过“drawnow”和“pause(X)”都无济于事,所以我想知道我做错了什么还是有其他方法可以做到这一点。 这是“updateImage()”函数(由按钮调用)中的伪代码:

cla(handles.h_image); %clear image axes. "handles" is a structure created by GUIDE containing handles to all objects in the figure. "handles.h_image" is the handle to the axes of my image
im = imshow(ch_data,'Parent',handles.h_image); drawnow %ch_data contains the image
hold on
for n = 1:length(myarray) % myarray is not actually too big, length of about 100 
    x1 = whatever; x2 = whatever; y1 = whatever; y2 = whatever;
    plot([x1 x2],[y1 y2],'linestyle','-','linewidth',1,'color',[211 99 252]/255);
    text(x1, y1, num2str(n),'Fontsize',10,'color',[208 240 255]/255);    
end
pause(5) %tried different values, but still not working in runtime
cd(results_dir) 
print(handles.container,'-dtiff', '-opengl', '-r300', 'myimagename');

编辑:我已经在单独的脚本上尝试了这部分代码,它工作正常......

f = figure;
im = imshow('peppers.png'); drawnow 
hold on
r = randi([1 size(im.CData,1)],1,200);
for n = 1:50 
    x1 = r(n); x2 = r(n*2); y1 = r(n*3); y2 = r(n*4);
    plot([x1 x2],[y1 y2],'linestyle','-','linewidth',1,'color',[211 99 252]/255);
    text(x1, y1, num2str(n),'Fontsize',10,'color',[208 240 255]/255);    
end
print(f,'-dtiff', '-opengl', '-r300', 'myimagename');

【问题讨论】:

  • 最好有一个我们可以尝试的最小的工作示例
  • 你试过不用opengl吗?过去我的行为很古怪......
  • @LuisMendo,问题是原始代码包含数百行,它被集成为一个 GUIDE 应用程序。我已经发布了代码(我认为)是相关的。尽管如此(请参阅我的编辑),我已经单独尝试了该代码并且它运行完美,因此 GUIDE 自动生成的代码一定有一些混乱。
  • @Justin 是的,我尝试不使用 opengl,但问题仍然存在。事实上,我尝试了我单独发布的代码,并且它可以工作(即使使用 opengl)
  • @AnderBiguri 是的,也不起作用。我猜这是 GUIDE 引入的某种奇怪的行为......

标签: matlab


【解决方案1】:

您的两个代码 sn-ps 之间的一个区别是,第一个您保存的是“handles.container”,而另一个是图形句柄。

试试

f = ancestor ( handles.h_image, 'figure )
print ( f, ..... )

【讨论】:

  • 感谢您的建议,但在我的代码中,handles.container 实际上是handles.h_image 的图形祖先,所以问题仍然存在。我想我必须接受它是一种错误,因为它在调试时工作得非常好。
  • 保存的文件中有什么内容?它是一个空图像吗?你能看到屏幕上的图像吗?
  • 它打印我用“imshow”命令绘制的任何内容,但不打印“plot”命令打印的标记和“text”命令打印的文本。在调试模式下,所有内容都会打印出来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 2021-12-18
  • 1970-01-01
相关资源
最近更新 更多