How to use hold command with Plotyy and Subplot?

3 views (last 30 days)
Dear Matlab Users,
I am facing a problem on applying hold command along with both Plotyy and subplot. Here, I am providng my code and attached my sample data for your convenience. Here, the subplot consists of 4 plotyy figures with 2*2 grid, and each plotyy fig. contains 4 plots with 2 at the left Y-axis, 1 at the right Y-axis and one hline plot for the threshold value. For adding 2 plots at the left Y-axis, I am using hold (ax1(1)), hold(ax2(1)), hold(ax3(1)), and hold(ax4(1)) in the subplot 1, 2, 3, and 4 respectively. When, I am running the attached code, it is showing an error on handle axis.
Please help me to know where I am going wrong in the attached code.
Please find the attached sample data and code matfile.
Thank you for your time.

Accepted Answer

dpb
dpb on 16 Jan 2020
Ver 2
[y,t]=xlsread('Sample Data.xlsx');
t=datetime(t(2:end,1),'inputformat','MM/dd/uuuu','format','MMM yy');
mos=(year(t(end))-year(t(1)))*12+1+6*(month(t(end))>6);
ttk=datetime(year(t(1)),1:6:mos,1,'format','MMM yy');
for i=1:4
hAx(i)=subplot(2,2,i);
hL{i,1}=plot(t,y(:,1:2),'linewidth',1.5);
hL{i,1}=[hL{i,1}; ...
yline(0.15,'m','GHE = 0.5', ...
'linewidth',2.5, ...
'labelHorizontalAlign','center')];
xticks(ttk)
hAx(i).XTickLabelRotation=90;
hAx(i).XAxis.FontSize=9;
yyaxis right
set(gca,'YColor','r')
hL{i,2}=plot(t,y(:,i+2),'r');
end
return
results in
untitled.jpg
Really too many ticks for the space with subplot() unless you're going to make the display full screen, but it's what asked for...
  5 Comments
RP
RP on 18 Jan 2020
Okay, I understood. Thank you very much for your help and suggestions.

Sign in to comment.

More Answers (1)

dpb
dpb on 15 Jan 2020
Edited: dpb on 15 Jan 2020
Using the features TMW has given us more recently...in particular, datetime and yyaxis along with using some data organization by indexing will help immensely to simplify...the following code generated
untitled.jpg
I didn't bother to finish the rest of the annotation, etc., that should be simple enough to insert into the loop with some preliminaries to save the needed info for labels, etc.
The line handles and basic axis handles are all saved so can also make modifications outside the loop if necessary/desired.
[y,t]=xlsread('Sample Data.xlsx');
t=datetime(t(2:end,1),'inputformat','MM/dd/uuuu');
for i=1:4
hAx(i)=subplot(2,2,i);
hL{i,1}=plot(t,y(:,1:2));
yyaxis right
set(gca,'YColor','r')
hL{i,2}=plot(t,y(:,i+2),'r');
end
  3 Comments
dpb
dpb on 16 Jan 2020
"There is no base hline function that matches your calling syntax so can't replicate what that function does, precisely, anyways..."
Oh. I see R2018 release introduced yline which would work. I was using R2017b and didn't have later release installed at the time. Nevertheless, hline appears in error or a local function...

Sign in to comment.

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!