Clear Filters
Clear Filters

how to solv this issue?

4 views (last 30 days)
ramya
ramya on 10 Apr 2022
Answered: Voss on 12 Apr 2022
LastName = {'Sanchez';'Johnson';'Li';'Diaz'};
Age = [38;43;38;40];
Smoker = logical([1;0;1;0]);
Height = [71;69;64;67];
Weight = [176;163;131;119];
BloodPressure = [93; 77; 83;80];
BP=[125;135;146;150]
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure,BP)
T1=table2cell(T)
T2=T1(:,2:5)
for i=1:4
for j=1:4
properties=T2(i,j);
end
temp1=T2(i,1);
temp2=T2(i,2);
temp3=T2(i,3);
temp4=T2(i,4);
temp5=T2(i,5);
create(temp1,temp2,temp3,temp4,temp5)
end
function create(temp1,temp2,temp3,temp4,temp5) % now i explained in detail my code
disp('age is')
disp(temp1)
if height==0
disp(temp4)
elseif height==1
disp(temp5)
end
end
% % error index in posiotion exceeds array bounds index must not exceeds 4
% R = [25;50;100;250] This is part of my program which i have tried
% to explain in simple way
% L=10
% A=5
% for i=1:4
% res=R(i)
% resistivity(R,A,L)
% end
%
% function resistivity(R,A,L)
% Rho=(R*L)/A
% end
i am getting an error index exceeds array bounds
how to rectify this error?
  2 Comments
David Hill
David Hill on 12 Apr 2022
Where is your error. Code executes without error for me.
ramya
ramya on 12 Apr 2022
now plz tell me how to rectify his error?

Sign in to comment.

Accepted Answer

Voss
Voss on 12 Apr 2022
The error happens on this line:
temp5=T2(i,5);
and is caused by attempting to access an element in column 5 of T2 but T2 has only 4 columns
LastName = {'Sanchez';'Johnson';'Li';'Diaz'};
Age = [38;43;38;40];
Smoker = logical([1;0;1;0]);
Height = [71;69;64;67];
Weight = [176;163;131;119];
BloodPressure = [93; 77; 83;80];
BP=[125;135;146;150];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure,BP)
T = 4×7 table
LastName Age Smoker Height Weight BloodPressure BP ___________ ___ ______ ______ ______ _____________ ___ {'Sanchez'} 38 true 71 176 93 125 {'Johnson'} 43 false 69 163 77 135 {'Li' } 38 true 64 131 83 146 {'Diaz' } 40 false 67 119 80 150
T1=table2cell(T)
T1 = 4×7 cell array
{'Sanchez'} {[38]} {[1]} {[71]} {[176]} {[93]} {[125]} {'Johnson'} {[43]} {[0]} {[69]} {[163]} {[77]} {[135]} {'Li' } {[38]} {[1]} {[64]} {[131]} {[83]} {[146]} {'Diaz' } {[40]} {[0]} {[67]} {[119]} {[80]} {[150]}
T2=T1(:,2:5) % columns 2 through 5 of T1 means that T2 has 4 columns
T2 = 4×4 cell array
{[38]} {[1]} {[71]} {[176]} {[43]} {[0]} {[69]} {[163]} {[38]} {[1]} {[64]} {[131]} {[40]} {[0]} {[67]} {[119]}
for i=1:4
temp1=T2(i,1);
temp2=T2(i,2);
temp3=T2(i,3);
temp4=T2(i,4);
temp5=T2(i,5); % error here: T2 has only 4 columns
create(temp1,temp2,temp3,temp4,temp5)
end
Index in position 2 exceeds array bounds. Index must not exceed 4.
function create(temp1,temp2,temp3,temp4,temp5)
disp('age is')
disp(temp1)
if height==0
disp(temp4)
elseif height==1
disp(temp5)
end
end
I don't know the correct way to rectify this error since I don't know what the code is supposed to do, but perhaps T2 should be defined differently. Maybe this:
T2=T1(:,2:6) % 5 columns
or maybe this:
T2=T1(:,2:end) % all columns from T1 except the first one

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!