Nested function not recognizing variable assigned in larger function

19 views (last 30 days)
Two functions essentially copy-pasted from my professor, just need to pass the outputs of the main function as inputs of the subfunction. The variables "t" and "V" are outputs of my larger @IandF function, but won't pass into my subfunction @integrate at the end. (Yes, I know global variables are not optimal–I think the scope of my code is small enough for it not to be an issue.) Looking for the fastest answer possible as the assignment is due tonight and I have been working on it for HOURS (new to Matlab so this is a struggle for me)! Code below. Any help is much appreciated.
function varargout = IandF(timespan,varargin)
% setting neuron parameters (unchanged)
V_rest = -60; % resting voltage in mV
V_peak = 20; %peak voltage in mV
V_reset = -70; % reset voltage in mV
V_thresh = -50; % threshold voltage in mV
tref = 10; %refractory period in ms
R = 100; % membrane resistance in mega-ohms
C = 0.2; % membrane capacitance in nF
tau = R*C; %time constant in ms
% defining start and end times based on input timespan vector
t_beg = timespan(1);
t_end = timespan(end);
% setting global variables for specific simulation conditions
global current % current generating function
global simCond % either f (sineCurrent) or t_step (stepCurrent)
global Imax % amplitude of current
global t_change % t_on or t_off
% assigning global variables to function inputs
current = varargin{1};
simCond = varargin{2};
Imax = varargin{3};
t_change = varargin{4};
if ~exist('timespan')
error('IandF requires time vector timespan');
end
% performing integrate and fire step
% setting ODE to search for fire event with maximum step of 0.1ms
% function @andFire defined elsewhere, same as function provided in lecture
options = odeset('Events',@andFire,'MaxStep',0.1);
% initial values for loop
V = V_rest;
ts = [t_beg t_end];
t = t_beg;
while t(end) < t_end
[t_new, V_new, tsp] = ode15s(@integrate,ts,V(end),options)
t = [t; t_new];
V = [V; V_new];
if t(end) < t_end
t = [t; tspan + [eps(tspan); eps(tspan)*2; tref]];
V = [V; V_peak; V_reset; V_reset];
ts = [t(end) t_end];
end
end
% defining subfunction to integrate voltage depending on current
integrate
function dVdt = integrate(t,V)
V_rest = -60; % resting voltage in mV
R = 100; % membrane resistance in mega-ohms
C = 0.2; % membrane capacitance in nF
tau = R*C; %time constant in ms
% setting global variables for specific simulation conditions
global current % current generating function
global simCond % either f (sineCurrent) or t_step (stepCurrent)
global Imax % amplitude of current
global t_change % t_on or t_off
% generating current for each individual time value based on inputs
I = current(t,simCond,Imax);
if 0 < t < t_change
IR = I*R;
else
IR = 0;
end
dVdt = (V_rest+V-IR)/tau;
end
% assigning outputs to time vector and calculated voltage
varargout{1} = t;
varargout{2} = V;
end

Answers (1)

Walter Roberson
Walter Roberson on 29 Jan 2021
Edited: Walter Roberson on 29 Jan 2021
sub-functions need to be of the general form
function outer_function
initialize any shared variable, MUST be before inner function is defined
main code that calls inner_function is usually here but not always
function inner_function
code that uses shared variables is here
end
main code that calls inner_function could go here
end
If you are going to use sub-functions, then do not use global! Use shared-variables instead.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!