File_Parsing in MATLAB
File Parsing- Parsing in computer languages refers to syntactic analysis of the input data normally from a file into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation. Parsing means resolve (a sentence or file ) into its component parts and describe their syntactic roles.
File parsing in matlab helps in analysis in many fields such as big data etc.. It will help to convert and save the file data into the internal code of matlab in easy user specific requirements. We= can read the existing file or we can also create the file in a given format.
Nasa file
The given file of thermodynamic data contains 14 coefficients which will help in calculating thermodynamics properties such as enthalpy, entropy and specific heats. There are 7 coefficients for high temperatures and 7 coefficients for low temperatures.
Code
- Download the file and keep it in the same directory.
- Open the file with fopen command and save to f1 variable.
- Get the header of the file name with the fgetl command.
- Next lines are read and stored in global temperature ranges.
- Using the strsplit command to split the three given temperatures.
- Using str2num command to convert str to number for all three temperatures.
- Temperatures are stored as low , mid and high global temperature variables respectively.
- Defining the next 3 lines as the comments using the same fgetl command.
- Then a for loop used to parsing the all 53 elements into the variables.
- Then the fgetl command is used to get the local temperature ranges.
- Strsplit command is used to split the temperatures from the strings.
- Str2num is used to convert the string into number by indexing the position of elements by end command in brackets.
- Store the temperatures in low, mid and high temperatures variable name.
- Linspace command used to specify the temperature ranges
- Next 3 lines are read by the fgetl command.
- By using findstr command is used in order to find the location of letter E and store in the variable line1.
- Then all the places before this location stored in line1 are selected and three places after the second location stored in line1. This is done to capture all coefficients.
- Repeat above two steps for line 2 and line 3 to get all coefficient values.
- Different functions are used such as thermodynamics_properties and molecular_mass functions are created in separate sub-program.
- Thermodynamics_Properties function has been called and then after molecular_mass function has been called.
- mkdir command used to create a directory in the desired location.
- cd used to display the current folder. cd newFolder changes the current folder to newFolder .
- plot command used to save the image file of temperature vs thermodynamics properties.
- Giving label to each plots
- Saveas command used to save the image file of temperature vs thermodynamics properties.
- Creating a new file by fopen command
- Putting the molecular weight in the file
- Closing the file
clear all;
close all;
clc;
f1=fopen('THERMO.dat','r');
header= fgetl(f1);
global_tempreture_ranges=fgetl(f1);
temp=strsplit(global_tempreture_ranges,' ');
low_global_temp=str2num(temp{2});
mid_global_temp=str2num(temp{3});
high_global_temp=str2num(temp{4});
comment1=fgetl(f1);
comment2=fgetl(f1);
comment3=fgetl(f1);
for i =1:53
line0=fgetl(f1);
lin_0=strsplit(line0,' ');
species_name=lin_0{1};
temp_2=strfind(line0,'G');
temp_values=line0(temp_2+4:length(line0));
val=strsplit(temp_values,' ');
local_low_tempreture=str2num(val{end-3});
local_mid_tempreture=str2num(val{end-1});
local_high_tempreture=str2num(val{end-2});
tempreture_range=linspace(local_low_tempreture,local_high_tempreture,200);
line1=fgetl(f1);
lin_1=strfind(line1,'E');
coeff_1=str2double(line1(1:(lin_1(1)+3)));
coeff_2=str2double(line1((lin_1(1)+4):(lin_1(2)+3)));
coeff_3=str2double(line1((lin_1(2)+4):(lin_1(3)+3)));
coeff_4=str2double(line1((lin_1(3)+4):(lin_1(4)+3)));
coeff_5=str2double(line1((lin_1(4)+4):(lin_1(5)+3)));
line2=fgetl(f1);
lin_2=strfind(line2,'E');
coeff_6=str2double(line2(1:(lin_2(1)+3)));
coeff_7=str2double(line2((lin_2(1)+4):(lin_2(2)+3)));
coeff_8=str2double(line2((lin_2(2)+4):(lin_2(3)+3)));
coeff_9=str2double(line2((lin_2(3)+4):(lin_2(4)+3)));
coeff_10=str2double(line2((lin_2(4)+4):(lin_2(5)+3)))
line3=fgetl(f1);
lin_3=strfind(line3,'E');
coeff_11=str2double(line3(1:(lin_3(1)+3)));
coeff_12=str2double(line3((lin_3(1)+4):(lin_3(2)+3)));
coeff_13=str2double(line3((lin_3(2)+4):(lin_3(3)+3)));
coeff_14=str2double(line3((lin_3(3)+4):(lin_3(4)+3)));
%Calculatiung
R=8.314;
[cp,H,S]=thermo(R,local_mid_tempreture,coeff_1,coeff_2,coeff_3,coeff_4,coeff_5,coeff_6,coeff_7,coeff_8,coeff_9,coeff_10,coeff_11,coeff_12,coeff_13,coeff_14,tempreture_range)
molecular_mass=molecular_weight(species_name)
mkdir('D:/Documents/File_Parsing',species_name);
cd(['D:/Documents/File_Parsing/',species_name]);
figure(1)
plot(tempreture_range,cp,'linewidth',3,'color','b');
grid on
title(species_name)
xlabel('Tempreture(K)')
ylabel('Specific Heat(J/K)')
figure(2)
plot(tempreture_range,H,'linewidth',2,'color','g');
grid on
title(species_name)
xlabel('Tempreture(K)')
ylabel('Enthalpy(J/Kg')
figure(3)
plot(tempreture_range,S,'linewidth',2,'color','b');
grid on
title(species_name)
xlabel('Tempreture(K)')
ylabel('Entropy(J/K)')
fprintf('Species Name:%6s ',species_name)
saveas(figure(1),'specific_heat vs tempreture.jpg');
saveas(figure(1),'specific_heat vs tempreture.fig');
saveas(figure(2),'enthalpy vs tempreture.jpg');
saveas(figure(2),'enthalpy vs tempreture.fig');
saveas(figure(3),'entropy vs tempreture.jpg');
saveas(figure(3),'entropy vs tempreture.fig');
%saving data in folder or file
tp=fopen('NASA_DATA_PARSING.txt','w');
fprintf(tp,'%15s :%15s \n','Species_Name',species_name);
fprintf(tp,'%15s :%15f \n ', 'molecular_weight',molecular_mass);
fclose(tp);
cd('D:/Documents/File_Parsing');
end
Important command
- str2num
- str2double
- strfind
- fgetl
- fopen
- Saveas
Molecular weight function
- To calculate the molecular weight of all the species.
- Two for loops, one for length of species name and another for length of elements.
- It will calculate molecular weight for each species. Then further strcmp to compare the species with elements and calculate their molecular weight.
- String is converted to number.
function[m]=molecular_weight(species_name)
elements=['H','C','N','O','A'];
Atomic_mass=[1,12,14,16,40];
m=0;
for i =1:length(species_name)
for j= 1:length(elements)
if strcmp(species_name(i),elements(j))
m=m+Atomic_mass(j);
x=j;
end
end
n=str2double(species_name(i));
if n>1
m=m+(Atomic_mass(x)*(n-1));
end
end
end

Thermodynamic properties has been calculated for above functiom
function[cp,H,S]=thermo(R,local_mid_tempreture,coeff_1,coeff_2,coeff_3,coeff_4,coeff_5,coeff_6,coeff_7,coeff_8,coeff_9,coeff_10,coeff_11,coeff_12,coeff_13,coeff_14,tempreture_range);
cp=zeros(size(tempreture_range));
H=zeros(size(tempreture_range));
S=zeros(size(tempreture_range));
for i=1:length(tempreture_range)
if tempreture_range(i)>local_mid_tempreture
cp(i)=R*((coeff_1)+(coeff_2*tempreture_range(i))+(coeff_3*tempreture_range(i)^2)+(coeff_4*tempreture_range(i)^3)+(coeff_5*tempreture_range(i)^4));
H(i)=R*tempreture_range(i)*((coeff_1)+((coeff_2*tempreture_range(i))/2)+((coeff_3*tempreture_range(i)^2)/3)+((coeff_4*tempreture_range(i)^3)/4)+((coeff_5*tempreture_range(i)^4)/5)+(coeff_6/tempreture_range(i)));
S(i)=((log(tempreture_range(i))*(coeff_1))+(coeff_2*tempreture_range(i))+((coeff_3*tempreture_range(i)^2)/2)+((coeff_4*tempreture_range(i)^3)/3)+((coeff_5*tempreture_range(i)^4)/4)+(coeff_7))*R;
else
cp(i)=R*((coeff_8)+(coeff_9*tempreture_range(i))+(coeff_10*tempreture_range(i)^2)+(coeff_11*tempreture_range(i)^3)+(coeff_12*tempreture_range(i)^4));
H(i)=((coeff_8)+((coeff_9*tempreture_range(i))/2)+((coeff_10*tempreture_range(i)^2)/3)+((coeff_11*tempreture_range(i)^3)/4)+((coeff_12*tempreture_range(i)^4)/5)+((coeff_13/tempreture_range(i))))*R*tempreture_range(i);
S(i)=((log(tempreture_range(i))*(coeff_8))+(coeff_9*tempreture_range(i))+((coeff_10*tempreture_range(i)^2)/2)+((coeff_11*tempreture_range(i)^3)/3)+(coeff_12*tempreture_range(i)^4)/4+(coeff_14))*R;
end
end
end




