Sub-plots with multiple coordinate system (y-axes)

I want to plot these four figures having secondary y-axis (attached) as sub-plots, in 2 rows and 2 columns. My code plots only one set of data from each figure. What are the corrections to the code below to make it work for secondary y-axes?
I have problem with the y-ticks, ylabels and ylimits. I have mentioned the details in the code below. I want the Ylim[0,2] for left y-axis and Ylim[0,5] for right y-axis.
Any help will be appreciated.
Dir = 'D:\Expt_Results\'
prefix='plot_';
BinSuffix=[1:4];
% Load figures into handles
for j = 1:1:length(BinSuffix)
h(j) = openfig([Dir prefix num2str(BinSuffix(j)) '.fig'],'reuse');
ax(j) = gca;
end
% Subplot loop
Fig_dim=[13 12];
hf = figure('Units','inches','Position',[0 0 Fig_dim]);
row_count = 0;
sp_count = 0;
no_rows=2;
no_cols=2;
for i = 1:length(BinSuffix)
fig = get(ax(i),'children');
s(i) = subplot(no_rows,no_cols,i);
copyobj(fig,s(i));
sp_count = sp_count + 1;
sp(sp_count) = s(i);
end

 Accepted Answer

Try this code
BinSuffix = 1:4;
% Load figures into handles
h = gobjects(1, numel(BinSuffix));
ax = gobjects(1, numel(BinSuffix));
for j = 1:1:length(BinSuffix)
h(j) = openfig(['plot_' num2str(j)]);
ax(j) = gca;
end
%%
fig = figure;
ax_new = gobjects(1, numel(BinSuffix));
for i=1:numel(ax)
ax_new(i) = subplot(2,2,i);
yyaxis(ax_new(i), 'left');
yyaxis(ax(i), 'left');
LineLeft = ax(i).Children;
copyobj(LineLeft, ax_new(i));
ax_new(i).YLim = [0 2];
yyaxis(ax_new(i), 'right');
yyaxis(ax(i), 'right');
LineRight = ax(i).Children;
copyobj(LineRight, ax_new(i));
ax_new(i).YLim = [0 5];
end

4 Comments

Thanks you so much.
How can, I add the y-axes labels? y1 on left y-axis of subplots 1 and 3, and y2 on right y-axis of sub-plots 2 and 4.
Left y-axis: I want y-ticks but and no y-tick labels for sub-plots 2 and 4.
Right y-axis: I want y-ticks and no y-tick labels on the the right y-axes of subplots 1 and 3.
Thanks, in advance.
Try this
BinSuffix = 1:4;
% Load figures into handles
h = gobjects(1, numel(BinSuffix));
ax = gobjects(1, numel(BinSuffix));
for j = 1:1:length(BinSuffix)
h(j) = openfig(['plot_' num2str(j)]);
ax(j) = gca;
end
%%
fig = figure;
ax_new = gobjects(1, numel(BinSuffix));
for i=1:numel(ax)
ax_new(i) = subplot(2,2,i);
yyaxis(ax_new(i), 'left');
yyaxis(ax(i), 'left');
LineLeft = ax(i).Children;
copyobj(LineLeft, ax_new(i));
ax_new(i).YLim = [0 2];
yyaxis(ax_new(i), 'right');
yyaxis(ax(i), 'right');
LineRight = ax(i).Children;
copyobj(LineRight, ax_new(i));
ax_new(i).YLim = [0 5];
if any(i==[1 3])
yyaxis(ax_new(i), 'left');
ylabel(ax_new(i), 'left_label');
yyaxis(ax_new(i), 'right');
ax_new(i).YTickLabel = [];
else
yyaxis(ax_new(i), 'left');
ax_new(i).YTickLabel = [];
yyaxis(ax_new(i), 'right');
ylabel(ax_new(i), 'right_label');
end
end
You can make any change by following this general template.
SS
SS on 15 May 2020
Edited: SS on 15 May 2020
Thanks a lot.
I am glad to be of help.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

SS
on 15 May 2020

Edited:

SS
on 15 May 2020

Community Treasure Hunt

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

Start Hunting!