【问题标题】:Recreate plot in MATLAB using figure handle使用图形句柄在 MATLAB 中重新创建绘图
【发布时间】:2013-08-09 03:19:25
【问题描述】:

是否可以通过将相同的图保存到某种类型的轴句柄来创建两次?

我的绘图代码为散点图中的每个点创建特殊符号。我想创建一个带有两个子图的图形。我想为轴的特定部分(0 到 10)设置的第一个图,我想从(90:100)开始的第二个图。我知道我可以通过复制和粘贴代码来重新创建情节,但是如果我更改情节的其他内容,这似乎很麻烦并且管理起来很麻烦。

我是否可以只创建一次绘图,将其保存到句柄,然后重新绘制?

这基本上是我正在寻找的功能:

figure;
hold on;
x = [1  10 20 10   2000 3000];
y = [10 30 40 20   100   200 ];

// Create plot one point at a time     
for i = 1:4
subplot(2,1,1); plot(x(i),y(i),'r');

// REDACTED CODE
// There is a bunch of code here to adjust the look of the first plot for each point
// In order to define the look of each marker in a scatter plot, 
// this has to be done one point at a time
// REDACTED CODE

end

// Adjust axis
axis([1 60 0 50]);

// Get figure handle
handle = gcf;


// Create second plot with same characteristics as first plot but with different axis boundaries
subplot(2,1,2); plot(handle);
axis([90 250 1000 4000]);  

【问题讨论】:

  • 如何创建一个将范围作为输入并生成绘图的函数?这是避免复制和粘贴代码的好方法。
  • 我可以这样做,但是每次我想要这个情节时我都必须创建一个函数。我只想要一些通用的解决方案,可以用于我计划创建的任何子图。
  • 你的意思是只使用一次函数plot?
  • 我不明白。每次您希望在不同的子图中绘制绘图时,只需使用不同的输入调用相同的函数。
  • 我刚刚更新了问题,以便您明白我的意思。请看我写的代码

标签: matlab graph charts


【解决方案1】:

您可以通过以下方式将该代码转换为函数:

function makeAPlot(axBound, ax)
% set current axis
axes(ax)

hold on;
x = [1  10 20 10   2000 3000];
y = [10 30 40 20   100   200 ];

for i = 1:4
 plot(x(i),y(i),'r');

% // REDACTED CODE
% // There is a bunch of code here to adjust the look of the first plot for each point
% // In order to define the look of each marker in a scatter plot,
% // this has to be done one point at a time
% // REDACTED CODE

end

axis(axBound)

以下是如何使用不同的参数调用它两次以将其绘制在具有不同轴范围的不同子图中:

ax1 = subplot(2,1,1);
makeAPlot([1 60 0 50], ax1)

ax2 = subplot(2,1,2);
makeAPlot([90 250 1000 4000], ax2)

如果这不起作用,您可以查看copyobj函数并使用它将句柄复制到绘图。

【讨论】:

  • 感谢您的回答,但这是针对特定情节的。我需要一个不需要为不同数据集创建新函数的通用解决方案
  • 将您的数据集添加为函数的输入。函数通常是通用解决方案。如果它真的不起作用,请查看我的编辑并尝试 copyobj 功能。
  • 嗯,有没有使用copyobj函数的例子?
猜你喜欢
  • 2014-11-03
  • 2014-05-20
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多