MATLAB: How to create a bar plot and a regular plot together using PLOTYY in MATLAB 7.7 (R2008b)

MATLAB

I want to use PLOTYY to draw a bar plot and another plot together so that the right and left y-axes show respective y-values.

Best Answer

The PLOTYY function takes X and Y data as its inputs in MATLAB 7.7 (R2008b). To workaround the issue, try the following commands:
n = 50;
mydata=rand(1,n);
f1 = figure;
bar_h=bar(mydata); %hggroup
set(bar_h, 'edgecolor', 'none');
% Draw plotyy
f2 = figure;
[AX, H1, H2] = plotyy(1:n, max(get(bar_h, 'Ydata')), 1:n,exp(-0.05*(1:n)));
% SET AX(1) with patch objects
set(bar_h, 'parent', AX(1));
a = get(AX(1), 'children'); % a(1): HGgroup, a(2): line
delete(a(2)); % deletes line
set(AX(1) ,'Ylim', [0 max(get(bar_h, 'Ydata'))])
% SET AX(2) Line properties
line_h = get(AX(2), 'children');
set(line_h, 'LineWidth',5);
close(figure(f1)) % The first BAR plot has been moved to the second figure.