Combine two MATLAB figures with two y axes
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Ameer Hamza
le 14 Mai 2020
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?
Réponse acceptée
Ameer Hamza
le 14 Mai 2020
Modifié(e) : Ameer Hamza
le 14 Mai 2020
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/293033/image.png)
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 commentaires
Belinda
le 15 Nov 2023
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?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!