gpt4 book ai didi

matlab - 在 MATLAB 中如何将多个现有绘图移动到一个子图中?

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:10 25 4
gpt4 key购买 nike

我有一个函数,myFunkyFigure,它接收数据,做一些有趣的事情,并为它生成的图形返回一个轴对象。

我希望能够调用此函数两次,创建两个不同的图形:

fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);

然后我想把它们放在一起放在一个子图中。

请注意,由于 myFunkyFigure 的时髦性,以下内容不起作用。

subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);

我相信我需要类似于 copyobj 的东西,但我无法让它工作(我尝试遵循 a solution in Stack Overflow question Producing subplots and then combine them into a figure later in MATLAB 但无济于事)。

最佳答案

显然,我们不知道你的数字有多“时髦”,但在这种情况下应该注意,最干净的解决方案是修改函数 myFunkyFigure 使其接受 additional optional arguments ,特别是轴的句柄在其中放置它创建的情节。然后你会像这样使用它:

hSub1 = subplot(2,1,1);         %# Create a subplot
myFunkyFigure(dataSet1,hSub1); %# Add a funky plot to the subplot axes
hSub2 = subplot(2,1,2); %# Create a second subplot
myFunkyFigure(dataSet2,hSub2); %# Add a funky plot to the second subplot axes

myFunkyFigure 的默认行为(即没有指定额外的参数)将创建自己的图形并将绘图放在那里。

但是,为了回答您提出的问题,假设您正在输出向量 fig 中的坐标轴句柄(而不是图形句柄),这里有一种方法可以完成此操作(注意:这基本上是 the same solution as the one given in the other question ,但既然你提到在调整它时遇到问题,我想我会重新格式化它以更好地适应你的具体情况):

hFigure = figure();                              %# Create a new figure
hTemp = subplot(2,1,1,'Parent',hFigure); %# Create a temporary subplot
newPos = get(hTemp,'Position'); %# Get its position
delete(hTemp); %# Delete the subplot
set(fig(1),'Parent',hFigure,'Position',newPos); %# Move axes to the new figure
%# and modify its position
hTemp = subplot(2,1,2,'Parent',hFigure); %# Make a new temporary subplot
%# ...repeat the above for fig(2)

上面的代码实际上会将坐标轴从旧图形移动到新图形。如果您希望坐标区对象出现在两个 图中,您可以改为使用函数 COPYOBJ,如下所示:

hNew = copyobj(fig(1),hFigure);  %# Copy fig(1) to hFigure, making a new handle
set(hNew,'Position',newPos); %# Modify its position

另请注意,此处仅使用 SUBPLOT 来生成轴平铺的位置。您可以通过自己指定位置来避免创建然后删除子图的需要。

关于matlab - 在 MATLAB 中如何将多个现有绘图移动到一个子图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com