如何将 MATLAB 中的多个现有图移动到一个子图?

发布于 2024-11-03 09:27:57 字数 577 浏览 1 评论 0原文

我有一个函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

显然,我们不知道你的数字有多“时髦”,但应该注意的是,在这种情况下,最干净的解决方案是修改函数 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

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)

上面的内容实际上会将轴从旧图形移动到新图形。如果您希望坐标区对象出现在两个图中,您可以使用函数COPYOBJ 像这样:

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

还要注意 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:

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

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):

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)

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:

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

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.

紧拥背影 2024-11-10 09:27:57

gnovice 的代码对我不起作用。

似乎一个人物无法成为另一个人物的孩子。例如 hNew = copyobj(fig(1),hFigure);给出了错误

Error using copyobj
Object figure[1] can not be a child of parent
figure[1]

相反,我必须将轴设为新图形的子级。这是我想出的功能

function []= move_to_subplots(ax,a,b)
%     %
% Inputs:
%       inputname: 
% Outputs:
%       name:  description type units
%       saved data: (does this overwrite a statically named file?)
%       plots:
%
% Standard call:
%
%
% Written by C. Hogg Date 2012_06_01
%
% 
debugmode=0;

hFigure=figure();

if ~exist('a')
        a=ceil(sqrt(length(ax)));
end

if ~exist('b')
        b=1;
    end    

if a*b<length(ax)|~exist('a')|~exist('b')
    disp('Auto subplot sizing')

    b=ceil(length(ax)/a);
end

for i=1:length(ax)

hTemp = subplot(a,b,i,'Parent',hFigure);         %# Make a new temporary subplot
newPos = get(hTemp,'Position');                  %# Get its position
delete(hTemp);

hNew = copyobj(ax(i),hFigure);
set(hNew,'Position',newPos)
end

%% Debug. Break point here.
if debugmode==1; dbstop tic; tic; dbclear all;end

end

这似乎对我有用。

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

Error using copyobj
Object figure[1] can not be a child of parent
figure[1]

Instead, I had to make the axes children of the new figure. This is the function I came up with

function []= move_to_subplots(ax,a,b)
%     %
% Inputs:
%       inputname: 
% Outputs:
%       name:  description type units
%       saved data: (does this overwrite a statically named file?)
%       plots:
%
% Standard call:
%
%
% Written by C. Hogg Date 2012_06_01
%
% 
debugmode=0;

hFigure=figure();

if ~exist('a')
        a=ceil(sqrt(length(ax)));
end

if ~exist('b')
        b=1;
    end    

if a*b<length(ax)|~exist('a')|~exist('b')
    disp('Auto subplot sizing')

    b=ceil(length(ax)/a);
end

for i=1:length(ax)

hTemp = subplot(a,b,i,'Parent',hFigure);         %# Make a new temporary subplot
newPos = get(hTemp,'Position');                  %# Get its position
delete(hTemp);

hNew = copyobj(ax(i),hFigure);
set(hNew,'Position',newPos)
end

%% Debug. Break point here.
if debugmode==1; dbstop tic; tic; dbclear all;end

end

This seems to work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文