Combine figures with subplots in new figure with subplots
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello,
how can I combine 2 figures (h1 and h2) with 2 subplots to one figure (c) with 4 subplots?
h1 = figure
subplot(2,1,1)
subplot(2,1,2)
h2 = figure
subplot(2,1,1)
subplot(2,1,2)
c = figure
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
채택된 답변
Mathieu NOE
2023년 11월 30일
hello Paul
like this ?
% Create first figure
hf_sub(1) = figure(1);
hp(1) = uipanel('Parent',hf_sub(1),'Position',[0 0 1 1]);
subplot(2,1,1,'Parent',hp(1));
plot(1:10);
subplot(2,1,2,'Parent',hp(1));
plot(rand(1,100));
% Create second figure
hf_sub(2) = figure(2);
hp(2) = uipanel('Parent',hf_sub(2),'Position',[0 0 1 1]);
subplot(2,1,1,'Parent',hp(2));
histogram(randn(1,1000));
subplot(2,1,2,'Parent',hp(2));
membrane
% Create third figure
hf_sub(3) = figure(3);
hp(3) = uipanel('Parent',hf_sub(3),'Position',[0 0 1 1]);
subplot(2,2,1,'Parent',hp(3));
histogram(randn(1,1000));
subplot(2,2,2,'Parent',hp(3));
membrane
subplot(2,2,3,'Parent',hp(3));
surf(peaks)
subplot(2,2,4,'Parent',hp(3));
plot(-(1:10));
% Create combined figure (horizontal concatenation)
hf_main = figure(4);
npanels = numel(hp);
hp_sub = nan(1,npanels);
% Copy over the panels
for idx = 1:npanels
hp_sub(idx) = copyobj(hp(idx),hf_main);
set(hp_sub(idx),'Position',[(idx-1)/npanels,0,1/npanels,1]);
end




% Create combined figure (vertical concatenation)
hf_main = figure(5);
npanels = numel(hp);
hp_sub = nan(1,npanels);
% Copy over the panels
for idx = 1:npanels
hp_sub(idx) = copyobj(hp(idx),hf_main);
% set(hp_sub(idx),'Position',[0,(idx-1)/npanels,1,1/npanels]); % figures are added from y bottom to top
set(hp_sub(idx),'Position',[0,(npanels-idx)/npanels,1,1/npanels]); % figures are added from y top to bottom
end

댓글 수: 12
Hey, Mathieu,
thank you. But my problem is, that I have already given e.g. your Figures 1 and 2. So the handle
hp(1) = uipanel('Parent',hf_sub(1),'Position',[0 0 1 1]);
isn't possible in my case. Is there a work around?
hello Paul
sorry I am not sure to understand your problem
going back to the original question, if you have two figures with two subplots , why not simply maket one figure with 4 subplots ?
Yeah, that is the goal.
However, the two figures with two subplots are returned by a function that can't be changed (not my own function). So I have to find a way to combine these two figures to one figure with 4 subplots.
Hopefully my problem is a bit clearer now.
in other words , you want to stack two figs ?
this is addressed here :
%First Figure
h1 = openfig('test1.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
%Second Figure
h2 = openfig('test2.fig','reuse');
ax2 = gca;
h3 = figure; %create new figure
s1 = subplot(1,2,1); %create and get handle to the subplot axes
s2 = subplot(1,2,2);
fig1 = get(ax1,'children'); %get handle to all the children in the figure
fig2 = get(ax2,'children');
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);
Mathieu NOE
2023년 11월 30일
편집: Mathieu NOE
2023년 11월 30일
Hey, yeah, I have already tried something like that. The problem is, that only the last subplot of each figure is copied. But I need to have all in a 2x2 subplot.
% Create first figure
figure;
subplot(2,1,1);
plot(1:10);
subplot(2,1,2);
plot(rand(1,100));
ax1 = gca; % get handle to axes of figure
% Create second figure
figure;
subplot(2,1,1);
histogram(randn(1,1000));
subplot(2,1,2);
membrane
ax2 = gca;
h3 = figure; %create new figure
s1 = subplot(1,2,1); %create and get handle to the subplot axes
s2 = subplot(1,2,2);
fig1 = get(ax1,'children'); %get handle to all the children in the figure
fig2 = get(ax2,'children');
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);

Paul Muster
2023년 11월 30일
편집: Paul Muster
2023년 11월 30일
EDIT: The goal is "simply" to make out of the figure top left and the figure top right ONE figure.
I know, but I am not doing that every day - I'm maybe not the best at this
% Create first figure
figure;
subplot(2,1,1);
plot(1:10);
ax1 = gca; % get handle to axes of figure
subplot(2,1,2);
plot(rand(1,100));
ax2 = gca; % get handle to axes of figure
% Create second figure
figure;
subplot(2,1,1);
histogram(randn(1,1000));
ax3 = gca;
subplot(2,1,2);
membrane

ax4 = gca;
%%%%%%%%%%
f1 = get(ax1,'children'); %get handle to all the children in the figure
f2 = get(ax2,'children');
f3 = get(ax3,'children');
f4 = get(ax4,'children');
%%%%%%%%%%
h3 = figure; %create new figure
s1 = subplot(2,2,1); %create and get handle to the subplot axes
s2 = subplot(2,2,2);
s3 = subplot(2,2,3); %create and get handle to the subplot axes
s4 = subplot(2,2,4);
copyobj(f1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(f2,s3);

copyobj(f3,s2);
copyobj(f4,s4);

In combination with
ax1=findobj(gcf,'type','axes'); % get handle to axes of figure
it works. Thank you very much!
ok good
I'm interested , how does the full code looks like now ?
Just a small modification of yours:
% Create first figure
figure;
subplot(2,1,1);
plot(1:10);
subplot(2,1,2);
plot(rand(1,100));
ax1=findobj(gcf,'type','axes'); % get handle to axes of figure
% Create second figure
figure;
subplot(2,1,1);
plot(10:-1:1);
subplot(2,1,2);
plot(rand(1,100));
ax2=findobj(gcf,'type','axes'); % get handle to axes of figure
%%%%%%%%%%
f1 = get(ax1(1),'children'); %get handle to all the children in the figure
f2 = get(ax1(2),'children');
f3 = get(ax2(1),'children');
f4 = get(ax2(2),'children');
%%%%%%%%%%
h3 = figure; %create new figure
s1 = subplot(2,2,1); %create and get handle to the subplot axes
s2 = subplot(2,2,2);
s3 = subplot(2,2,3); %create and get handle to the subplot axes
s4 = subplot(2,2,4);
copyobj(f1,s3); %copy children to new parent axes i.e. the subplot axes
copyobj(f2,s1);
copyobj(f3,s4);
copyobj(f4,s2);
excellent !
tx !!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Subplots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
