MATLAB: How to plot one data set with two y axes

plotplotyytwo y axes

Hi, I am trying to represent battery capacity against percentage of load covered by the capacity of the battery against the cost of the battery on one graph with two y axes. With data for battery capacity on the x axis, the left side y axis to display the percentage of load covered by the battery capacity and the second y axis on the right side to display the cost per capacity. Currently I've been using plotyy but it displays two plots and I'm only after one. I've been looking at scaling the cost axis to the percentage of load covered but I am not sure how to go about it. So far I have the following code,
%
pb = 120; % cost of battery per kWh
OfyStor = linspace(5,100,45); % percentage of the annual demand covered by the Capacity
Capacity = linspace(2,130,45); % Capacity of energy storage
cost_pb = Capacity*pb; % Total cost per battery capacity
figure(1)
[ax, hLine1, hLine2] = plotyy(Capacity, OfyStor, Capacity, cost_pb)
xlabel(ax(1), 'Capacity (kWh)');
ylabel(ax(1), '% of load stored');
ylabel(ax(2), 'Cost($)');
If anyone has any recommendation it would be greatly appreciated.

Best Answer

%
pb = 120; % cost of battery per kWh
OfyStor = linspace(5,100,45); % percentage of the annual demand covered by the Capacity
Capacity = linspace(2,130,45); % Capacity of energy storage
cost_pb = Capacity*pb; % Total cost per battery capacity
figure(1)
[ax, hLine1, hLine2] = plotyy(Capacity, OfyStor, Capacity, cost_pb)
xlabel(ax(1), 'Capacity (kWh)');
ylabel(ax(1), '% of load stored');
ylabel(ax(2), 'Cost($)');
grid on
I adjusted your code, and got two plots in one figure... Is this what you want?
Related Question