Combine two MATLAB figures with two y axes

조회 수: 18 (최근 30일)
SS
SS 2020년 5월 14일
댓글: Belinda 2023년 11월 15일
Hi. I have two MATLAB figures and I want to combine them together, meaning to show them on one plot. These two figures have two y-axes. Unfortunately, the code I am using combines only the figures based on the right Y-axes. Can someone help me with this. I am attaching the fig files.
Dir = 'D:\Original_Plots\';
prefix1='plot_1';
prefix2='plot_2';
BinSuffix={''}; % Text_followed by number ID
% Load figures
for j = 1:1:length(BinSuffix)
h1(j) = openfig([Dir prefix1 BinSuffix{j} '.fig'],'reuse');
ax1(j) = gca;
h1(j)
end
for j = 1:1:length(BinSuffix)
h2(j) = openfig([Dir prefix2 BinSuffix{j} '.fig'],'reuse');
ax2(j) = gca;
end
% Combine figures
for j = 1:1:length(BinSuffix)
hf = figure(20);
hold on;
fig1 = get(ax1(j),'children');
fig2 = get(ax2(j),'children');
s = gca;
copyobj(fig1,s);
copyobj(fig2,s);
end
% AXES LABELS
xlabel('x1,x2');hold on;
ylabel('y1'); % on left axis
hold on;
ylabel('y2'); % on right axis
savefig('2_plots');
  댓글 수: 2
Ameer Hamza
Ameer Hamza 2020년 5월 14일
Do you only have two files, or do you want to extend it to several files? Also, do you want to keep the current left and right locations of plots the same location in the new figure?
SS
SS 2020년 5월 14일
편집: SS 2020년 5월 14일
HI, thanks for your reply. I have only two files. I want to keep the same locations of the axes positions. Also, how can I adjust the 'Ylim' for both the y-axes on the left and the right?

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 14일
편집: Ameer Hamza 2020년 5월 14일
Try this code
fig1 = openfig('plot_1.fig');
fig2 = openfig('plot_2.fig');
ax1 = findobj(fig1, 'type', 'axes');
yyaxis(ax1, 'left');
line1L = ax1.Children;
yyaxis(ax1, 'right');
line1R = ax1.Children;
ax2 = findobj(fig2, 'type', 'axes');
yyaxis(ax2, 'left');
line2L = ax2.Children;
yyaxis(ax2, 'right');
line2R = ax2.Children;
%%
fig = figure;
ax = axes();
LineL = copyobj([line1L line2L], ax);
yyaxis(ax, 'right');
LineR = copyobj([line1R line2R], ax);
To change the ylimits you can do something like this
yyaxis(ax, 'left');
ax.YLim = [0 2]; % change Ylimits of left axes
yyaxis(ax, 'right');
ax.YLim = [0 2]; % change Ylimits of right axes
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2020년 5월 15일
I am glad to be of help!!
Belinda
Belinda 2023년 11월 15일
Hi, I have such a similar problem, but using this code results in such too small range of visualized data of the file "VR Acc x".
Can anybody please help me that the range can be set to a nice display of this small data range of the y-axis?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by