MATLAB: Index Exceeds Number of Array Elements Error in For Loop

arrayelementserrorexceedsfor loophelpindexMATLABnumber

I keep receiving the following error on line 36 (Icom2=….): Index exceeds the number of array elements (1). I am unsure what is wrong as I have tried everything I can think of to resolve the issue.
%William D. - ME411 HWK4 - October 4, 2020
load trial1.txt
M=86; %Total Body Mass is 85kg
x1=trial1(:,1);
x2=trial1(:,3);
y1=trial1(:,2);
y2=trial1(:,4);
fs=100;
t=[1/fs:1/fs:length(trial1)/fs];
plot(t,trial1)
xlabel('Time')
ylabel('Position')
title('Trial Data')
%>>>>Combined Foot and Leg Mass<<<<
footandlegM=0.061*M;
fprintf('Combined foot and leg Mass=')
disp(footandlegM)
%>>>>Center of Mass for Combined Foot and Leg<<<<<
for i=1:length(trial1)
Lo(i)=sqrt((x1(i)-x2(i))^2+(y1(i)-y2(i))^2);
end
L=mean(Lo);
COMp=0.606*L; %Nearest Proximal
COMd=0.394*L; %Nearest Distal
fprintf('COMproximal=')
disp(COMp)
fprintf('COMdistal=')
disp(COMd)
%>>>>>Moment of Intertia - about COM<<<<<
COG=0.416*L;
Icom=(footandlegM)*COG^2;
fprintf('Icom=')
disp(Icom)
for k=1:length(Lo) %Varying With Time
COG2(k)=0.416*Lo(k);
Icom2(k)=(footandlegM)*COG(k)^2
end
%>>>>>Moment of Intertia - about knee<<<<<
P=0.735*L;
Ip=footandlegM*P^2; %Proximal Radius of Gyration
%for j=1:length(Lo)
% for c=1:length(Lo)
% PII=0.735*Lo(c);
%end

%IIp(j)=footandlegM.*(PII(j)).^2; %Inertia with time
%end
Iu=Icom+(COMp^2)*(footandlegM); %Parallel Axis Theorem
fprintf('Ip=')
disp(Ip)
fprintf('Iparallelaxis=')
disp(Iu)
%>>>>>Time and Inertia Value<<<<<
%Nobody Cares, Work Harder
%Keep Hammering

Best Answer

  • COG=0.416*L;
    GOC is a scalar.
    for k=1:length(Lo) %Varying With Time
    COG2(k)=0.416*Lo(k);
    Icom2(k)=(footandlegM)*COG(k)^2
    end
    You assign to COG2(k) in the one line. In the next line you try to index COG(k) rather than the COG2(k) that you just wrote to. But COG is a scalar.