Challenge

Project 1 - Parsing NASA thermodynamic data

%%
clear all
close all
clc

%% Getting the data file

fileId=fopen('THERMO.dat','r')
fgetl(fileId)                        % Reads the first line from "THERMO.dat" file

%% Reading header informations

header_line=fgetl(fileId)            % Reads the second line from "THERMO.dat" file. The "fgetl" command reads the first line of loaded file, only once
G=str2num(header_line)               % STR2NUM converts the string array in to array of numbers 

%% Extracting the global temperatures from the numerical array G

globe_low_temp=G(:,1)                     
globe_mid_temp=G(:,2)
globe_high_temp=G(:,3)

%% SKIPPING COMMENTS FROM FILE

for c=1:3                        % This portion of the code is to neglect the comment lines present after header line
    fgetl(fileId)
end

%% EXTRACTING THE SPECIES DATA 

% This "for" loop extracts the co-efficients for all the 53 species

for i=1:53                   % Since the file has 53 species, so the length is declared as "53"
    line1=fgetl(fileId);
    l=strsplit(line1); % This function splits the string of "s_line" variable by considering 'space' as default delimiter
    m=length(l);       
    sp=l(1);
    species=char(sp) % This command converts cell array stored in "elements" variable in to array of characters
    atom=['H','C','O','N','Ar','S'];
    atomic_weight=[1.008 12.011 15.999 14.007 39.948 32.065];

   %% The following are three local temperature values of all 53 species

    lo_temp=str2double(l(m-3));              % Low temperature value
    mid_temp=str2double(l(m-1));             % Mid temperature value
    high_temp=str2double(l(m-2));            % High temperature value

    %% SEPERATING EACH CO-EFFICIENT VALUES OF ALL SPECIES BASED ON THE COMMON FACTOR AVAILABLE IN EACH LINE

    line2=fgetl(fileId);               % Second line
    a=strfind(line2,'E');              % This command finds of the position of the exponential sign 'E' in each co-efficient value
    a1=str2double(line2(1:(a+3)));    % STR2DOUBLE converts the cell array values into "double data type" number
    a2=str2double(line2((a(1)+4):(a(2)+3)));
    a3=str2double(line2((a(2)+4):(a(3)+3)));
    a4=str2double(line2((a(3)+4):(a(4)+3)));
    a5=str2double(line2((a(4)+4):(a(5)+3)));

    %% THIRD LINE

    line3=fgetl(fileId);              
    b=strfind(line3,'E');
    a6=str2double(line3(1:(b(1)+3)));
    a7=str2double(line3((b(1)+4):(a(2)+3)));
    a8=str2double(line3((b(2)+4):(a(3)+3)));
    a9=str2double(line3((b(3)+4):(a(4)+3)));
    a10=str2double(line3((b(4)+4:(a(5)+3))));

    %% FOURTH LINE

    line4=fgetl(fileId);
    c=strfind(line4,'E');
    a11=str2double(line4(1:(c(1)+3)));
    a12=str2double(line4((c(1)+4):(a(2)+3)));
    a13=str2double(line4((c(2)+4):(a(3)+3)));
    a14=str2double(line4((c(3)+4):(a(4)+3)));

    %% CREATING TEMPERATURE ARRAY FOR LOWER & HIGHER TEMPERATURES

    TL=linspace(lo_temp,mid_temp,100);  % Temperature array of size 1x100 between low temperature & mid temperature

    TH=linspace(mid_temp,high_temp,100); % Temperature array of size 1x100 between mid & high temperatures

    %% DECLARING UNIVERSAL GAS CONSTANT

    R=8.314;

    %% CALCULATING SPECIFIC HEAT, ENTHALPY & ENTROPY WITH RESPECT TO LOW & HI TEMPERATURE VALUES

    for k=1:100

        % for lower temperature

        cp_low(k)=(a8+a9*TL(k)+a10*TL(k)^2+a11*TL(k)^3+a12*TL(k)^4)*R;

        h_low(k)=(a8+a9*TL(k)/2+a10*(TL(k)^2)/3+a11*(TL(k)^3)/4+a12*(TL(k)^4)/5+a13/TL(k))*R*TL(k);

        s_low(k)=(a8*log(TL(k))+a9*TL(k)+a10*(TL(k)^2)/2+a11*(TL(k)^3)/3+a12*(TL(k)^4)/4+a13)*R;

        % for higher temperature

        cp_high(k)=(a1+a2*TH(k)+a3*TH(k)^2+a4*TH(k)^3+a5*TH(k)^4)*R;

        h_high(k)=(a1+a2*TH(k)/2+a3*(TH(k)^2)/3+a4*(TH(k)^3)/4+a5*(TH(k)^4)/5+a6/TH(k))*R*TH(k);

        s_high(k)=(a1*log(TH(k))+a2*TH(k)+a3*(TH(k)^2)/2+a4*(TH(k)^3)/3+a5*(TH(k)^4)/4+a6*(TH(k)^5)/5+a7)*R;
    end

    %% CREATING SEPERATE FOLDER FOR EACH SPECIES

    nasa_file=species;  % All the 53 species are stored in the variable'nasa_file'
    mkdir(nasa_file);   % This command creates seperate folders for each and every species
    current=pwd;        % "PWD" returns the current folder of MATLAB
    cd(nasa_file);

    %% CREATING PLOT FOR SPECIFIC HEAT

    figure(1)
    plot(TL,cp_low,'linewidth',1.5,'color','b')
    hold on
    plot(TH,cp_high,'linewidth',1.5,'color','r')
    hold off
    grid on
    title('TEMPERATURE vs SPECIFIC HEAT',species)
    xlabel('TEMPERATURE');
    ylabel('SPECIFIC HEAT');
    legend('LOW TEMPERATURE','HIGH TEMPERATURE');

   %% SAVING THE PLOT AS .jpg

   fig=sprintf('Specific heat of %s.jpg',species);   %"SPRINTF - Converts data into string or character vector
   saveas(gca,fig);                                  % save the figure to specific format(.png or .jpg) inside the parent folder

   %% CREATING FIGURE FOR ENTHALPY

   figure(2)
   plot(TL,h_low,'linewidth',1.5,'color','b')
   hold on
   plot(TH,h_high,'linewidth',1.5,'color','r')
   hold off
   grid on
   title('TEMPERATURE vs ENTHALPY',species);
   xlabel('TEMPERATURE');
   ylabel('ENTHALPY');
   legend('LOW TEMPERATURE','HIGH TEMPERATURE');

   %% SAVING THE PLOT AS .jpg

   fig=sprintf('Enthalpy of %s.jpg',species);
   saveas(gca,fig);

   %% CREATING FIGURE FOR ENTROPY
   figure(3)
   plot(TL,s_low,'linewidth',1.5,'color','b');
   hold on
   plot(TH,s_high,'linewidth',1.5,'color','r')
   hold off
   grid on
   title('TEMPERATURE vs ENTROPY',species);
   xlabel('TEMPERATURE');
   ylabel('ENTROPY');
   legend('LOW TEMPERATURE','HIGH TEMPERATURE');

   %% SAVING THE PLOT AS .jpg
   fig=sprintf('Entropy of %s.jpg',species);
   saveas(gca,fig)
   cd(current)

   %% CALCULATION OF MOLECULAR WEIGHT
   atom=['H','C','O','N','Ar','S'];
   atomic_weight=[1.008 12.011 15.999 14.007 39.948 32.065];
   mw=0;
   for p=1:length(species)
       for q=1:length(atomic_weight)
           if strcmp(species(p),atom(q)) % Compare the position of each species with respect to position of atoms like H,C,Ar,O,S,N
               mw=mw+atomic_weight(q);
               position=q;
           end
       end

       %% 
       n=str2double(species(p)); 
       if n>1
           mw=mw+(atomic_weight(position)*(n-1));
       end
   end

 %% DISPLAYING MOLECLUAR WEIGHT

   fprintf('molecular weight of %s:%f\n',species,mw)  % Print the molecular weight of all 53 species

end

ERROR MESSAGE

Reason for error:- The file was not loaded properly & no proper file identifier

Debug procedure:- Then the .dat file was properly saved in the directory where the matlab script has been saved and after clicking "RUN" button the error was rectified.

Reason for error:- The value stored in the variable G was still a cell array.

Debug procedure:- The value was later converted into number and the error got rectified

Reason for error:- The script was not terminated with proper end statement

Debug procedure:- The "end" statement was added to a nested "for" loop

Reason for error:- The "str2double" command was not properly working at some places of script

Debug procedure:- "STR2DOUBLE" was later changed to "STR2NUM"

Reason for error:- Improper declaration of a variable "k"

Debug procedure:- uppercase k was changed to lower case k at one of the co-efficient

PROGRAM EXPLANATION

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.