Combine two MATLAB figures with two y axes

18 次查看(过去 30 天)
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 个评论
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