Project

file parsing using matlab

* report:

line 5 - this makes sure that every time the code is executed, MATLAB FILES is my current directory.
line 6 - the number species is taken as input
line 9 - depending on the number species entered the for loop runs that many times.
line 12 - here the initial atomic weight of carbon, nitrogen, hydrogen, oxygen and argon are zero. if we dont initialise these with zero, then at the end when the total molecular weight is calculated it returns an empty matrix because any one of the above atoms wouldnt have been present in the molecule.
line 13 to 17 - the atomic weight of all the atoms are given
line 20 - opens the thermo.dat file
line 21 to 31 - the while loop is used to count how many different species are there in the given data file.
line 33 - closes the data file.
line 34 - opens the data file again
line 35 to 49 - the first 4 lines of the data file are parsed. since the second line contains the global temperature, it is stored in the variable ‘temp\'
line 52 - the species name is taken as an input
line 53 to 63 - the species name is stored in ‘dirname’. a new folder is created in the name of the current species name as given by the user. this new folder is made as the current folder. the variable s2 contains are the species name for comparison. now the species name given by the user and the names already in the variable s2 are compared. strcmpi is case insensitive and only compares the size of the short variable. if it matches, then it returns 1 and if it does not match then it returns a 0. the output is stored in the variable num. num is of logical data type. it is now a 1d array which contains 1 in only one cell and the rest would be 0 for a particular iteration. now ‘find(num)’ finds and returns the position of the cell which consists of non-zero value. this is then stored in the variable ‘in’.
line 65 to 74 - the next loop starts. here the loop runs from the first species to the last species. the last species here is the one entered by the user. now the species header is stored in variable ‘species’. now we need only a set of elements in that header line. so we take the elements from column 25 to 44 and stored in variable mol. now these elements contain the info for the kinds of atoms and the number of atoms in that particular molecule. we then collect elements from 49th column to 75th column. these contain the low, mid and high temperature for that species. those temperatures are then separated by strsplit which takes the blank space as a delimiter and splits the temperatures. since these temperatures are still a string. they are converted to number using str2num command and stored in respective variables.
line 75 to 122 - the next for loop searches for the kinds of atoms and the number. it runs through each element in the string. here switch case is used and an if-elseif-else control structure is used. each case calculates the total mass contributed by that element by multiplying the atom’s atomic weight with the number of atoms. at the end of the code, all the mass contributed by individual elements are added to give the molecular weight.
line 126 to 139 - in here another for loop is used. now each of the next three lines under the particular species header is read. the coefficients are separated by finding a pattern. here the ‘E’ is the pattern. so the value of a single coefficient is the number elements before the E plus the three elements after E. the obtained data is ten converted to a number and stored in variable ‘a’. variable ‘a’ ia 3D array. it contains the 14 coefficients of the species for which the code runs. say if initially the input for number of species is given as 14, then coefficients for 14 species are stored in variable ’a’. the next four elements in that line are extracted using the for loop nested inside.
line 141 to end - now in order to plot the curves for specific heat, enthalpy and entropy, the coefficients have to be used. using for loop and if condition nested inside the for loop, it is made sure that at that time only the first 7 coefficients are stored in Variable A. now the plotting is done using the plot command. the formulae for cp,h,and s is given in another function. the function is invoked in the main program.
the first row of the plot contains the graphs plotted with temperature in the low temperature range. the second row contains the high temperature graphs. in the next for loop, the next 7 coefficients are extracted. the MOLW variable contains the molecular weight value for that particular species in the iteration. the molecular weight is printed in the command window. now the current folder is changed to old folder which is MATLAB FILES.
* plots:
oxygen:
\"\"

nitrogen:

\"\"

carbon dioxide

\"\"

* screenshots:

\"\"

* folder screenshot:

\"\"

* function:

function [CP,H,S] = file_parsing_calc(A,T)
R = 8.314;
CP = (A(1) + A(2).*(T/2) + A(3).*(T.^2) + A(4).*(T.^3) + A(5).*(T.^4))*R;
H = (A(1) + A(2).*(T./2) + A(3).*(T.^2)./3 + A(4).*(T.^3)./4 + A(5).*(T.^4)./5 + A(6)./T).*R.*T;
S = (A(1).*(log(T)) + A(2).*T + A(3).*(T.^2)./2 + A(4).*(T.^3)./3 + A(5).*(T.^4)./4 + A(7)).*R;

*main program:

%file parsing
close all;
clear all;
clc;
cd(\'/Users/Ritvii/Desktop/MATLAB FILES\'); 
no=input(\'mention the no. of species:\');


for p=1:no


WO=0; WHY=0; WC=0; WN=0; WA=0;
C = 12.0107;
HY = 1.00794;
O = 15.9994;
N = 14.0067;
AR = 39.948;
i=1;
ct=0;
fid1 = fopen(\'THERMO.dat\',\'r\');
while i<1000
    
    i=i+1;
    ct=ct+1;
    b = fgetl(fid1);
    c = strfind(b,\'END\');
    if(c==1)
        break;
    end
    
end
num_of_species = (ct-6)/4;
fclose(fid1);    
fid = fopen(\'THERMO.dat\',\'r\');
for i=1:4;
    if(i==2)
        tline = fgetl(fid);
        tline1 = strsplit(tline,\' \');
        for j=2:length(tline1)
            temp(j) = str2num(tline1{j});
        end
        fgetl(fid);
        fgetl(fid);
    end
    if(i==2)
        break;
    end
      fgetl(fid);
end

fgetl(fid);
s1 =input(\'Mention the species:\',\'s\');
dirname = s1;
mkdir(dirname);
cd(dirname);
s2 = {\'o\',\'o2\',\'h\',\'h2\',\'oh\',\'h2o\',\'ho2\',\'h2o2\',\'c\',\'ch\',\'ch2\',\'ch2(s)\',...
    \'ch3\',\'ch4\',\'co\',\'co2\',\'hco\',\'ch2o\',\'ch2oh\',\'ch3o\',\'ch3oh\',\'c2h\',...
    \'c2h2\',\'c2h3\',\'c2h4\',\'c2h5\',\'c2h6\',\'ch2co\',\'hcco\',\'hccoh\',\'h2cn\',...
    \'hcn\',\'hno\',\'n\',\'nnh\',\'n2o\',\'nh\',\'nh2\',\'nh3\',\'no\',\'no2\',\'hcno\',...
    \'hocn\',\'hnco\',\'nco\',\'cn\',\'hcnn\',\'n2\',\'ar\',\'c3h8\',\'c3h7\',\'ch3cho\',\'ch2cho\'};

num = strcmpi(s1,s2);
in = find(num);

for s =1:in
  species = fgetl(fid);
  mol = species(1,25:44);
  templine= species(1,49:75);
  temp1= strsplit(templine,\' \');
  low_temp= str2num(temp1{1});
  high_temp= str2num(temp1{2});
  mid_temp= str2num(temp1{3});
  T1 = low_temp:mid_temp;
  T2 = mid_temp:high_temp;
  for e=1:length(mol)
      switch(mol(e))
          case \'C\'
              if(e==1)
                WC = C*(str2num(mol(5)));
              elseif(e==6)
                  WC = C*(str2num(mol(10)));
              elseif(e==11)
                  WC = C*(str2num(mol(15)));
              else
                  WC = C*(str2num(mol(20)));
              end
          case \'H\'
              if(e==1)
                WHY = HY*(str2num(mol(5)));
              elseif(e==6)
                  WHY = HY*(str2num(mol(10)));
              elseif(e==11)
                  WHY = HY*(str2num(mol(15)));
              else
                  WHY = HY*(str2num(mol(20)));
              end
          case \'O\'
              if(e==1)
                WO= O*(str2num(mol(5)));
               
              elseif(e==6)
                  WO = O*(str2num(mol(10)));
              elseif(e==11)
                  WO = O*(str2num(mol(15)));
              else
                  WO = O*(str2num(mol(20)));
              end
          case \'N\'
              if(e==1)
                WN = N*(str2num(mol(5)));
              elseif(e==6)
                  WN = N*(str2num(mol(10)));
              elseif(e==11)
                  WN = N*(str2num(mol(15)));
              else
                  WN = N*(str2num(mol(20)));
              end
          case \'A\'
              WA = AR*(str2num(mol(5)));
          
      end
  end
      
              
  
for k = 1:3
    sline = fgetl(fid);
    Evalue = findstr(sline,\'E\');
    a1 = sline(1:Evalue(1)+3);
    a(k,1,s) = str2num(a1);
    for g=1:4
        if(k==3&g==4)
            break
        end
    a2 = sline(Evalue(g)+4:Evalue(g+1)+3);
    a(k,g+1,s) = str2num(a2);
    end
end
end 

for x=1:2
    for z=1:5
     if (x==2&&z==2)
         break
     end
 A(z,x) = a(x,z,in);
    end
end


[CP,H,S] = file_parsing_calc(A,T1);
figure(p)
subplot(2,3,1);
plot(T1,CP);
xlabel(\'temperature\');
ylabel(\'CP Values\');
title(\'CP vs low temperature\');

subplot(2,3,2);
plot(T1,H);
xlabel(\'temperature\');
ylabel(\'H Values\');
title(\'H vs low temperature\');

subplot(2,3,3);
plot(T1,S);
xlabel(\'temperature\');
ylabel(\'S Values\');
title(\'S vs low temperature\');


for x=2:3
    for z=3:5
     if (x==3&&z==5)
         break
     end
 A(z,x) = a(x,z,in);
    end
end

[CP,H,S] = file_parsing_calc(A,T2);
figure(p)
subplot(2,3,4);
plot(T2,CP);
xlabel(\'temperature\');
ylabel(\'CP Values\');
title(\'CP vs high temperature\');

subplot(2,3,5);
plot(T2,H);
xlabel(\'temperature\');
ylabel(\'H Values\');
title(\'H vs high temperature\');

subplot(2,3,6);
plot(T2,S);
xlabel(\'temperature\');
ylabel(\'S Values\');
title(\'S vs high temperature\');
saveas(p,dirname);

MOLW = WC+WHY+WN+WO+WA;
fprintf(\'the molecular weight of %s is: %d\\n\',s1,MOLW);
cd(\'/Users/Ritvii/Desktop/MATLAB FILES\');
end
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.