简体   繁体   English

如何在Matlab中重用图形?

[英]How can reuse figure in Matlab?

I would like to reuse a figure I create in Matlab script. 我想重用我在Matlab脚本中创建的图形。

fig1 = figure;
plot(...);
title(...);
% ...
% now I would like to plot fig1 again with a different title
% ...
% now I would like to plot fig1 again as a subplot in a 2x2 grid

How can I do that without code duplication? 没有代码重复怎么办?

Can I use the figure object? 我可以使用图形对象吗? Or perhaps save the plot object somehow? 还是以某种方式保存绘图对象?

plot and friends all work on the current axes, so just put all of that code (not including figure ) into a separate (sub)function, then call it after setting up a new figure/title/subplot. plot和好友都在当前轴上工作,因此只需将所有代码(不包括figure )放入单独的(子)函数中,然后在设置新的Figure / title / subplot后调用它。

If you can't do this for whatever reason, check out the example at the bottom of the page here . 如果由于某种原因您无法执行此操作,请在此处查看页面底部的示例。

fig1 = figure; 图1 =图;

p1=plot(...); p1 = plot(...);

title('something'); title('某物');

% ... %...

% now I would like to plot fig1 again with a different title 现在我想用不同的标题再次绘制fig1

title('something else'); 标题(“其他”); % This will replace the old title with the new one 'something_else'. %这将用新的“ something_else”替换旧标题。

% now I would like to plot fig1 again as a subplot in a 2x2 grid 现在我想再次将fig1绘制为2x2网格中的子图

delete(p1); delete(p1);

subplot(2,2,1); 子图(2,2,1);

p1=plot(...); p1 = plot(...);

OR, you can just refresh your figure (without closing it and opening another one...) by typing: 或者,您可以通过键入以下内容来刷新图形(无需关闭图形并打开另一个图形...):

clf reset clf重置

This will reset all figure properties, such as background color. 这将重置所有图形属性,例如背景色。 Then, you can re-plot whatever you like. 然后,您可以重新绘图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM