如何将 MATLAB 中的多个现有图移动到一个子图?
我有一个函数 myFunkyFigure
,它接收数据,执行一些有趣的操作,并为其生成的图形返回一个轴对象。
我希望能够调用此函数两次,创建两个不同的图形:
fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);
然后我想将它们一起放入一个子图中。
请注意,由于 myFunkyFigure
的时髦性,以下内容不起作用。
subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);
我相信我需要类似于 copyobj
的东西,但我无法让它发挥作用(我尝试按照 Stack Overflow 问题中的解决方案生成子图,然后稍后在 MATLAB 中将它们组合成图形,但无济于事)。
I have a function, myFunkyFigure
, that takes in data, does some funky things, and returns an axis object for the figure it produces.
I would like to be able to invoke this function twice, creating two different figures:
fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);
Then I would like to put them into a subplot together.
Note that, because of the funkiness of myFunkyFigure
, the following does not work.
subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);
I believe that I need something along the lines of copyobj
, but I haven't been able to get that to work (I tried following a solution in Stack Overflow question Producing subplots and then combine them into a figure later in MATLAB but to no avail).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,我们不知道你的数字有多“时髦”,但应该注意的是,在这种情况下,最干净的解决方案是修改函数
myFunkyFigure
,使其接受 其他可选参数,特别是用于放置其创建的绘图的轴的句柄。然后您将像这样使用它:myFunkyFigure
的默认行为(即没有指定其他参数)将创建它自己的图形并将绘图放在那里。但是,为了回答您提出的问题,考虑到您要输出矢量
fig 中的轴句柄(而不是图形句柄),这里有一种方法可以实现此目的
(注意:这基本上是与中给出的解决方案相同另一个问题,但既然你提到调整它时遇到困难,我想我应该重新格式化它以更好地适合你的具体情况):上面的内容实际上会将轴从旧图形移动到新图形。如果您希望坐标区对象出现在两个图中,您可以使用函数COPYOBJ 像这样:
还要注意 SUBPLOT 此处仅用于生成轴平铺的位置。您可以通过自己指定位置来避免创建然后删除子图的需要。
Obviously, we don't know how "funky" your figures are, but it should be noted in such a case that the cleanest solution would be to modify the function
myFunkyFigure
such that it accepts additional optional arguments, specifically the handle of an axes in which to place the plot it creates. Then you would use it like so:The default behavior of
myFunkyFigure
(i.e. no additional arguments specified) would be to create its own figure and place the plot there.However, to answer the question you asked, here's a way to accomplish this given that you are outputting the axes handles (not the figure handles) in the vector
fig
(note: this is basically the same solution as the one given in the other question, but since you mention having trouble adapting it I thought I'd reformat it to better fit your specific situation):The above will actually move the axes from the old figure to the new figure. If you want the axes object to appear in both figures, you can instead use the function COPYOBJ like so:
Also note that SUBPLOT is only used here to generate a position for the tiling of the axes. You could avoid the need to create and then delete subplots by specifying the positions yourself.
gnovice 的代码对我不起作用。
似乎一个人物无法成为另一个人物的孩子。例如 hNew = copyobj(fig(1),hFigure);给出了错误
相反,我必须将轴设为新图形的子级。这是我想出的功能
这似乎对我有用。
The code from gnovice didn't work for me.
It seemed that a figure couldn't be made the child of another figure. For example hNew = copyobj(fig(1),hFigure); gave the error
Instead, I had to make the axes children of the new figure. This is the function I came up with
This seems to work for me.