Why do i receive error message "index exceeds number of array elements" here?
1 view (last 30 days)
Show older comments
pig master
on 20 Feb 2022
Commented: Image Analyst
on 21 Feb 2022
clear
vthresh = -50;
vreset = -65;
tmax = 2;
vleak = -70;
rmem = 100;
cmem = 0.1;
delta_t = 0.0001;
iappvec = 100:100:600;
tvec = 0:delta_t:tmax;
ref_period = 2.5;
for x = 1:length(iappvec) %cycling through iapp values
vmem=zeros(1,length(tvec));
vmem(1)=vreset;
for i = 2:length(tvec) %forward euler
dqdt=(vleak+vmem(i-1))/rmem+iappvec(x);
dvdt=dqdt/cmem;
vmem(i)=vmem(i-1)+dvdt*delta_t;
spiketime=0;
if vmem>vthresh %spike
vmem=vreset;
spiketime=tvec(i);
end
if tvec(i)<spiketime+ref_period
vmem=vreset;
end
end
end
0 Comments
Accepted Answer
Image Analyst
on 20 Feb 2022
vmem is a single number, not a vector
vmem(1)=vreset; % vmem = -65
So when you do
dqdt=(vleak+vmem(i-1))/rmem+iappvec(x);
and try to reference vmem(3-1) = vmem(2), well, there is no vmem(2).
Just step through it with the debugger to prove it.
4 Comments
Walter Roberson
on 21 Feb 2022
? Where is vmem a scalar before it is assigned a vector by
vmem=zeros(1,length(tvec));
?
The fix is very likely to change
vmem=vreset;
to
vmem(i)=vreset;
Image Analyst
on 21 Feb 2022
You're right. The first time through it was a vector with 20,001 elements, all zero except for the first one which was -65. The second time through, it was a scalar with a single element/value of -65. The lack of comments makes it hard to figure out what's going on, like when/if you need to reset vmem.
More Answers (1)
Walter Roberson
on 20 Feb 2022
for x = 1:length(iappvec) %cycling through iapp values
vmem=zeros(1,length(tvec));
vmem is a vector reinitialized for each x value.
if vmem>vthresh %spike
vmem=vreset;
spiketime=tvec(i);
end
if tvec(i)<spiketime+ref_period
vmem=vreset;
end
In both of those conditions, you overwrite all of the vector vmem, making it into a scalar.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!