Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc

% Make directory
mkdir 'C:UsersAdminDesktopNASA3 File Parsing'

% Reading Data file NASA Thermodynamic
f1=fopen('THERMO.dat','r')
% Heading
line1=fgetl(f1);
% temperature
global_temp=fgetl(f1)
% Spliting the temperature
t=strsplit(global_temp,' ');

% Converting string cells to numbers using str2num
global_low_temp=str2num(t{2});
global_medium_temp=str2num(t{3});
global_high_temp=str2num(t{4});

% Define universal gas constant in J/mol-K
R=8.314;
fgetl(f1)
fgetl(f1)
fgetl(f1)
for i= 1:5
    gt=fgetl(f1);
    A=strsplit(gt,' ')
    Species_SP=(A{1})
    local_low_temperture=str2num(A{end-3});
    local_medium_temperture=str2num(A{end-1});
    local_high_temperture=str2num(A{end-2});
    line_1=fgetl(f1);
    a=strfind(line_1,'E');



    % Extracting higher and lower temperature coefficients
    % Extracting First 7 High temperature coefficients

    a1=str2num(line_1(1:(a(1)+3)));

    a2=str2num(line_1((a(1)+4):(a(2)+3)));

    a3=str2num(line_1((a(2)+4):(a(3)+3)));

    a4=str2num(line_1((a(3)+4):(a(4)+3)));

    a5=str2num(line_1((a(4)+4):(a(5)+3)));

    line_2=fgetl(f1);
    b=strfind(line_1,'E');

    a6=str2num(line_2(1:(b(1)+3)));

    a7=str2num(line_2((b(1)+4):(b(2)+3)));


    % Extracting Second 7 low temperature coefficients
    a8=str2num(line_2((b(2)+4):(b(3)+3)));

    a9=str2num(line_2((b(3)+4):(b(4)+3)));

    a10=str2num(line_2((b(4)+4):(b(5)+3)));

    line_3=fgetl(f1);
    c=strfind(line_1,'E');

    a11=str2num(line_3(1:(c(1)+3)));

    a12=str2num(line_3((c(1)+4):(c(2)+3)));

    a13=str2num(line_3((c(2)+4):(c(3)+3)));

    a14=str2num(line_3((c(3)+4):(c(4)+3)));

    % Temperature range
    Temp=linspace(local_low_temperture,local_high_temperture,1000);

    % calcilation
    for j=1:length(Temp)
        if Temp(j)<local_medium_temperture
            Cp=(a1+a2.*Temp+a3.*Temp.^2+a4.*Temp.^3+a5.*Temp.^4).*R
            H=(a1+(a2.*Temp)./2+(a3.*Temp.^2)/3+(a4.*Temp.^3)/4+(a5.*Temp.^4)/5+a6./Temp).*Temp.*R;
            S=(a1*log(Temp)+a2.*Temp+(a3.*Temp.^2)/2+(a4.*Temp.^3)/3+(a5.*Temp.^4)/4+a7).*R;
        else
            Cp=(a8+a9.*Temp+a10.*Temp.^2+a11.*Temp.^3+a12.*Temp.^4).*R
            H=(a8+(a9.*Temp)./2+(a10.*Temp.^2)/3+(a11.*Temp.^3)/4+(a12.*Temp.^4)/5+a13./Temp).*Temp.*R;
            S=(a8*log(Temp)+a9.*Temp+(a10.*Temp.^2)/2+(a11.*Temp.^3)/3+(a12.*Temp.^4)/4+a14).*R;
        end
    end

    % plotting(Cp,H,S,Temp,Species_SP)
    % Saving as a folder

    cur_dir=pwd;
    mkdir(Species_SP);
    cd(Species_SP);

    % Plot1 , Specific heat(Cp) vs Temperature(Temp)
    figure(1)
    plot(Temp,Cp,'LineWidth',3,'Color','b');
    xlabel('Temperture[K]')
    ylabel('Specific Heat[KJ]');
    title(sprintf('Varying Specific Heat with Temperature Range of %s',Species_SP));
    grid on
    saveas(figure(1),'Specific Heat.jpg');


    % Plot2 , Enthalpy(H) vs Temperature(Temp)
    figure(2)
    plot(Temp,H,'LineWidth',3,'Color','r');
    xlabel('Temperture[K]')
    ylabel('Enthalpy[KJ/mol]');
    title(sprintf('Varying Enthalpy with Temperature Range of %s',Species_SP));
    grid on
    saveas(figure(2),'Enthalpy.jpg');


    % Plot2 , Entropy(S) vs Temperature(Temp)
    figure(3)
    plot(Temp,S,'LineWidth',3,'Color','g');
    xlabel('Temperture[K]')
    ylabel('Entropy[KJ/mol-K]');
    title(sprintf('Varying Entropy with Temperature Range of %s',Species_SP));
    grid on
    saveas(figure(3),'Entropy.jpg');

    cd(cur_dir);

    % Molecular weight
      M = Molecular_weight(Species_SP)
      f2=fopen('Molecular mass','w')
      fprintf('%s n %s n %f n','species','Molecular mass')
      
end
fclose(f1)
fclose(f2)

function of molecular weight

% Molecular weight function
function Molecularweight = Molecular_weight(Species_SP)
% name of element - Hydrogen , Carbon, Oxygen, Nitrogen, Argon 
element_name= ['H','C','O','N','Ar'];

% define atomic weight
Atomic_weight= [1.008 12.001 15.999 14.007 39.948];
Molecularweight=0;

for i=1:length(Species_SP)
    for j= 1:length(element_name)
        if strcmp(Species_SP(i),element_name(j))
            Molecularweight= Molecularweight+ Atomic_weight(j);
            position=j;
        end
    end
   
    
    n=str2num(Species_SP(i));
    if n>1
        Molecularweight=Molecularweight+Atomic_weight(position)*(n-1);
         fprintf('molecular weight %s :',Species_SP);
fprintf('%d',Molecularweight);
disp(' ');

    end
end


Meanings of operation which is used in main code

fopen - Open a file

r - open the file for reading

fgetl - read line from file

strsplit - split the string array

str2num - convert string array to numeric array

strfind - find one string with in another

linspace - create linearly spaced vectors

fprintf - weite formatted data to file

pwd - Identify current directory

mkdir - make new directory

cd - change working directory

results

Plots of O2

Plot of N2

Plot of CO2

Error

1) Error- The first error is use THERMO.dat file. I have save THERMO file on desktop and in code i have use to upload file form matlab file ,so it will display the error because the THERMO.dat file is missing from that location.

2)Error-

3) Error-

References

https://in.mathworks.com/help/matlab/ref/fgetl.html

https://in.mathworks.com/help/matlab/ref/saveas.html

https://in.mathworks.com/help/matlab/ref/mkdir.html

https://in.mathworks.com/help/matlab/ref/fprintf.html

https://in.mathworks.com/matlabcentral/answers/17580-how-to-define-a-path-in-saveas-command

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.