
%**********************************************************************
%               Combining MATLAB and CalC codes: a demo
%       Dependence of facilitation of calcium transients (FCT) on
%                   the total buffer concentration
%
%  CalC script "FCT.par" is called with a command-line parameter
%  specifying total buffer concentration, varied within a for-loop
%
%         For detailed description of the FCT phenomenon see 
%     Matveev Sherman Zucker (2004) Biophys J 86:2691 and scripts at
%          http://web.njit.edu/~matveev/calc/BSMscripts.html
%
%                  Victor Matveev, December 17, 2012
%======================================================================
figure(2);

str = computer;                          % Check computer architecture
prog = '.\cwin67.exe FCT.par ';          % Verify name of CalC executable for your OS!

BTarray  = linspace(10, 600, 25);        % Total buffer concentration array
FCTarray = [];                           % 5-pulse facilitation array

for BT = BTarray
    
        str    = [ prog, ' ', num2str( BT ) ]; % Append command-line parameter
        [status result]  = system(str);        % Call CalC 
        FCT      = str2num(result);            % Store result in variable FCT
        FCTarray = [FCTarray, FCT];            % Append variable to array
        
        f = fopen('Ca_local.dat', 'r');        % Read trace of [Ca2+] vs time
        B = fscanf(f, '%g');                   % (produced by FCT.par script)
        fclose(f);
        A = reshape(B,2,length(B)/2);
        
        subplot(211);
        plot(A(1,:), A(2,:), 'b-', 'linewidth', 2);
        title(['[Ca^{2+}] at 80 nm from channel [Buffer]_{total}=', num2str(BT), ' FCT=', num2str(FCT)]);
        xlabel('Time (ms)'); ylabel('[Ca^{2+}] (\muM)');
        axis([0 43 0 max(A(2,:)) ]);
        
        subplot(212);
        plot(BTarray(1:length(FCTarray)), FCTarray, 'ko--');
        title('5-pulse facilitation vs. buffer concentration');
        xlabel('[Buffer]_{total} (\muM)'); ylabel('max_5 / max_1');
        axis([BTarray(1) BTarray(end) 1 max(FCTarray)]);
        
        drawnow;
 end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
