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

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?

2 commentaires

You will understand it after you look at this. It's a magic link that solves virtually all problems like this. After that you will discover that Tg_in is not an array.
Hi Image analyst. Sorry but I don't understand these things. New to Matlab.

Connectez-vous pour commenter.

 Réponse acceptée

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.)

7 commentaires

adi's "Answer" moved here because it's really a reply to Geoff and not an answer to the original question:
Hi, Thanks Geoff but as I said, I am new to Matlab and I don't understand what you are saying. Is there any changes you will recommend me to resolve this issue?
Where did the paper say Tg_in was supposed to come from? How did you define it? Apparently you gave it some value, though only a single value instead of an array like the rest of the code expects. Where did you assign it? You're only giving us part of your code for some reason. How about if I just say
Tg_in = rand(1, 1000000);
to just assign a bunch of random numbers? Will that work for you (I doubt it). Surely you must have some idea of what Tg_in is, or what it should be, or what it means, or how it got assigned.
Hi, Sorry for misplacing my reply,
Anyways here is the code:
%input data
[input1,input2,input3,input4,input5]=textread('input.txt','%f,%f,%f,%f,%f');
Time_in=input1; %Time input
SV_in=input2; %space ve1ocity
PPM_nh3_in=input3; %NH3 input MOLE fraction in PPM
PPM_no_in=input4; %NO input in PPM
Tg_in=input5; %input gas temp in K
Xnh3_in=PPM_nh3_in/1.0E6; %NH3 input MOLE fraction
Xno_in=PPM_no_in/1.0E6; %NO MOLE fraction
[MM, MM2]=size(input3);
MOLEMASS_AIR=28.97E0; %mo1ar mass of air [kg/kmo1]
rho_g_STP=1.293E0; %Gas density at STP
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*volume*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 am taking input from input.txt file where I have one line as follows:
2000,30000,350,175,500
So the Tg_in will be 500 degree kelvin
For complete code please check this MIT paper's Appendix D
dspace.mit.edu/bitstream/handle/1721.1/61914/707091278.pdf
Adi - if your input file has just one line of text and so Tg_in is assigned the fifth element of that row, then it is a scalar value and so NN can only be 1 as it corresponds to the number of rows (or lines) in your input file.
If you want NN to be 1000, then your input file must have 1000 rows of data.
Hi Geoff, Thanks. Now I have understood the problem. I have given the link to the complete code. Can you take a look? Also, if I have to provide the input from input.txt, then how to put those rows? I mean how to format the txt file? And instead of txt file, if I want to give input.xls where I can put 5 columns and can add upto 1000 input rows then what will be the code instead of
[input1,input2,input3,input4,input5]=textread('input.txt','%f,%f,%f,%f,%f');
Waiting for your reply. Regards,
Adi - please ask a specific question rather than writing a number of them in the same post. It is unclear what the link to the complete code is that you mentioned. As for using an Excel file, use xlsread for read the data from file.
okay. anyhow I fixed the issues! Thank a lot for all your help and time :)

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by