  function data_logger
%data logging program, that collects temperature and readings from the
%Lakeshore and HP multimeter respectively. Just run this (F5). When you are
%done for the day, close the figure window.
%please do not alter the source code!
delete( timerfindall )
T = timer( 'ExecutionMode', 'fixedDelay', 'period', 1, 'TasksToExecute', 60*60*3, 'userdata', strcat( 'data', filesep, datestr( now, 'ddmmmHHMM' ), '.mat' ), ...
    'StartFcn', @create_plot, 'StopFcn', @StopFcn, 'TimerFcn', @plot_data );
get( T );
start( T );
%     
function create_plot( timer_obj, caller )
figure(100); clf; set( gcf, 'name', 'close window to stop data logger' );
tmp = {get( timer_obj, 'userdata' ), ...
    [axes( 'position', [0.2 0.55 0.6 0.3], 'color', 'none', 'ycolor', 'r' ), axes( 'position', [0.2 0.55 0.6 0.3], 'yaxislocation', 'right', 'color', 'none' ), ...
    axes( 'position', [0.2 0.1 0.6 0.3], 'color', 'none', 'ycolor', 'r' ), axes( 'position', [0.2 0.1 0.6 0.3], 'yaxislocation', 'right', 'color', 'none' ) ] };
ylabel( tmp{2}(1), 'temp. (K)', 'color', 'r' );
ylabel( tmp{2}(2), 'reading (V or Amp)', 'color', 'k' );
ylabel( tmp{2}(3), 'temp. (K)', 'color', 'r' );
ylabel( tmp{2}(4), 'reading (V or Amp)', 'color', 'k' );
xlabel( tmp{2}(1), 't (sec)' );
xlabel( tmp{2}(3), 't (sec)' );
title( tmp{2}(3), 'close-up, last 40 sec' );

set( timer_obj, 'userdata', tmp );
hold( tmp{2}(1), 'on' );
hold( tmp{2}(2), 'on' );
hold( tmp{2}(3), 'on' );
hold( tmp{2}(4), 'on' );

function StopFcn( timer_obj, caller )
tmp = get( timer_obj, 'userdata' );
fprintf( 'data saved as mat file to %s\n', tmp{1} );
figure; set( gcf, 'name', tmp{1} );
load( tmp{1} );
sec = (days-days(1))*24*60*60;
subplot( 2,1,1);
plot( sec, kelvin, '.-r' )
xlabel( 't (sec)' );
ylabel( 'temperature (K)' );
subplot( 2,1,2);
plot( sec, reading, '.-k' )
xlabel( 't (sec)' );
ylabel( 'reading (V or Amp)' );

function plot_data( timer_obj, caller )
tmp = get( timer_obj, 'userdata' );
title( tmp{2}(1), datestr( now ) );
drawnow;
if exist( tmp{1} )
    load( tmp{1} )
else
    [days, kelvin, reading] = deal( [], [], [] );
end;
[days(end+1), kelvin(end+1), reading(end+1)] =get_data;
sec = (days(end)-days(1))*24*60*60;
plot( tmp{2}(1), sec, kelvin(end), 'or');
plot( tmp{2}(2), sec, reading(end), '*k' );
plot( tmp{2}(3), sec, kelvin(end), 'or');
plot( tmp{2}(4), sec, reading(end), '*k' );
xlim( tmp{2}(3), sec(end)+[-min([40, sec(end)]) 1] );
xlim( tmp{2}(4), sec(end)+[-min([40, sec(end)]) 1] );
save( tmp{1}, 'days', 'kelvin', 'reading' );

function [time, kelvin, reading] = get_data( varargin )
%gets individual data points through the GPIB interface, please do not
%alter source code!
persistent Lakeshore HPmultimeter 
if isempty( Lakeshore )
    Lakeshore = gpib('AGILENT', 7, 12);
    HPmultimeter = gpib('AGILENT', 7, 23);
    fopen(Lakeshore(1));
    fopen(HPmultimeter(1));
end
fprintf(Lakeshore, 'KRDG? B');
time = now;
kelvin = str2num( fscanf( Lakeshore ) );
reading = str2num( fscanf( HPmultimeter ) );
return;

