我有一个函数,myFunkyFigure
它接收数据,做一些时髦的事情,并为它产生的数字返回一个轴对象.
我希望能够两次调用此函数,创建两个不同的数字:
fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);
Run Code Online (Sandbox Code Playgroud)
然后我想把它们放在一起.
请注意,由于其功能myFunkyFigure
,以下功能无效.
subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);
Run Code Online (Sandbox Code Playgroud)
我相信我需要一些东西copyobj
,但我无法让它工作(我尝试在Stack Overflow问题中生成子图,然后在MATLAB中将它们组合成一个图但是无济于事) .
gno*_*ice 11
显然,我们不知道你的数字是多么"时髦",但在这种情况下应该注意,最干净的解决方案是修改函数myFunkyFigure
,使其接受额外的可选参数,特别是轴的句柄.放置它创建的情节.然后你会像这样使用它:
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
Run Code Online (Sandbox Code Playgroud)
myFunkyFigure
(即没有指定其他参数)的默认行为是创建自己的图并将图放在那里.
但是,要回答你问的问题,这里有一种方法可以实现这一点,因为你在向量中输出了轴句柄(而不是图形句柄)fig
(注意:这与其他问题中给出的解决方案基本相同,但是既然你提到我很难适应它,我想我会重新格式化它以更好地适应你的具体情况):
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)
Run Code Online (Sandbox Code Playgroud)
以上将实际将轴从旧图移动到新图.如果您希望轴对象出现在两个图中,您可以改为使用COPYOBJ函数,如下所示:
hNew = copyobj(fig(1),hFigure); %# Copy fig(1) to hFigure, making a new handle
set(hNew,'Position',newPos); %# Modify its position
Run Code Online (Sandbox Code Playgroud)
另请注意,SUBPLOT仅用于生成轴平铺的位置.您可以通过自己指定位置来避免创建然后删除子图的需要.