All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
EXTRACTING DATA FROM THE NASA THERMODYNAMIC DATA BY FILE PARSING IN MATLAB AIM: To extract the data from the text file and calculating the Enthalpy, Entropy and Specific Heat for the corresponding local temperature ranges of the…
Harish Kumar
updated on 18 Dec 2020
EXTRACTING DATA FROM THE NASA THERMODYNAMIC DATA BY FILE PARSING IN MATLAB
AIM:
To extract the data from the text file and calculating the Enthalpy, Entropy and Specific Heat for the corresponding local temperature ranges of the species. Molecular mass of the corresponding species will be displayed in the command window.
CODE:
Main Program:
%PROGRAM FOR FILE PARSING AND RETRIVEING THE USEFUL INFORMATION
%RESETTING THE MATLAB WINDOW
clear all
close all
clc
%ASSIGNING THE UNIVERSAL GAS CONSTANT
R=8.314;
%READING THE DATA FILE
f1=fopen('THERMO.dat','r');
fgetl(f1);
temp_line=fgetl(f1);
temp_str=strsplit(temp_line,' ');
%READING THE GLOBAL TEMPERATURE RANGE
for i=2:length(temp_str)%i started from 2 to ignore the space in the 2nd line of the file
if i==2
temp_val=str2num(temp_str{i});
global_low_temp=temp_val;
else
if i==3
temp_val=str2num(temp_str{i});
global_mid_temp=temp_val;
else
temp_val=str2num(temp_str{i});
global_high_temp=temp_val;
end
end
end
%SKIPPING THE HEADER LINES
fgetl(f1);
fgetl(f1);
fgetl(f1);
%TO OPEN FILE TO WRITE THE MOLECULE MASS VALUES
f2=fopen('molecular_mass.txt','w');
for j=1:1000
%READING THE HEADER LINE OF THE COMPONENT
data_line=fgetl(f1);
if strcmp(data_line,'END')==1
disp('END OF DATA');
break;
end
temp_for_plot=0;
cp=0;
s=0;
h=0;
header_data=strsplit(data_line,' ');
element_name=header_data{1};
local_temp=[str2num(header_data{end-3}),str2num(header_data{end-2}),str2num(header_data{end-1})];
%READING THE COEFFICIENTS LINE FROM THE DATA FILE
coeffs=fgetl(f1);
coeffs_1=getelements(coeffs);
coeffs=fgetl(f1);
coeffs_2=getelements(coeffs);
coeffs=fgetl(f1);
coeffs_3=getelements(coeffs);
%WRITING THE COEFFICIENTS IN A SINGLE ARRAY
%Writing the first five values
for k=1:length(coeffs_1)
b(k)=coeffs_1(k);
end
%Writing the Second five values
i=1;
for k=6:10
b(k)=coeffs_2(i);
i=i+1;
end
%Writing the last four values
i=1;
for k=11:14
b(k)=coeffs_3(i);
i=i+1;
end
%SET OF CODES TO CALCULATE Cp,S,H FOR THE PARTICULAR ELEMENT
z=1;
for t=local_temp(1):local_temp(2)
if t<=local_temp(3)
temp=t;
a=[b(1),b(2),b(3),b(4),b(5),b(6),b(7)];
else
temp=t;
a=[b(8),b(9),b(10),b(11),b(12),b(13),b(14)];
end
cp(z)=((a(1)+(a(2)*temp)+(a(3)*temp^2)+(a(4)*temp^3)+(a(5)*temp^4))*R);
h(z)=(((a(1))+((a(2)*temp)/2)+((a(3)*temp^2)/3)+((a(4)*temp^3)/4)+((a(5)*temp^4)/5)+(a(6)/temp))*R*temp);
s(z)=((a(1)*log(temp))+(a(2)*temp)+((a(3)*temp^2)/2)+((a(4)*temp^3)/3)+((a(5)*temp^4)/4)+a(7));
temp_for_plot(z)=temp;
z=z+1;
end
%CODE TO PLOT THE Cp,S,H Vs TEMPERATURE RANGE FOR EVERY ELEMENT AND SAVING THEM IN CORRESPONDING FOLDER
mkdir('H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing',element_name);
cd(['H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing\',element_name]);
figure()
plot(temp_for_plot,cp,'linewidth',2);
title('Temperature Vs Specific Heat for ',element_name);
xlabel('Temperature[K]');
ylabel('Specific Heat[J Kg-1 K-1]');
saveas(gcf,'Temperature Vs Specific Heat.png');
figure()
plot(temp_for_plot,h,'linewidth',2);
title('Temperature Vs Enthalpy for ',element_name);
xlabel('Temperature[K]');
ylabel('Enthalpy[J]');
saveas(gcf,'Temperature Vs Enthalpy.png');
figure()
plot(temp_for_plot,s,'linewidth',2);
title('Temperature Vs Entropy for ',element_name);
xlabel('Temperature[K]');
ylabel('Entropy[J K-1]');
saveas(gcf,'Temperature Vs Entropy.png');
cd('H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing\')
%TO CLOSE THE FIGURE WINDOWS
close all
%FUNCTION TO GET THE MOLECULAR MASS OF THE ELEMENT
molecular_mass=getmolecularmass(element_name);
fprintf('Molecular Mass of %s is %f\n',element_name,molecular_mass);
fprintf(f2,'Molecular Mass of %s is %f\n',element_name,molecular_mass);
molecular_mass=0;
end
%TO CLOSE THE FILES OPENED DURING THE CODE EXECUTION
fclose(f1);
fclose(f2);
Sub Program-1:
function coefficients=getelements(coeffs)
k=findstr(coeffs,'E');
for i=1:length(k)
if i==1
a{i}=coeffs(1:k(i)+3);
else
a{i}=coeffs(k(i-1)+4:k(i)+3);
end
b(i)=str2num(a{i});
coefficients=b;
end
end
Sub Program-2:
function molecular_mass=getmolecularmass(element_name)
molecular_mass=0;
elements=['N' 'O' 'C' 'H' 'S' 'AR'];
mass=[14 16 12 1 32 40];
for A=1:length(element_name)
for B=1:length(elements)
if strcmp(element_name(1),elements(6))==1
molecular_mass=mass(6);
break;
else
if strcmp(element_name(A),elements(B))==1
molecular_mass=molecular_mass+mass(B);
mass_of_element=mass(B);
end
end
end
D=str2num(element_name(A));
if D~=1
molecular_mass=molecular_mass+(mass_of_element*(D-1));
end
end
end
CODE EXPLANATION:
Lines 3-6:
%RESETTING THE MATLAB WINDOW
clear all
close all
clc
These lines will reset the MATLAB window
Lines 11-15:
%READING THE DATA FILE
f1=fopen('THERMO.dat','r');
fgetl(f1);
temp_line=fgetl(f1);
temp_str=strsplit(temp_line,' ');
f1=fopen('THERMO.dat','r'); This command is used to open a file from any directory of the file. Syntax is fopen(filename,permission). Since we’re reading the file here permission used here is ‘r’. If we want to write anything into the file ‘w’ should be provided as input in place of permission. This is assigned to a variable f1.If f1 returns a positive value that means the fopen command executed successfully and if it is negative it means it is not executed.
fgetl(f1); fgetl(f1) reads a first line of the file. If we used the same command for the nth time
in the code then nth fgetl(f1) reads the nth line in the file.
temp_line=fgetl(f1); This command reads the 2nd line in the file and assigns it to a variable temp_line. The data will be stored as a array.
temp_str=strsplit(temp_line,' ');Here temp_line is splitted into a n number of strings. Here the delimiter used is space. Delimiter is limit until which the data to be considered as one string. So totally the string will be splitted into a (space-1) number of strings.
Lines 17-31:
%READING THE GLOBAL TEMPERATURE RANGE
for i=2:length(temp_str)%i started from 2 to ignore the space in the 2nd line of the file
if i==2
temp_val=str2num(temp_str{i});
global_low_temp=temp_val;
else
if i==3
temp_val=str2num(temp_str{i});
global_mid_temp=temp_val;
else
temp_val=str2num(temp_str{i});
global_high_temp=temp_val;
end
end
end
Eventhough we have splitted the data they will be still considered as a string. str2num function is used to convert the string into a number. temp_str is a array of string and to refer the position of string array we need to use the curly brackets instead of ordinary ones. for loop is here started from 2 to skip the first value of the temp_str which is a space. Here nested if else statement is used to assign the global high, mid, and low temperatures.
Lines 33-36:
%SKIPPING THE HEADER LINES
fgetl(f1);
fgetl(f1);
fgetl(f1);
These three lines will skip 4th to 6th line of the file.
Lines 38-39:
%TO OPEN FILE TO WRITE THE MOLECULE MASS VALUES
f2=fopen('molecular_mass.txt','w');
This command will open a file and allows file writing.
Line 41:
for j=1:1000
Here we had defined a for loop as we’re performing same set of operations for all the elements in the file.
Lines 43-55:
%READING THE HEADER LINE OF THE COMPONENT
data_line=fgetl(f1);
if strcmp(data_line,'END')==1
disp('END OF DATA');
break;
end
temp_for_plot=0;
cp=0;
s=0;
h=0;
header_data=strsplit(data_line,' ');
element_name=header_data{1};
local_temp=[str2num(header_data{end-3}),str2num(header_data{end-2}),str2num(header_data{end-1})];
First we’re defining a header data of first line to a variable data_line. Since we’ve defined a for loop count to 1000 numbers, we need to break the loop once the data reaches the last line for that purpose, we’ve defined a if statement where we’re comparing the first string of data_line with the word ‘END’ if the word matches then the loop will break the loop.
Variables temp_for _plot defined to store the temperature range as an array.
header_data=strsplit(data_line,' ');In this line we’re splitting the data_line with delimiter as space and storing it in variable header_data.
element_name=header_data{1}; Since we know that first string of the header line is the element name we’re storing it in a variable element_name. for every for-loop count element name changes as per the data stored in the text file.
local_temp=[str2num(header_data{end-3}),str2num(header_data{end-2}),str2num(header_data{end-1})];Second last data to fourth last data on the header line were the local temperature ranges.
Lines 57-63:
%READING THE COEFFICIENTS LINE FROM THE DATA FILE
coeffs=fgetl(f1);
coeffs_1=getelements(coeffs);
coeffs=fgetl(f1);
coeffs_2=getelements(coeffs);
coeffs=fgetl(f1);
coeffs_3=getelements(coeffs);
Here first line of the coefficient values were stored in coeffs variable. The entire line will be stored as a single array. To split the data into different coefficient values we are using a function getelements. Input for the function is the coeffs which is a single array.
function coefficients=getelements(coeffs)
k=findstr(coeffs,'E');
for i=1:length(k)
if i==1
a{i}=coeffs(1:k(i)+3);
else
a{i}=coeffs(k(i-1)+4:k(i)+3);
end
b(i)=str2num(a{i});
coefficients=b;
end
end
Here in the first line of the function we’re finding the positions of the letter ‘E’ in the array to split the data into number of coefficient values as same as the number of ‘E’ in the array, variable k will store the positions of the ‘E’ in the coeffs array. For-loop is defined and will executes for the number of time as the number of values stored in k.
First value of the coefficient will be from the first position of string to 3rd position from first positions of ‘E’. Second coefficient value will be from /4th position from the first ‘E’ position/ to /3rd position of the string from the 2nd ‘E’ position. Now they were split and will be converted to number and stored in a variable b and b is stored in a coefficients variable. Now the values in coefficients will be passed to the main program to the variable coeffs_1.
Similarly coeffs_2 and coeffs_3 values were obtained.
Lines 65-81:
%WRITING THE COEFFICIENTS IN A SINGLE ARRAY
%Writing the first five values
for k=1:length(coeffs_1)
b(k)=coeffs_1(k);
end
%Writing the Second five values
i=1;
for k=6:10
b(k)=coeffs_2(i);
i=i+1;
end
%Writing the last four values
i=1;
for k=11:14
b(k)=coeffs_3(i);
i=i+1;
end
In these line we’re writing the values stored in the variables coeffs_1,coeffs_2,coeffs_3 in a variable b as a single array.
Lines 83-98:
%SET OF CODES TO CALCULATE Cp,S,H FOR THE PARTICULAR ELEMENT
z=1;
for t=local_temp(1):local_temp(2)
if t<=local_temp(3)
temp=t;
a=[b(1),b(2),b(3),b(4),b(5),b(6),b(7)];
else
temp=t;
a=[b(8),b(9),b(10),b(11),b(12),b(13),b(14)];
end
cp(z)=((a(1)+(a(2)*temp)+(a(3)*temp^2)+(a(4)*temp^3)+(a(5)*temp^4))*R);
h(z)=(((a(1))+((a(2)*temp)/2)+((a(3)*temp^2)/3)+((a(4)*temp^3)/4)+((a(5)*temp^4)/5)+(a(6)/temp))*R*temp);
s(z)=((a(1)*log(temp))+(a(2)*temp)+((a(3)*temp^2)/2)+((a(4)*temp^3)/3)+((a(5)*temp^4)/4)+a(7));
temp_for_plot(z)=temp;
z=z+1;
end
In these set of lines we’ll be calculating the Specific Heat, Entropy and Enthalpy values for the element. Here variable z defined to store the values required to plot the graphs.
for-loop executes from low temperature value to high temperature value. First 7 coefficient values were used to the calculate the values of specific heat, entropy, and enthalpy up to the mid temperature value then the temperature values above the mid temperature values were last seven coefficient values were used. This will be decided by using if-else statement.
Now when the loop is executed the local low temperature will be given as input and it is compared with the local_temp(3) which is mid temp. low temp is less than mid temp and if conditio passes and if statement executes, and temp values will be the low temp and first seven coefficients values were assigned to the variable a. Now specific heat, enthalpy and entropy were calculated using the first seven coefficient values and the temp value is stored in a temp_for_plot variable which holds the temp values as an array. All these values were referred using the variable z and z is incremented every time when the loop executes. The above said process will be repeated upto local high temperature.
These set of statements will be executed for every element in the file.
Lines 100 -124:
%CODE TO PLOT THE Cp,S,H Vs TEMPERATURE RANGE FOR EVERY ELEMENT AND SAVING THEM IN CORRESPONDING FOLDER
mkdir('H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing',element_name);
cd(['H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing\',element_name]);
figure()
plot(temp_for_plot,cp,'linewidth',2);
title('Temperature Vs Specific Heat for ',element_name);
xlabel('Temperature[K]');
ylabel('Specific Heat[J Kg-1 K-1]');
saveas(gcf,'Temperature Vs Specific Heat.png');
figure()
plot(temp_for_plot,h,'linewidth',2);
title('Temperature Vs Enthalpy for ',element_name);
xlabel('Temperature[K]');
ylabel('Enthalpy[J]');
saveas(gcf,'Temperature Vs Enthalpy.png');
figure()
plot(temp_for_plot,s,'linewidth',2);
title('Temperature Vs Entropy for ',element_name);
xlabel('Temperature[K]');
ylabel('Entropy[J K-1]');
saveas(gcf,'Temperature Vs Entropy.png');
cd('H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing\')
%TO CLOSE THE FIGURE WINDOWS
close all
First line in this set of command mkdir('H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing',element_name); will create a folder with the element_name and in the second line we’re changing the directory to the foleder which is created using the command cd(['H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing\',element_name]);. Opening the new figure window Plotting the temperature vs specific heat in the first plot and saving it in the folder which is created using the element name. plot is saved using saveas command saveas(gcf,'Temperature Vs Specific Heat.png'); gcf refers to get current figure and file name of the plot to be given in a single quotes in this command third input argument can be given which will be the file format it is optional though. Temperature vs entropy , Temperature vs Enthalpy were also plotted and saved in the current directory using the saveas command. After saving the plots the current directory must be changed to the folder where the original code is stored using the command cd('H:\MATLAB for Mechanical Engineers\file parsing backup\File parsing\').
Close all is used after returning to the current folder to close the current figures opened by MATLAB.
Line 136-130:
%FUNCTION TO GET THE MOLECULAR MASS OF THE ELEMENT
molecular_mass=getmolecularmass(element_name);
fprintf('Molecular Mass of %s is %f\n',element_name,molecular_mass);
fprintf(f2,'Molecular Mass of %s is %f\n',element_name,molecular_mass);
molecular_mass=0;
Molecular mass of the element is calculated using a function named getmolecularmass the input for the function is the element_name.
Code in the function:
function molecular_mass=getmolecularmass(element_name)
molecular_mass=0;
elements=['N' 'O' 'C' 'H' 'S' 'AR'];
mass=[14 16 12 1 32 40];
for A=1:length(element_name)
for B=1:length(elements)
if strcmp(element_name(1),elements(6))==1
molecular_mass=mass(6);
break;
else
if strcmp(element_name(A),elements(B))==1
molecular_mass=molecular_mass+mass(B);
mass_of_element=mass(B);
end
end
end
D=str2num(element_name(A));
if D~=1
molecular_mass=molecular_mass+(mass_of_element*(D-1));
end
end
end
Now we have the element_name as the input. Defining the molecular_mass with value 0. Elements will be stored as a array and their corresponding atomic mass were stored in the variable mass. For-loop is defined with loop counter variable A and executes number of times as number of characters in the element_name. second for-loop defines with the variable name B and executes as same number of times as number of characters in the elements array. Now in the if statement the first character in the element name and the sixth character in the elements will be compared if condition agrees 6th value of the mass array will be assigned as molecular_mass and ends the loop.
If the condition fails else statement will be executed in the first line of the else statement each element_name characters will be compared with all the characters in the elements array. If the statement agrees them the molecular mass will be the sum of the molecular mass and the mass of the nth element where n is the value of B. Once the for-loop with B variable ends the current element name will be converted to number and if D not equal to 1 molecular_mass will be the sum of the molecular_mass and the product of the mass of the element and D-1. Here the mass of the element is multipled with D-1 instead of D since we have mass of the element is already added to the molecular_mass for one time. The molecular_mass will be returned to the main program.
Now the molecular_mass of the element is printed in the command window and in the file which we have opened in the line 38 with writing permission using the fprintf command. After printing the molecular_mass is assigned as 0.
Lines 133-145:
%TO CLOSE THE FILES OPENED DURING THE CODE EXECUTION
fclose(f1);
fclose(f2);
These lines will close the files which we have opened during the program execution.
OUTPUT:
Folder where the files were saved:
Molecular masses printed in the command window.
Some elements shown below:
O2:
N2:
CO2:
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
DESIGN OF HOOD USING NX CAD
DESIGN OF HOOD ASSEMBLY AIM: To design the hood inner panel and to fix the striker in path of the trajectory of hinge axis. INTRODUCTION: …
06 Sep 2021 11:20 AM IST
Radar Mast & Final Assembly of Yacht
MODELLING AND ASSEMBLING THE YACHT MODEL INTRODUCTION: Yacht is a sailing vehicle or power vessel commonly used for racing, Sailing, or for pleasure. There are various kinds of yachts available based of their propelling modes and the purpose which they are used. Yachts differ from boats by size and appearances. To be characterised…
30 Aug 2021 05:24 PM IST
MODELLING, ASSEBMLING AND RENDERING AMERICAN CHOPPER USING SOLIDWORKS
DESIGN OF AMERICAN CHOPPER INTRODUCTION: In this project we will be modelling the American Chopper model from the sketch. All the parts were modelled separately using the Solidworks part modelling workbench and they will be assembled in Solidworks assembly workbench. Finally, to make the chopper model realistic some of…
06 Aug 2021 06:14 PM IST
DESIGN OF FRONT DOOR TRIM USING CATIA
DESIGN OF FRONT DOOR TRIM: AIM: To create a door trim solid part along with B side features with CLASS A surface as input. INPUTS: We have skin data for the Lower substrate, Map pocket, Bottle Holder, Arm rest to create solid model. Along with skin data we also have the Master sections of how the parts were joined together…
04 Aug 2021 03:59 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.