All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Aim : Retriving the data from the file and analysing the required things. Objective : 1. Data retreving from the text file using matlab. 2. Required inbuilt functions in matlab. Theory : Data retreving process is very important…
Epuri Yoga Narasimha
updated on 13 Dec 2020
Aim : Retriving the data from the file and analysing the required things.
Objective :
1. Data retreving from the text file using matlab.
2. Required inbuilt functions in matlab.
Theory :
Data retreving process is very important to know so that can analyse with that data.
In this file written code to retrive the data of nasa to study properties.
Taken molecular weights of elements approximately.
Some Inbuilt functions in matlab :
fopen() : open file , or obtain information about open files.
fclose() : close one or all open files.
fgetl() : Read line from line,removing newline characters.
frewind() : Move file position indicator to beginning of open file.
strcmp() : compare strings(case sensitive).
strcmpi() : compare strings(case insensitive).
strsplit() : split string or character vector at specified delimiter.
strcat() : concatenate or join strings horizontally.
str2double() : convert strings to double precesion values.
srtok() : parses str from left to right , using whitepsace characters as delimiters , and returns part of all of the text in token.
strfind() : searches a string or character in a string.
linspace() : create a n spaced points in interval [p,q].
fprintf() : file print formatted - use this command for printing text to a file or the command window.
sprintf() : string print formatted - use this for creating a matlab string( character vector ).
disp() : displays the value of variable X without printing variable name.
input() : used for to tell the user to give input.
saveas() : saves the figure specified by fig to file filename.
msgbox() : create message dialog box.
questdlg() : create question dialog box.
inputdlg() : create dialog box to gather user input.
uiwait() : Block diagram execution and wait to resume.
xlabel() : labeling x-axis.
ylabel() : labeling y-axis.
title() : labeling title.
ftell() : raturns numer of characters present in the file.
fscanf() : reads the data from the file using respective formatspec.
mkdir() : this command used to create a folders.
pwd : gives the location of present directory.
cd : used to open the required directory (or) change current folder.
Function for enthalpy , entropy , specific heat :
Enthalpy :
CpR=a1+a2â‹…T+a3â‹…T2+a4â‹…T3+a5â‹…T4.
Entropy:
HRâ‹…T=a1+a2â‹…T2+a3â‹…T23+a4â‹…T34+a5â‹…T45+a6T.
Specific Heat :
SR=a1â‹…ln(T)+a2â‹…T+a3â‹…T23+a4â‹…T33+a5â‹…T44+a7.
Main Code :
close all ; clear all ; clc ;
% fopen to open the file (>3)
f = fopen('nasa.txt','r');
% fgetl to access the line from text.
fir_com = fgetl(f);
% for global temperatures
l_global = fgetl(f);
%skipping 3 lines of comments
for i=1:3
fgetl(f);
end
%Creating a folder for to store data of all species
mkdir('total_files')
%Total number of species 53
% so,no.of iterations ->53.
for j = 1:53
s = fgetl(f);
format long E
formula = strtok(s);
fing = strfind(s,'G');
s_local = s(fing+4:length(s));
s_split = strsplit(s_local,' ');
local_min = str2double(s_split{1});
local_mid = str2double(s_split{3});
local_max = str2double(s_split{2});
% accessing the co-efficients
for j=1:3
k=1;
line2 = fgetl(f);
d = strfind(line2,'E');
final_E = d+3;
for i = 1:length(d)
fo{i,j} = line2(k:(d(i)+3));
k = (d(i)+3)+1;
end
end
%% Molecular weight
[weight] = molecular_weight(formula);
tex = sprintf('molecular weight of formula %s is %f',formula,weight);
disp(tex)
%% Entahalpy
[entalpy] = entalpy_curve(local_min,local_mid,local_max,fo);
t = local_min:1:local_max;
plot(t,entalpy,'color','r');
xlabel('temperature');
ylabel('entalpy');
title('temperature vs entalpy of' ,formula);
grid on
cd total_files;
% creating a file for respective specie.
mkdir(formula)
% directing to required directory.
cd(formula);
% saving the plots with considered format in directory.
saveas(gcf,strcat(formula,'_enthalpy'),'jpg')
%% Entropy
[entropy] = entropy_curve(local_min,local_mid,local_max,fo);
plot(t,entropy,'color','r');
xlabel('temperature');
ylabel('entropy');
title('temperature vs entropy of' ,formula);
grid on
saveas(gcf,strcat(formula,'_entropy'),'jpg')
%% Specific heat
[specific_heat] = specific_curve(local_min,local_mid,local_max,fo);
plot(t,specific_heat,'color','r');
xlabel('temperature');
ylabel('specific heat');
title('temperature vs specific_heat of' ,formula);
grid on
saveas(gcf,strcat(formula,'_specific_heat'),'jpg')
% cd .. , for directing to home directory.
cd ..
cd ..
end
Step Explanation of main_code :
1. close all -> to clear all the plots in present interface.
2. clear all -> to clear all the data in the workspace.
3. clc -> to clear the command window.
4. fopen to open the file , and stored the file identifier value in variable.
5. fgetl for accessing the line from the file.
6. silimarly , using fgetl accesses the global temperatures data and stored in variables.
7. skipped the unnecessary text.
8. created a folder to store all files of species , using mkdir inbuilt function.
9. Iterated the loop for 53 times , as no.of species are 53.
10. used format 'format long E' , for good precision.
11. 'strok' used for to store the species formula in variable.
12. 'strfind(str,'G') -> find the index position of G in string , so with reference to that index position , can access the local temperatures easily.
13. 'strsplit' -> split the string at specified delimiter.
14. 'str2double' -> converting string to the double precesion number.
15. stores the co-efficients in cell array by using for loop.
16. 'molecular_weight' function used to calculate the molecular weigth.
17. used seperate function for 'enthapy' , 'entropy' , 'specific heat' calculations.
18. 'plot' command used to plot the data.
19. 'saveas' used to save the plots automatically.
MOLECULAR_WEIGHT CODE :
function [output] = molecular_weight(species)
x1_c(1) = 0;
y1_c(2) = 0;
ko =1;
fo = 1;
% array containing the elements.
s_ele = ['O','H','C','N','S','A','R'];
% array contining the value of weights for respective elements.
mol_wei = [16,1,12,14,32,39.95,0];
% split used to elminate '()' content in species.
specie_0 = split(species,'(');
% split command to split string.
specie_1 = split(specie_0(1),'');
% accessing the required string.
specie = specie_1(2:length(specie_1)-1);
l_mol = length(specie);
% for loop used to calculate the molecular weight of specie.
% code written based on requirement flow.
for i = 1:l_mol
if specie{i} =='1' || specie{i} =='2' || specie{i} =='3' || specie{i} =='4' || specie{i} =='5'|| specie{i} =='6' || specie{i} =='7' || specie{i} =='8'
x = specie{i-1};
xo = find(s_ele == x); % find used to find the index of particular item in array.
mol_wei(xo);
x1_c(ko) = mol_wei(xo);
ko = ko +1;
else
y = specie{i};
y1 = find(s_ele == y);
mol_wei(y1);
y1_c(fo) = mol_wei(y1);
fo = fo+1;
end
end
% storing the total molecular weight of specie in output.
output = sum(y1_c) + sum(x1_c);
end
Explanation of Molecular_weight code :
1. Code used for to calculate the molecular weight of species.
2. for loop explanation ...
3. as specie contain 'number' and 'element' so used if-else loop to calulate weight.
4. if loop used for numericals , and accessed the weight of before element weight to number.
5. else used for weigth of elements in the specie formula.
6. finally , club the total weight in output variable.
ENTHALPY CODE :
function [entalpy] = entalpy_curve(min,mid,max,fo)
dong=fo;
R = 8.314;
format long E
%t=linspace(min,max,200);
t = min:1:max;
for i = 1:14 % length -> 7.
d1 = dong{i};
a(i) = str2double(d1);
end
for i=1:length(t)
if t(i)>=mid
% high temperature co-efficients.
H(i) = (a(1) + (a(2)*t(i))/2 + (a(3)*t(i)^2)/3 + (a(4)*t(i)^3)/4 +(a(5)*t(i)^4)/5 + a(6)/t(i))*R*t(i);
else
% low temperature co-efficients.
H(i) = (a(8) + (a(9)*t(i))/2 + (a(10)*t(i)^2)/3 + (a(11)*t(i)^3)/4 +(a(12)*t(i)^4)/5 + a(13)/t(i))*R*t(i);
end
entalpy = H;
end
end
Explanation of enthalpy code:
1. format long E used this format for good precesion.
2. split the temperature range , using basic principle [start:step:end].
3. first for loop used to convert the co-efficient strings into double precesion number.
4. calculated enthalpy , for respective temperature , observe the code.
5. stored the enthalpy data in to variable enthalpy.
6. Similar format of code , used for enthalpy and specific code calculations.
ENTROPY CODE :
function [entropy] = entropy_curve(min,mid,max,fo)
dong=fo;
R = 8.314;
format long E
%t=linspace(min,max,200);
t = min:1:max;
for i = 1:14 % length -> 7.
d1 = dong{i};
a(i) = str2double(d1);
end
for i=1:length(t)
if t(i)>=mid
% high temperature co-efficients.
S(i) = (a(1)*log(t(i)) + a(2)*t(i) + (a(3)*t(i)^2)/2 + (a(4)*t(i)^3)/3 + (a(5)*t(i)^4)/4 + a(7))*R;
else
% low temperature co-efficients.
S(i) = (a(8)*log(t(i)) + a(9)*t(i) + (a(10)*t(i)^2)/2 + (a(11)*t(i)^3)/3 + (a(12)*t(i)^4)/4 + a(14))*R;
end
entropy = S;
end
end
SPECIFIC_HEAT CODE :
function [specific] = specific_curve(min,mid,max,fo)
dong=fo;
R = 8.314;
format long E
%t=linspace(min,max,200);
t = min:1:max;
for i = 1:14 % length -> 7.
d1 = dong{i};
a(i) = str2double(d1);
end
for i=1:length(t)
if t(i)>=mid
% high temperature co-efficients.
cp(i) = (a(1) + a(2)*t(i) + a(3)*t(i)^2 + a(4)*t(i)^3 + a(5)*t(i)^4)*R;
else
% low temperature co-efficients.
cp(i) = (a(1) + a(2)*t(i) + a(3)*t(i)^2 + a(4)*t(i)^3 + a(5)*t(i)^4)*R;
end
specific = cp;
end
end
OUTPUTS SPECIFIED :
Plots of O2:
PLOTS OF N2 :
PLOTS OF CO2 :
MOLECULAR WEIGHT OF SPECIES :
references:
https://in.mathworks.com/help/matlab/ref/fopen.html
https://in.mathworks.com/help/matlab/ref/fclose.html
https://in.mathworks.com/help/matlab/ref/fprintf.html
https://in.mathworks.com/help/matlab/ref/sprintf.html
https://in.mathworks.com/help/matlab/ref/fscanf.html
https://in.mathworks.com/help/matlab/ref/str2double.html
https://in.mathworks.com/help/matlab/ref/mkdir.html
https://in.mathworks.com/help/matlab/ref/cd.html
Conclusions:
1. Data in file processed and showed the specified data of species.
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...
Project 4
Objective 1. Detect lanes using Hough Transform Method. Steps in lane Detection 1. Detect Edges using Canny Edge Detection Method. 2. Capture the Region of Interest in Image. 3. Detect lines using Hough Transform. 4. Calculate the actual left and right lanes from all lines. 6. Find the coordinates of the lines. 7. Display…
21 Feb 2023 05:22 AM IST
Lane Detection using Hough Transform
Objective 1. Detect lanes using Hough Transform Method. Steps in lane Detection 1. Detect Edges using Canny Edge Detection Method. 2. Capture the Region of Interest in Image. 3. Detect lines using Hough Transform. 4. Calculate the actual left and right lanes from all lines. 6. Find the coordinates of the lines. 7. Display…
21 Feb 2023 05:20 AM IST
Edge Detection and Hough Transform
Objective: Detecting Edges using Canny Edge Detector. Understand and display the outputs of all steps in the canny Edge Detection process. Effect on the output on changing the parameters of the Non-Maximum Suppression. Understand Hough Transform and Lane Detection using Hough Transform. Steps Follow in the Discussion:…
28 Dec 2022 09:58 PM IST
week 2 : Image Filtering
Objective: Apply Prewitt and Laplacian filter on the image Libraries used: Opencv Open Source computer vision library. cross-platform supported. can perform through c, c++, and python. numpy Mathematical library to work in the domain of Linear algebra, Fourier transform, and matrices. Image Filtering…
09 Jul 2022 06:36 AM 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.