Challenge

Parsing NASA Thermodynamic Data using MATLAB

Parsing a file in Matlab

Parsing a file means reading in a data stream of some sort and building an in-memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.

Aim and Introduction

NASA came up with polynomials that can be used to evaluate thermodynamic properties such as `Cp, H`, and `S`. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species, our aim is to parse the data file using Matlab and find the properties.

Objective

1. Parsing the file 'THERMO.dat' to get the coefficients

2. Calculating the molecular weight of the species

3. Calculating the `Cp, H`, and `S`

4. Plotting the results and saving the plots by creating folder as per the species name

Governing Equations

`(Cp)/R=a_1+a_2T+a_3T^2+a_4T^3+a_5T^4`

`H/(RT)=a_1+a_2T/2+a_3T^2/3+a_4T^3/4+a_5T^4/5+a_6/T`

`S/R=a_1lnT+a_2T+a_3T^2/2+a_4T^3/3+a_5T^4/4+a_7`

Explanation of the Code to Parse the file

The file THERMO.dat is opened and a while loop is formulated to read and store the file line by line. Function 'fgetl' is used to read the file line by line. The function 'feof, is used to get ending of the file and thus the loop.

clear all
close all
clc

%opening a file THERMO.dat for reading

f1=fopen('THERMO.dat','r');

%reading the complete file THERMO.dat
j=1;
i=1;
ct_element_name=1;
ct_coef=1;
while ~feof(f1)
    
    line=fgetl(f1);
    
    len=length(line);
    
    if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
        continue
    end
    
    compfile(j,(1:len))=line;
    
    j = j+1;
    
end

Collecting the coefficients and temperature for respective species

It is observed that the data is stored in a pattern for each species which is repeated after 4 lines, this pattern is used to collect the data.

Consider Line 1

At the end of the line, number one specifies that it is the first line for species O and from this line, we can take the species name which is the first character and the low, high, and mid temperatures.

Line is split using space as delimiter and data is collected based on the basis of the cell formed by the 'strsplit' function.

   if line(80)=='1'
       temp=strsplit(line,' ');
       low_temp=str2num(temp{end-3});
       high_temp=str2num(temp{end-2});
       mid_temp=str2num(temp{end-1});
       element_temp(1:3)=[low_temp high_temp mid_temp];
       name=strsplit((line(1:6)));
       element_name(ct_element_name,1)=string(name{1,1});
       element_temperature(ct_element_name,:)=element_temp;
       ct_element_name=ct_element_name+1;
    end

Considering the 2nd,3rd and 4th Line

At the end of the line, the number specifies the line number. The lines 2nd, 3rd, and 4th contain the coefficients for solving the governing equation. The first seven are the high-temperature coefficients and the remaining seven are low-temperature coefficients.

It is observed that the letter E is used after every coefficient to specify the power, hence 'E' is used as the delimiter in the 'findstr' function. Using the index of the letter 'E' each coefficient of the line is found and stored.

The same logic is applied to the 3rd and 4th lines respectively.

if line(80)=='2'
           coef2_index=findstr(line,'E');
           high_coef_1=str2num(line(1:(coef2_index(1)+3)));
           high_coef_2=str2num(line((coef2_index(1)+4):(coef2_index(2)+3)));
           high_coef_3=str2num(line((coef2_index(2)+4):(coef2_index(3)+3)));
           high_coef_4=str2num(line((coef2_index(3)+4):(coef2_index(4)+3)));
           high_coef_5=str2num(line((coef2_index(4)+4):(coef2_index(5)+3)));
           high_element_coef(1:5)=[high_coef_1  high_coef_2  high_coef_3  high_coef_4  high_coef_5];
    end
       
    if line(80)=='3'
           coef3_index=findstr(line,'E');
           high_coef_6=str2num(line(1:(coef3_index(1)+3)));
           high_coef_7=str2num(line((coef3_index(1)+4):(coef3_index(2)+3)));
           low_coef_1=str2num(line((coef3_index(2)+4):(coef3_index(3)+3)));
           low_coef_2=str2num(line((coef3_index(3)+4):(coef3_index(4)+3)));
           low_coef_3=str2num(line((coef3_index(4)+4):(coef3_index(5)+3)));
           high_element_coef(6:7)=[high_coef_6  high_coef_7 ];
           low_element_coef(1:3)=[low_coef_1 low_coef_2 low_coef_3];
    end
       
    if line(80)=='4'
           coef4_index=findstr(line,'E');
           low_coef_4=str2num(line(1:(coef2_index(1)+3)));
           low_coef_5=str2num(line((coef4_index(1)+4):(coef4_index(2)+3)));
           low_coef_6=str2num(line((coef4_index(2)+4):(coef4_index(3)+3)));
           low_coef_7=str2num(line((coef4_index(3)+4):(coef4_index(4)+3)));
           low_element_coef(4:7)=[low_coef_4 low_coef_5  low_coef_6 low_coef_7];
           coef(ct_coef,:) = [low_element_coef high_element_coef];
           ct_coef=ct_coef+1;
    end

Calculating the Molecular Mass

The weight for each element used in species is declared that is the atomic weight of oxygen is 8 amu. Now each character of the species name is compared to the element assigned with the corresponding weight. For example we have declared O=8; then we compare it with the characters of the specifies if it matches the values are assigned to the molecular weight of the element.

First, the elements are assigned with respective molecular weight

The characters in species name is compared to each element names assigned

% Calculating the molecular weight
O=8;
H=1;
C=12;
N=14;
A=40;

for k=1:length(element_name)
    
    element=char(element_name);
    loop_element=element(k,:);
    m_weight=0;       

    for index=1:length(loop_element)

        if loop_element(index)=='O'
            m_weight=m_weight+O;
        end
        
        if loop_element(index)=='H'
            m_weight=m_weight+H;
        end
        
        if loop_element(index)=='C'
            m_weight=m_weight+C;
        end
        
        if loop_element(index)=='N'
            m_weight=m_weight+N;
        end
  
        if loop_element(index)=='A'
            m_weight=m_weight+A;

Till now we have only compared the element with the alphabetical character of the species name, but the numerical values with specifies the number how many times the same element is used, for this we have used the else function in the above code. We find the preceding species character for the numerical value and the molecular weight is calculated respectively. The numerical values are stored as the characters which are converted to a number using the str2num function. One is subtracted from the obtained number as its already added to the molecular weight of the species. For example, O2 have 2 elements of oxygen. Here first 8 is added to molecular weight and one is subtracted from, that is 2-1=1 and the values are obtained is multiplied the 8 and added to molecular weight.

% Calculating the molecular weight
O=8;
H=1;
C=12;
N=14;
A=40;

for k=1:length(element_name)
    
    element=char(element_name);
    loop_element=element(k,:);
    m_weight=0;       

    for index=1:length(loop_element)

        if loop_element(index)=='O'
            m_weight=m_weight+O;
        end
        
        if loop_element(index)=='H'
            m_weight=m_weight+H;
        end
        
        if loop_element(index)=='C'
            m_weight=m_weight+C;
        end
        
        if loop_element(index)=='N'
            m_weight=m_weight+N;
        end
  
        if loop_element(index)=='A'
            m_weight=m_weight+A;
        
        else
            element_num=(1:20);
            for m=1:length(element_num)
                if(str2num(loop_element(index)))==element_num(m)
                    
                    if loop_element(index-1)=='O'
                        m_weight=m_weight+(O*((str2num(loop_element(index)))-1));
                    end
        
                    if loop_element(index-1)=='H'
                       m_weight=m_weight+(H*((str2num(loop_element(index)))-1));
                    end
        
                    if loop_element(index-1)=='C'
                        m_weight=m_weight+(C*((str2num(loop_element(index)))-1));
                    end
        
                    if loop_element(index-1)=='N'
                        m_weight=m_weight+(N*((str2num(loop_element(index)))-1));
                    end
                    
                    if loop_element(index-1)=='A'
                        m_weight=m_weight+(A*((str2num(loop_element(index)))-1));
                    else
                        m_weight=m_weight+0;
                    end
                end
                m=m+1;
            end
        end            
    end
    molecular_weight(k,1)=m_weight;
end

Calculating the `Cp, H`, and `S`

The values of `Cp, H`, and `S` are dependent on the coefficients and temperature. The coefficients are different for low-temperature range and high-temperature range based on that we have to check whether the temperature is high or low.

First, we use the linspace function to get evenly distributed values ranging from low to high temperature of the species. We formulate the for loop for length of species and use 'if' to check the values of temperature and calculate the value of `Cp, H`, and `S` respectively.

%finding the values of Cp,H and S for higher temperatures coefficients
r=8.31446261815324;
cal_loop=1;
cal_coef=1;

for cal_loop=1:length(element)
    
    temperature(cal_loop,:)=linspace(element_temperature(cal_loop,1),element_temperature(cal_loop,2),1000);

    for cal_coef=1:length(temperature)
        
        if temperature(cal_loop,cal_coef) < element_temperature(cal_loop,3)

              t=temperature(cal_loop,cal_coef);
              a1=coef(cal_loop,1);
              a2=coef(cal_loop,2);
              a3=coef(cal_loop,3);
              a4=coef(cal_loop,4);
              a5=coef(cal_loop,5);
              a6=coef(cal_loop,6);
              a7=coef(cal_loop,7);
              cp=(a1+(a2*t)+(a3*t^2)+(a4*t^3)+(a5*t^4))*r;
              h=(a1+(a2*t/2)+(a3*t^2/3)+(a4*t^3/4)+(a5*t^4/5)+a6/t)*(r*t);
              s=(a1*log(t)+a2*t+(a3*t^2)/2+(a4*t^3)/3+(a5*t^4)/4+a7)*r;
              specific_heat(cal_loop,cal_coef)=cp;
              enthalpy(cal_loop,cal_coef)=h;
              entropy(cal_loop,cal_coef)=s;
       
        else
              t=temperature(cal_loop,cal_coef);
              a1=coef(cal_loop,8);
              a2=coef(cal_loop,9);
              a3=coef(cal_loop,10);
              a4=coef(cal_loop,11);
              a5=coef(cal_loop,12);
              a6=coef(cal_loop,13);
              a7=coef(cal_loop,14);
              cp=(a1+a2*t+a3*t^2+a4*t^3+a5*t^4)*r;
              h=(a1+(a2*t/2)+(a3*t^2/3)+(a4*t^3/4)+(a5*t^4/5)+a6/t)*(r*t);
              s=(a1*log(t)+a2*t+a3*t^2/2+a4*t^3/3+a5*t^4/4+a7)*r;
              specific_heat(cal_loop,cal_coef)=cp;
              enthalpy(cal_loop,cal_coef)=h;
              entropy(cal_loop,cal_coef)=s;
        end
        cal_coef=cal_coef+1;
    end
    cal_loop=cal_loop+1;
end

Plotting the graphs and Saving plots in Species Folder

A for loop is formulated as per the length of species.

A plot is generated and subdivided into 3 plots using the subplot function. The subplot one consists of 'Specific Heat(Cp) vs Temperature, subplot two consists of 'Enthalpy(H) vs Temperature' and the third subplot consists of 'Entropy(S) vs Temperature'.

for plot_loop=1:length(element)
    
    fig_1 = figure(1);
    subplot(3,1,1);
    plot(temperature(plot_loop,:),specific_heat(plot_loop,:),'color','r');
    title(['Specific Heat(Cp) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Specific Heat (J/K kg)');
    
    subplot(3,1,2);
    plot(temperature(plot_loop,:),enthalpy(plot_loop,:),'color','g');
    title(['Enthalpy(H) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    
    subplot(3,1,3);
    plot(temperature(plot_loop,:),entropy(plot_loop,:),'color','b');
    title(['Entropy(S) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Entropy (J/K)');
    
    fig_2=figure(plot_loop+1);
    plot(temperature(plot_loop,:),specific_heat(plot_loop,:),'color','r');
    title(['Specific Heat(Cp) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Specific Heat (J/K kg)');
    
    fig_3=figure(plot_loop+2);
    plot(temperature(plot_loop,:),enthalpy(plot_loop,:),'color','g');
    title(['Enthalpy(H) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    
    fig_4=figure(plot_loop+3);
    plot(temperature(plot_loop,:),entropy(plot_loop,:),'color','b');
    title(['Entropy(S) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    

Creating the folders and saving the plots

New folders are created using the mkdir function and the current folder is changed to a newly created folder using the 'cd' function. The plot is saved by function saveas where the file name and the file type is provided for saving the plots.

After saving the plots the current folder is changed to the parent folder using the 'cd' function.

%creating new foler
    mkdir (element_name(plot_loop,:));
    
    %changing directory to new folder
    cd (element_name(plot_loop,:));
    
    %saving the files
     name_fig1=char(element_name(plot_loop,:));
     name_fig2=char(element_name(plot_loop,:));
     name_fig3=char(element_name(plot_loop,:));
     name_fig4=char(element_name(plot_loop,:));
   
    %saving the plots
    saveas(fig_1,['Specific Heat,Enthalpy and Entropy vs Temperature for ' name_fig1 '.png']);
    saveas(fig_2,['Specific Heat vs Temperature for ' name_fig2 '.png']);
    saveas(fig_3,['Enthalpy vs Temperature for ' name_fig3 '.png']);
    saveas(fig_4,['Entropy vs Temperature for ' name_fig4 '.png']);
    
    %returning back to the original directory
    cd ..;
    
    %closing all the plots
    close (1:(plot_loop+4))

Folders made after saving the files

Plot for O2

Plot for N2

Plot for CO2

Taking input (case insensitive) from user to display the plot and molecular weight

An input is taken from the user in the form of string using the input function. The input given is stored in a variable called species_name. Now the user input at species_name is compared to element_name using the strcmpi function, which compare the string irrespective of the case and returns a logical array containing the values where the input matches element name. Now the index where the input matches is found using the find function. The values at the index corresponds to the species name entered by the user and repective molecular weight and plot is displayed for the input species.

%taking input from user to display plot and molecular weight
species_name=input('species name = ','s');
index_array=strcmpi(species_name,element_name);
index=find((strcmpi(species_name,element_name))==1);

     figure();
    subplot(3,1,1);
    plot(temperature(index,:),specific_heat(index,:),'color','r');
    title(['Specific Heat(Cp) vs Temperature for ',element_name(index,:)]);
    xlabel('Temperature (K)');
    ylabel('Specific Heat (J/K kg)');
    
    subplot(3,1,2);
    plot(temperature(index,:),enthalpy(index,:),'color','g');
    title(['Enthalpy(H) vs Temperature for ',element_name(index,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    
    subplot(3,1,3);
    plot(temperature(index,:),entropy(index,:),'color','b');
    title(['Entropy(S) vs Temperature for ',element_name(index,:)]);
    xlabel('Temperature (K)');
    ylabel('Entropy (J/K)');
    
    if index_array(index)==1
        disp(strjoin(['Species Name is ' element_name(index) ' and Molecular Weight is ' num2str(molecular_weight(index))]));
    else
        disp('Invalid Species Name, Please choose from the below names');
        table(element_name);
    end

Output showing the input (case insensitive) from user to display the plot and molecular weight

Element name and corresponding molecular weight in the command window

Error Occurred

1. The index position must be entered for storing the values in a loop.

2. The data types should be matched

3. The string command will return the entire string while using index but in char it will return the character at the index

Complete Code

clear all
close all
clc

%opening a file THERMO.dat for reading

f1=fopen('THERMO.dat','r');

%reading the complete file THERMO.dat
j=1;
i=1;
ct_element_name=1;
ct_coef=1;
while ~feof(f1)
    
    line=fgetl(f1);
    
    len=length(line);
    
    if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
        continue
    end
    
    compfile(j,(1:len))=line;
    
    j = j+1;
    
end

%Collecting the required data from the file

for i=1:218
    
    line=compfile(i,:);
    
    if i== 2
        globaltemp=strsplit(line,' ');
        low_glo_temp=str2num(globaltemp{2});
        mid_glo_temp=str2num(globaltemp{3});
        high_glo_temp=str2num(globaltemp{4});
    end
    
    if line(80)=='1'
       temp=strsplit(line,' ');
       low_temp=str2num(temp{end-3});
       high_temp=str2num(temp{end-2});
       mid_temp=str2num(temp{end-1});
       element_temp(1:3)=[low_temp high_temp mid_temp];
       name=strsplit((line(1:6)));
       element_name(ct_element_name,1)=string(name{1,1});
       element_temperature(ct_element_name,:)=element_temp;
       ct_element_name=ct_element_name+1;
    end
    
    if line(80)=='2'
           coef2_index=findstr(line,'E');
           high_coef_1=str2num(line(1:(coef2_index(1)+3)));
           high_coef_2=str2num(line((coef2_index(1)+4):(coef2_index(2)+3)));
           high_coef_3=str2num(line((coef2_index(2)+4):(coef2_index(3)+3)));
           high_coef_4=str2num(line((coef2_index(3)+4):(coef2_index(4)+3)));
           high_coef_5=str2num(line((coef2_index(4)+4):(coef2_index(5)+3)));
           high_element_coef(1:5)=[high_coef_1  high_coef_2  high_coef_3  high_coef_4  high_coef_5];
    end
       
    if line(80)=='3'
           coef3_index=findstr(line,'E');
           high_coef_6=str2num(line(1:(coef3_index(1)+3)));
           high_coef_7=str2num(line((coef3_index(1)+4):(coef3_index(2)+3)));
           low_coef_1=str2num(line((coef3_index(2)+4):(coef3_index(3)+3)));
           low_coef_2=str2num(line((coef3_index(3)+4):(coef3_index(4)+3)));
           low_coef_3=str2num(line((coef3_index(4)+4):(coef3_index(5)+3)));
           high_element_coef(6:7)=[high_coef_6  high_coef_7 ];
           low_element_coef(1:3)=[low_coef_1 low_coef_2 low_coef_3];
    end
       
    if line(80)=='4'
           coef4_index=findstr(line,'E');
           low_coef_4=str2num(line(1:(coef2_index(1)+3)));
           low_coef_5=str2num(line((coef4_index(1)+4):(coef4_index(2)+3)));
           low_coef_6=str2num(line((coef4_index(2)+4):(coef4_index(3)+3)));
           low_coef_7=str2num(line((coef4_index(3)+4):(coef4_index(4)+3)));
           low_element_coef(4:7)=[low_coef_4 low_coef_5  low_coef_6 low_coef_7];
           coef(ct_coef,:) = [low_element_coef high_element_coef];
           ct_coef=ct_coef+1;
    end
  
    i = i+1;
    
end


% Calculating the molecular weight
O=8;
H=1;
C=12;
N=14;
A=40;

for k=1:length(element_name)
    
    element=char(element_name);
    loop_element=element(k,:);
    m_weight=0;       

    for index=1:length(loop_element)

        if loop_element(index)=='O'
            m_weight=m_weight+O;
        end
        
        if loop_element(index)=='H'
            m_weight=m_weight+H;
        end
        
        if loop_element(index)=='C'
            m_weight=m_weight+C;
        end
        
        if loop_element(index)=='N'
            m_weight=m_weight+N;
        end
  
        if loop_element(index)=='A'
            m_weight=m_weight+A;
        
        else
            element_num=(1:20);
            for m=1:length(element_num)
                if(str2num(loop_element(index)))==element_num(m)
                    
                    if loop_element(index-1)=='O'
                        m_weight=m_weight+(O*((str2num(loop_element(index)))-1));
                    end
        
                    if loop_element(index-1)=='H'
                       m_weight=m_weight+(H*((str2num(loop_element(index)))-1));
                    end
        
                    if loop_element(index-1)=='C'
                        m_weight=m_weight+(C*((str2num(loop_element(index)))-1));
                    end
        
                    if loop_element(index-1)=='N'
                        m_weight=m_weight+(N*((str2num(loop_element(index)))-1));
                    end
                    
                    if loop_element(index-1)=='A'
                        m_weight=m_weight+(A*((str2num(loop_element(index)))-1));
                    else
                        m_weight=m_weight+0;
                    end
                end
                m=m+1;
            end
        end            
    end
    molecular_weight(k,1)=m_weight;
end

%finding the values of Cp,H and S for higher temperatures coefficients
r=8.31446261815324;
cal_loop=1;
cal_coef=1;

for cal_loop=1:length(element)
    
    temperature(cal_loop,:)=linspace(element_temperature(cal_loop,1),element_temperature(cal_loop,2),1000);

    for cal_coef=1:length(temperature)
        
        if temperature(cal_loop,cal_coef) < element_temperature(cal_loop,3)

              t=temperature(cal_loop,cal_coef);
              a1=coef(cal_loop,1);
              a2=coef(cal_loop,2);
              a3=coef(cal_loop,3);
              a4=coef(cal_loop,4);
              a5=coef(cal_loop,5);
              a6=coef(cal_loop,6);
              a7=coef(cal_loop,7);
              cp=(a1+(a2*t)+(a3*t^2)+(a4*t^3)+(a5*t^4))*r;
              h=(a1+(a2*t/2)+(a3*t^2/3)+(a4*t^3/4)+(a5*t^4/5)+a6/t)*(r*t);
              s=(a1*log(t)+a2*t+(a3*t^2)/2+(a4*t^3)/3+(a5*t^4)/4+a7)*r;
              specific_heat(cal_loop,cal_coef)=cp;
              enthalpy(cal_loop,cal_coef)=h;
              entropy(cal_loop,cal_coef)=s;
       
        else
              t=temperature(cal_loop,cal_coef);
              a1=coef(cal_loop,8);
              a2=coef(cal_loop,9);
              a3=coef(cal_loop,10);
              a4=coef(cal_loop,11);
              a5=coef(cal_loop,12);
              a6=coef(cal_loop,13);
              a7=coef(cal_loop,14);
              cp=(a1+a2*t+a3*t^2+a4*t^3+a5*t^4)*r;
              h=(a1+(a2*t/2)+(a3*t^2/3)+(a4*t^3/4)+(a5*t^4/5)+a6/t)*(r*t);
              s=(a1*log(t)+a2*t+a3*t^2/2+a4*t^3/3+a5*t^4/4+a7)*r;
              specific_heat(cal_loop,cal_coef)=cp;
              enthalpy(cal_loop,cal_coef)=h;
              entropy(cal_loop,cal_coef)=s;
        end
        cal_coef=cal_coef+1;
    end
    cal_loop=cal_loop+1;
end

for plot_loop=1:length(element)
    
    fig_1 = figure(1);
    subplot(3,1,1);
    plot(temperature(plot_loop,:),specific_heat(plot_loop,:),'color','r');
    title(['Specific Heat(Cp) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Specific Heat (J/K kg)');
    
    subplot(3,1,2);
    plot(temperature(plot_loop,:),enthalpy(plot_loop,:),'color','g');
    title(['Enthalpy(H) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    
    subplot(3,1,3);
    plot(temperature(plot_loop,:),entropy(plot_loop,:),'color','b');
    title(['Entropy(S) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Entropy (J/K)');
    
    fig_2=figure(plot_loop+1);
    plot(temperature(plot_loop,:),specific_heat(plot_loop,:),'color','r');
    title(['Specific Heat(Cp) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Specific Heat (J/K kg)');
    
    fig_3=figure(plot_loop+2);
    plot(temperature(plot_loop,:),enthalpy(plot_loop,:),'color','g');
    title(['Enthalpy(H) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    
    fig_4=figure(plot_loop+3);
    plot(temperature(plot_loop,:),entropy(plot_loop,:),'color','b');
    title(['Entropy(S) vs Temperature for ',element_name(plot_loop,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    

    %creating new foler
    mkdir (element_name(plot_loop,:));
    
    %changing directory to new folder
    cd (element_name(plot_loop,:));
    
    %saving the files
     name_fig1=char(element_name(plot_loop,:));
     name_fig2=char(element_name(plot_loop,:));
     name_fig3=char(element_name(plot_loop,:));
     name_fig4=char(element_name(plot_loop,:));
   
    %saving the plots
    saveas(fig_1,['Specific Heat,Enthalpy and Entropy vs Temperature for ' name_fig1 '.png']);
    saveas(fig_2,['Specific Heat vs Temperature for ' name_fig2 '.png']);
    saveas(fig_3,['Enthalpy vs Temperature for ' name_fig3 '.png']);
    saveas(fig_4,['Entropy vs Temperature for ' name_fig4 '.png']);
    
    %returning back to the original directory
    cd ..;
    
    %closing all the plots
    close (1:(plot_loop+4));
    
end

%taking input from user to display plot and molecular weight
species_name=input('species name = ','s');
index_array=strcmpi(species_name,element_name);
index=find((strcmpi(species_name,element_name))==1);

     figure();
    subplot(3,1,1);
    plot(temperature(index,:),specific_heat(index,:),'color','r');
    title(['Specific Heat(Cp) vs Temperature for ',element_name(index,:)]);
    xlabel('Temperature (K)');
    ylabel('Specific Heat (J/K kg)');
    
    subplot(3,1,2);
    plot(temperature(index,:),enthalpy(index,:),'color','g');
    title(['Enthalpy(H) vs Temperature for ',element_name(index,:)]);
    xlabel('Temperature (K)');
    ylabel('Enthalpy (J)');
    
    subplot(3,1,3);
    plot(temperature(index,:),entropy(index,:),'color','b');
    title(['Entropy(S) vs Temperature for ',element_name(index,:)]);
    xlabel('Temperature (K)');
    ylabel('Entropy (J/K)');
    
    if index_array(index)==1
        disp(strjoin(['Species Name is ' element_name(index) ' and Molecular Weight is ' num2str(molecular_weight(index))]));
    else
        disp('Invalid Species Name, Please choose from the below names');
        table(element_name);
    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.