# How to fix: Index exceeds the number of array elements (4).

3 views (last 30 days)
SITI HASANAH on 24 Jun 2020
Answered: Urmila Rajpurohith on 30 Jun 2020
Hello, im write a coding to estimate the specific volume v of steam for the pressure range 0.01 through 10MPa and temperature range 400 degree Celcius through 1000 degree Celius using the ideal gas equation
but i got the error where it said "Index exceed the number of array elements(4) so I confuse about this error
fid = fopen('abc.txt','w');
Vact= [31.06300,40.29600,49.52700,58.75800;
6.209400,8.057700,9.904700,11.75130;
0.306610,0.401110,0.494380,0.587210;
0.073430,0.098860,0.122920,0.146530;
0.039958,0.055665,0.069856,0.083571;
0.026436,0.038378,0.048629,0.058391];
P = [0.01,0.05,1,4,7,10];
T = [673.15,873.15,1073.15,1273.15];
for i=1:6
for j=1:4
vol(i,j) = ((0.000461631).*T(j))./P(i);
end
end
% Calculate %error
for i=1:6
for j=1:4
error(i,j) = 100.0*(vol(i,j)-Vact(i,j))/Vact(i,j);
end
end

Pulkit Goel on 25 Jun 2020
You can make vol as a zeros matrix before starting that loop to remove that warning. It mostly helps with performance improvements.
vol = zeros(6,4);
You should be getting same warning with error variable. which can be removed in same way.
Stephen Cobeldick on 25 Jun 2020
You definitely should NOT name any variable error, as this shadows the very important inbuilt error function.
SITI HASANAH on 25 Jun 2020
so it i should change variable error with other variable?

Urmila Rajpurohith on 30 Jun 2020
yes,you can replace the variable error with any other variable name;
The best suggestion is not to use inbuilt function names as variable names.
To check if any variable share a name with a built-in function you can use the which function.
For example :
>> error = 2;
>> which -all error
error is a variable.
C:\Program Files\MATLAB\R2020b\toolbox\matlab\connector2\logger\+connector\+internal\Logger.p % Shadowed connector.internal.Logger method
built-in (C:\Program Files\MATLAB\R2020b\toolbox\matlab\lang\error)
Hope this helps!