Code covered by the BSD License  

Highlights from
addaxis_unit

image thumbnail
from addaxis_unit by Friedhelm Steinhilber
This function adds a second y-axis to a x-y plot using a relationship between the two y-axes.

addaxis_unit_example_hFigure
function addaxis_unit_example_hFigure
%Example if handle is figure

clear 
close all

%% Artificial Data
x = 0:10;
y1 = x;
y2_ticks = [0 10 20 30 40 50 60 70 80 90 100]';

%% 1. Example Plot with common Matlab function
figure(1)
plot(x,y1)
xlabel('x')
ylabel('y1')
addaxis_unit(gcf,@f_common_Matlab,y2_ticks,'y2 = y1^2')
set(gcf,'ResizeFcn',{@addaxis_unit 'h' 'f' 'ticks2unit' 'ylabel2unit'});

%% 2. Example Plot with inline function
% inline function
f_inline = inline('sqrt(y2)','y2');

figure(2)
plot(x,y1)
xlabel('x')
ylabel('y1')
addaxis_unit(gcf,f_inline,y2_ticks,'y2 = y1^2')

%% 3. Example Plot with anonymous Matlab function
% anonymous Matlab function
f_anonymous = @(y2)sqrt(y2);

figure(3)
plot(x,y1)
xlabel('x')
ylabel('y1')
addaxis_unit(gcf,f_anonymous,y2_ticks,'y2 = y1^2')

%% 4. Example Plot with anonymous Matlab function,
% and uses ticks of first y-axis

% anonymous Matlab function
f_anonymous = @(y2)y2.^2;

y2_ticks = 'y1';
figure(4)
plot(x,y1)
xlabel('x')
ylabel('y1')
addaxis_unit(gcf,f_anonymous,y2_ticks,'y2 = y1^2')


function y1 = f_common_Matlab(y2)
%common Matlab function
y1 = sqrt(y2);

Contact us at files@mathworks.com