MATLAB: Attempted to access Tg_in(2); index out of bounds because numel(Tg_in)=1.

for loop

Hello, I am working on a project which needs matlab. I am new to matlab and have only basic information. Now I have found a paper which included a matlab code. I am trying to understand it. So when I type the code and run the simulation matlab gives me this error: "Attempted to access Tg_in(2); index out of bounds because numel(Tg_in)=1."
The for loop for this code is:
for m=1:NN %MM
rho_g(m)=354.6E0/Tg_in(m); %density(Tg_in) [kg/m^3]
Mass_input(m)=SV_in(m)/3600.0E0*Vo1ume*rho_g_STP; %Tota1 mass input [Kg/s]
mass_input_cell(m)=Mass_input(m)/num_cell; %Mass f1ow rate per cell [Kg/s]
q_input_cell(m)=mass_input_cell(m)/rho_g(m); %f1ow rate per cell [m^3/s]
u_in(m)=q_input_cell(m)/Ag; %f1ow ve1ocity [m/s]
Cair_in(m)=rho_g(m)/MOLEMASS_AIR*1.0E3;
Cnh3_in(m)=Cair_in(m)*Xnh3_in(m);
Cno_in(m)=Cair_in(m)*Xno_in(m);
Co2_in(m)=Cair_in(m)*0.08;
end
I have put NN=1000;
Can you guys help me to understand what is wrong in it?

Best Answer

  • Adi - the error message is telling you that you the code is treating the Tg_in variable as if it were an array whereas you have defined it as a scalar. So within the second iteration of the for loop, there is an attempt to access
    Tg_in(2)
    and it fails because Tg_in is just a scalar. If you have defined NN to be 1000, then Tg_in must be an array (or vector) of 1000 elements. (The same is true for SV_in too.)