Index exceeds array bounds at position 3
Show older comments
The variable i on line 86 plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),nsd(n,k,i,j)) is said to exceed array bounds once the variable indexes to 5. I am assuming this may be related to an issue at line 43 vsine(n,counter)=ampl(5-i)*sin(2*pi*freq(FREQ)*0.001*ptime(n))+vmean(j); since any value larger than 5 here would return a 0 or negative position variable for ampl. However, if I initialize i at line 79 as for i=1:4:16 then i should be able to index to 16. Is there a logic error I'm missing? Thank you!
freq=[100 250 500 1000];
cycle=[200 80 40 20];
begin=[753 785 795 739];
cnum=[2 4 8 16];
%tth must be accounted for in ZHAD
vhold=-90;
vr=-75;
%all the above are just initial values, trial stands for which model trial
%data was used
%gives us time(ptime)
step=0.05;
ptime=0:step:1199*step;
%This gets sintrace value which is not needed. Left in just in case
for FREQ=1:4
SNUM=16;
for i=1:SNUM
%sweepnamestr=sprintf('%s%s%s', 'Trace_',int2str(9),'_',int2str(i),'_1_1'); %9 used to be 10-FREQ
sweepnamestr=sprintf('%s%s%s', 'Trace_',int2str(10-FREQ),'_',int2str(i),'_1_1');
tracetemp=eval(sweepnamestr);
trace(:,i)=tracetemp(:,2)*10^9; %store sweeps in nA
sintrace(:,i)=trace(301:1500,i);
end
init=14.175;
inipt=285;
finpt=1501;
% to get the voltage amounts (vsine)
ampl=[15 30 45 60];
vmean=[0 -15 -30 -45];
counter=1;
for j=1:length(vmean)
for i=1:length(ampl)
for n=1:1200
vsine(n,counter)=ampl(5-i)*sin(2*pi*freq(FREQ)*0.001*ptime(n))+vmean(j);
end
counter=counter+1;
end
end
%generate current from parameters
cycle=[200 80 40 20];
ta=AM(trial,:);
tb=BM(trial,:);
tg=GM(trial,:);
init=[1;0;0;0;0];
for t=1:length(ptime);
vv=vsine(t);
coeff=[-0.000000000001456 0.000000000012566 0.000000013391643 -0.000000133240032 -0.000033555887315 0.000473197916512 0.038026092731197];
m=aldamtrx(ta,tb,vv);
p0=expm(m*50)*init;
%ppd=p0;
pgval=polyval(coeff,vv);
%ppd = expm(m*step)*ppd;
ppd = expm(m*step)*p0;
nsd(n,k,i,j) = (ppd(5))*(vv-vr)*pgval;
end
%plot the voltage vs current
%see sinavgplot for the use of the values in the first 2 loops
for i=1:4:16
%for i=1:4
ii=(i-1)/4+1;
%ii = (i-1)+1;
for j=1:4
k=i+j;
plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),nsd(n,k,i,j))
hold on
end
%pause
hold off
title([num2str(freq(FREQ)),'Hz, ','Mean V=',num2str(vmean(ii)),'V'])
axis([-100 100 -.5 1,])
end
pause
end
5 Comments
However, if I initialize i at line 79 as for i=1:4:16 then i should be able to index to 16.
i=1:4:16
Walter Roberson
on 12 Nov 2024
You should not compute the name of the variable to be processed.
Walter Roberson
on 12 Nov 2024
for j=1:length(vmean)
for i=1:length(ampl)
for n=1:1200
After the end of this triple loop, the value of n will be the last thing that n was set to in the loops. So after these loops, n will be set to 1200.
for t=1:length(ptime);
That loop does not touch n.
for i=1:4:16
%for i=1:4
ii=(i-1)/4+1;
%ii = (i-1)+1;
for j=1:4
k=i+j;
plot(vsine(begin(FREQ):begin(FREQ)+cycle(FREQ)-1,k),nsd(n,k,i,j))
for i and for j do not touch n. But you use n in the nsd(n,k,i,j) call. n is still set to 1200. Is that what you want?
A couple of pointers:
1. This
sweepnamestr=sprintf('%s%s%s', 'Trace_',int2str(10-FREQ),'_',int2str(i),'_1_1');
would be better written like
sweepnamestr=sprintf('Trace_%d_%d_1_1',10-FREQ,i);
Example to show they are the same:
FREQ = 3;
i = 5;
sweepnamestr=sprintf('%s%s%s', 'Trace_',int2str(10-FREQ),'_',int2str(i),'_1_1')
sweepnamestr=sprintf('Trace_%d_%d_1_1',10-FREQ,i)
2. You can avoid using eval() by loading the Traces mat file into a structure (rather than loading all its variables directly into the workspace), and then using a dynamic field name to reference the relevant field of the structure. Example:
S = load('hyster_20140721_001sin100.mat');
% ...
tracetemp = S.(sweepnamestr);
Accepted Answer
More Answers (0)
Categories
Find more on Startup and Shutdown 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!
