NASA Parsing with weight and search
######Row is the name of the cell array######
The code is divided into three parts:
- Function to get the data from THERMO.dat file and putting it in an cell array
- Code to take input from the user and searching the species, calculating result within temperature range and plotting the result. Also to save the plots produced in a perticular location. (also weight of the species is calculated, stored in variable sum)
- Code is similar to 2nd one, but instead of searching for a perticular species it plots and saves all the plots.
1st Code -
function row = NASA_Parsing()
%opening the file
f1 = fopen(\'thermo.dat\',\'r\')
%get the first line in variable a and initiation
a=fgetl(f1);
%iniitiating line counter variable
L = 1
%putting condition to end loop
while a(1) ~= \'E\'
%next line in a
a = fgetl(f1);
%looping till we reach 4th line
if L>4
%condition to avoid false entry in cell array
if a(1) == \'E\'
break
else
%putting the species name in the 1st colunmn
row{L-4,1} = strrep(a(1:10),\' \',\'\')
%putting temp values in following columns
row{L-4,2} = str2num(a(57-8:57));
row{L-4,3} = str2num(a(58:58+8));
row{L-4,4} = str2num(a(58+8+2:58+8+8+2));
%go to next line
a = fgetl(f1);
%find position of E
pos = findstr(a,\'E\');
%loop through positions of E
for x = 1:length(pos)
%putting coefficients in next columns
row{L-4,4+x} = str2num(a(pos(x)-11:pos(x)+3));
end
%same process as above
a = fgetl(f1);
pos = findstr(a,\'E\');
for x = 1:length(pos)
row{L-4,9+x} = str2num(a(pos(x)-11:pos(x)+3));
end
%same process as above
a = fgetl(f1);
pos = findstr(a,\'E\');
for x = 1:length(pos)
row{L-4,14+x} = str2num(a(pos(x)-11:pos(x)+3));
end
end
end
%increasing line counter after every 4 lines
L=L+1
end
fclose(\'all\')
end
2nd Code -
clear all
close all
clc
%using function to get row matrix
row = NASA_Parsing()
%putting value of R
R = 8.314
%initializing i
i = 0
%taking input from user
s = input(\'enter the specie name \',\'s\');
%making a cell array containing element symbol and their weight
ele = { \'H\' , 1;
\'C\' , 12;
\'N\' , 14;
\'O\' , 16 };
%putting names in a array E
E = [ele{1,1} ele{2,1} ele{3,1} ele{4,1}]
%initializing sum
sum = 0
%initializing old_Locb
old_Locb = 1
%finding the species in 1st column of cell array
for x = 1:53
p = strcmpi(s,row{x,1})
if p == 1
i = x;
break
end
end
%condition to prevent error
if i ~= 0
a = row{i,1}
%calculating wt of the species
for x=1:length(a)
[Lia,Locb] = ismember(a(x),E);
if Lia
sum = sum + ele{Locb,2};
old_Locb = Locb;
else
sum = sum - ele{old_Locb,2} + ele{old_Locb,2}*str2num(a(x));
end
end
%printing wt of species
[ num2str(sum) \' - this is the weight of the species\']
%making matrices containing temperature ranges
low_temp = row{i,2}:row{i,4};
high_temp = row{i,4}:row{i,3};
%calculating Cp H and S at low temp
Cp1 =R*((row{i,5}) + (row{i,6}*low_temp) +...
(row{i,7}*(low_temp.^2)) + (row{i,8}*(low_temp.^3)) +...
(row{i,9}*(low_temp.^4)));
H1 = low_temp.*(R*((row{i,5}) + (row{i,6}*low_temp/2) +...
(row{i,7}*(low_temp.^2)/3) + (row{i,8}*(low_temp.^3)/4) ...
+ (row{i,9}*(low_temp.^4)/5) + (row{i,10}./low_temp)));
S1 = R*((row{i,5}*log(low_temp)) + (row{i,6}*low_temp) +...
(row{i,7}*(low_temp.^2)/2) + (row{i,8}*(low_temp.^3)/3) +...
(row{i,9}*(low_temp.^4)/4) + (row{i,11}));
%calculating Cp H and S at high temp
Cp2 = R*((row{i,12}) + (row{i,13}*high_temp) +...
(row{i,14}*(high_temp.^2)) + (row{i,15}*(high_temp.^3)) +...
(row{i,16}*(high_temp.^4)));
H2 = high_temp.*(R*((row{i,12}) + (row{i,13}*high_temp/2) +...
(row{i,14}*(high_temp.^2)/3) + (row{i,15}*(high_temp.^3)/4) ...
+ (row{i,16}*(high_temp.^4)/5) + (row{i,17}./high_temp)));
S2 = R*((row{i,12}*log(high_temp)) + (row{i,13}*high_temp) +...
(row{i,14}*(high_temp.^2)/2) + (row{i,15}*(high_temp.^3)/3) +...
(row{i,16}*(high_temp.^4)/4) + (row{i,18}));
%making a new folder
status = mkdir([\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1}]);
%giving figure name etc and plotting
h = figure(\'Name\',[row{i,1} \':Cp Vs T\'],\'NumberTitle\',\'off\');
plot(low_temp,Cp1,high_temp,Cp2)
ylabel(\'Cp\')
xlabel(\'T\')
title(row{i,1})
%saving graph at perticular folder
saveas(h,[\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1} \'\\CpVsT.png\'])
%same repeated for H and S
h = figure(\'Name\',[row{i,1} \':H Vs T\'],\'NumberTitle\',\'off\');
plot(low_temp,H1,high_temp,H2)
ylabel(\'H\')
xlabel(\'T\')
title(row{i,1})
saveas(h,[\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1} \'\\HVsT.png\'])
h = figure(\'Name\',[row{i,1} \':S Vs T\'],\'NumberTitle\',\'off\');
plot(low_temp,S1,high_temp,S2)
ylabel(\'S\')
xlabel(\'T\')
title(row{i,1})
saveas(h,[\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1} \'\\SVsT.png\'])
%incase species doesn\'t exist
else
p = \'nothing found\'
end
3rd Code -
clear all
close all
clc
%using function to get row cell array
row = NASA_Parsing()
%assigning value to R
R = 8.314
%looping through all the elements in row
for i=1:53
%making matrices containing temperature ranges
low_temp = row{i,2}:row{i,4};
high_temp = row{i,4}:row{i,3};
%calculating Cp H and S at low temp
Cp1 =R*((row{i,5}) + (row{i,6}*low_temp) +...
(row{i,7}*(low_temp.^2)) + (row{i,8}*(low_temp.^3)) +...
(row{i,9}*(low_temp.^4)));
H1 = low_temp.*(R*((row{i,5}) + (row{i,6}*low_temp/2) +...
(row{i,7}*(low_temp.^2)/3) + (row{i,8}*(low_temp.^3)/4) ...
+ (row{i,9}*(low_temp.^4)/5) + (row{i,10}./low_temp)));
S1 = R*((row{i,5}*log(low_temp)) + (row{i,6}*low_temp) +...
(row{i,7}*(low_temp.^2)/2) + (row{i,8}*(low_temp.^3)/3) +...
(row{i,9}*(low_temp.^4)/4) + (row{i,11}));
%calculating Cp H and S at high temp
Cp2 = R*((row{i,12}) + (row{i,13}*high_temp) +...
(row{i,14}*(high_temp.^2)) + (row{i,15}*(high_temp.^3)) +...
(row{i,16}*(high_temp.^4)));
H2 = high_temp.*(R*((row{i,12}) + (row{i,13}*high_temp/2) +...
(row{i,14}*(high_temp.^2)/3) + (row{i,15}*(high_temp.^3)/4) ...
+ (row{i,16}*(high_temp.^4)/5) + (row{i,17}./high_temp)));
S2 = R*((row{i,12}*log(high_temp)) + (row{i,13}*high_temp) +...
(row{i,14}*(high_temp.^2)/2) + (row{i,15}*(high_temp.^3)/3) +...
(row{i,16}*(high_temp.^4)/4) + (row{i,18}));
%making a new folder
status = mkdir([\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1}])
%giving figure name etc and plotting
h = figure(\'Name\',[row{i,1} \':Cp Vs T\'],\'NumberTitle\',\'off\');
plot(low_temp,Cp1,high_temp,Cp2)
ylabel(\'Cp\')
xlabel(\'T\')
title(row{i,1})
%saving graph at perticular folder
saveas(h,[\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1} \'\\CpVsT.png\'])
%same repeated for H and S
h = figure(\'Name\',[row{i,1} \':H Vs T\'],\'NumberTitle\',\'off\');
plot(low_temp,H1,high_temp,H2)
ylabel(\'H\')
xlabel(\'T\')
title(row{i,1})
saveas(h,[\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1} \'\\HVsT.png\'])
h = figure(\'Name\',[row{i,1} \':S Vs T\'],\'NumberTitle\',\'off\');
plot(low_temp,S1,high_temp,S2)
ylabel(\'S\')
xlabel(\'T\')
title(row{i,1})
saveas(h,[\'C:\\Users\\mypc\\Desktop\\MatlabPlot\\\' row{i,1} \'\\SVsT.png\'])
end
Explanation:
######Row is the name of the cell array######
Code 1 (function) :
Firstly, we open the \'THERMO.dat\' file, in read mode. We store new line in variable \'a\', and we initialize the variable, next we start looping through the file, we get the second line in variable. We loop till the 5th, line where the data starts. Next we impose a condition to break out of the loop ones we reach the \'END\'. 10 charaters of the 1st line are put is first column (species name), their spaces are removed first using strrep() function. Next, the temperatures are determined depending on their position. Next we move on to the next line and we determine the position of E in the line, then we loop through these positions storing the coefficients in following columns, then same procedure is repeated twice as there are 2 more lines contining coefficients. Before storing these coefficients they are converted to numbers from string. Next we increment the line counter, after every fourth line and finally close the file.
Code 2 (search weight and plot) :
We use the function defined above and, store the data in the cell array row. Put value of R(universal gas constant), and initialize \'i\'. Take input from the user using input function. We make a cell array containing symbol of element and their corresponding wt. We put all the names of the element in the element \'E\'. Next, we initiate \'sum\' and \'old_Locb\'.
Finally, we search for the entered name of species by looping through the first column of \'row\'. We use strcmpi() to compare entered and present string insensitively(without case sensitivity). And we store the row no. of the found specie in variable \'i\'. Next, we put a condition, incase specie entered is not found in the \'row\'.
We put the name of the specie(present in first column of \'row\') in variable \'a\'. We loop through the characters in a and identify whether its present in \'E\' (using function ismember()) and if yes what is the location. If its present then we add the wt of the element in the variable sum, if not then we add the product of the \'no.\' present and weight of the previous identified specie. After the end of the loop we print the variable sum.
Next, we put the range of temperature in two arrays names low_temp and high_temp. Then we calculate arrays containing values of Cp, H and S (for corresponding values of Temp) using matrix operation.
Finally we make a folder using mkdir() function. Then we plot the graphs, Cp vs Temp, H vs Temp and S vs Temp. And put label on them using xlabel() and ylabel() function. We put title using title() function and we finally use saveas() function to save the plots in the newly created folder.
Next, we print \'nothing found\' if element is not found.
Code 3 (plot all) :
Procedure for calculation on Cp, H and S is same as code 2, but insteading plotting and saving graph only for the specie which is found, we plot and save graph by looping through all the rows of the cell array \'row\'.
Folder containg all files