Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc
%Header reaading file
f1=fopen('Thermo_data.txt','r');
line1=fgetl(f1);
for i=1:4;
   line2_4= fgetl(f1);
end
%Reading temp values

for j=1:53;

    
    
tline=fgetl(f1);% %string_group of characters is in the form of  single cell array
A=strsplit(tline,' ');%to split into multiple cell arrays

species=A{1};

t_low=str2num(A{end-3});%string to number
t_high=str2num(A{end-2});
t_med=str2num(A{end-1});


%coefficients
co_eff1=fgetl(f1);
co_eff2=fgetl(f1);
co_eff3=fgetl(f1);
a=strfind(co_eff1,'E');

a1=co_eff1(1:a(1)+3);
a2=co_eff1(a(1)+4:a(2)+3);
a3=co_eff1(a(2)+4:a(3)+3);
a4=co_eff1(a(3)+4:a(4)+3);
a5=co_eff1(a(4)+4:a(5)+3);
a1=str2num(a1);
a2=str2num(a2);
a3=str2num(a3);
a4=str2num(a4);
a5=str2num(a5);

a6=co_eff2(1:a(1)+3);
a7=co_eff2(a(1)+4:a(2)+3);
a8=co_eff2(a(2)+4:a(3)+3);
a9=co_eff2(a(3)+4:a(4)+3);
a10=co_eff2(a(4)+4:a(5)+3);
a6=str2num(a6);
a7=str2num(a7);
a8=str2num(a8);
a9=str2num(a9);
a10=str2num(a10);

a11=co_eff3(1:a(1)+3);
a12=co_eff3(a(1)+4:a(2)+3);
a13=co_eff3(a(2)+4:a(3)+3);
a14=co_eff3(a(3)+4:a(4)+3);

a11=str2num(a11);
a12=str2num(a12);
a13=str2num(a13);
a14=str2num(a14);

% To calculate enthalpy(h),entropy(s),specific heat(cp)



temp=linspace(t_low,t_high,500);
R=8.314;
for i=1:length(temp);
   
   if temp(i)<=t_med


 cp(i)=R*(a8+(a9*temp(i))+(a10*temp(i)^2)+(a11*temp(i)^3)+(a12*temp(i)^4));
 h(i)=R*temp(i)*(a8+(a9*temp(i)/2)+(a10*temp(i)^2/3)+(a11*temp(i)^3/4)+(a12*temp(i)^4/5)+(a13/temp(i)));
 s(i)=R*(a8*log(temp(i))+(a9*temp(i))+(a10*temp(i)^2/2)+(a11*temp(i)^3/3)+(a12*temp(i)^4/4)+a14);

      
else
      
cp(i)=R*(a1+(a2*temp(i))+(a3*temp(i).^2)+(a4*temp(i).^3)+(a4*temp(i).^4));%high temp coeffients
h(i)=R*temp(i)*(a1+(a2*temp(i)/2)+(a3*temp(i)^2/3)+(a4*temp(i)^3/4)+(a5*temp(i)^4/5)+(a6/temp(i)));
s(i)=R*((a1*log(temp(i)))+(a2*temp(i))+(a3*temp(i).^2/2)+(a4*temp(i)^3/3)+(a5*temp(i)^4/4)+a7);

   end

end

figure(1);
plot(temp,cp,'linewidth',2);
xlabel('temp');
ylabel('Specific heat');
title('Relation b/w temp & specific heat')
text()
figure(2);
plot(temp,h,'linewidth',2);
xlabel('temp');
ylabel('Enthalpy');
title('Relation b/w temp & Enthalpy')
figure(3);
plot(temp,s,'linewidth',1);
xlabel('temp');
ylabel('Entropy');
title('Relation b/w temp & Entropy')

W=Molecular_weight(species)
disp(W)





end



function[W]=Molecular_weight(species)

Atomic_names=['H','C','O','N','Ar']
Atomic_weights=[1.0078,12.0107,15.999,14.0067,39.948]
W=0
%comparision b/w species & atomic names
for i=1:length(species)
    for j=1:length(Atomic_names)
        if species(i)==Atomic_names(j)
            W=W+Atomic_weights(j)
            position=(j)
            
            

        end
    end
    n=str2num(species(i))
    if  n>=1
        W=W+Atomic_weights(position)*(n-1)
    end
end

fprintf('molecular_weight of %s=%f',species,W)
disp(' ')
end
        



Explanation:

file writing:

close all
clear all
clc
file open:
fopen('Skill_lync.txt')
file writing:
A=fopen('Skill_lync.txt','wt')%filename=skill_lync,%wt To write
for i=1:5
fprintf(A,'the iteration:');%here 'the iteration' repeats&print for 1to 5 times in a line
fprintf(A,'theiteration %f %f',143,789);
fprintf(A,'the iteration %f %f \n \n',212 ,322);%here its changes to new line
end
%Header information to the file
fprintf(A,'%s %s %s \n','time',' pressure','velocity');
file Reading:
A=fopen('skill_lync.txt','r');%open file with file handle
b=fgetl(A);% here we get 1st line to read
b1=fgetl(A)
b2=fgetl(A)
b3=fgetl(A)
b4=fgetl(A)
b5=fgetl(A)
b6=fgetl(A)
b7=fgetl(A)
b8=fgetl(A) etc.,
Extracting the data:
First, open the file & then read the file by fgetl ..it reads only one line ,if we waant required to be extracted we can use loop
for skipping for other lines..After reading the line, get split the string to the cell array and convert to the string number by using str2num.then after, reading the coefficients lines in order extract the cofficients of different species in the given data
after extracting get convert to the string to number ....for 1species
we read the coffiecients by no characters occupied by the given line by can found by strfind,,,,by this way we extract species of coefficeints..
The same process can be repeated for other species by for giving loop... & finding the molecular weight of species
this can find by comparision of total species with the atomic names & it also contains atomic weights
normal method for molecular weight
let consider co2
here c=1molecule
0=2molecules
m_w=1*atomicweight of c+2*atomicweight of o...
By this file parsing we can read /write any data ..
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.