Matlab Tutorials | Examples
Practice 11:
Files: Periodic Table – Electron Configurations
You are given a file named “PeriodicTable.txt”. The format of the file is as follows:
The third column of this file, electron configuration, gives the arrangement of the electrons in various atomic orbitals around the nucleus. For simplicity, the configuration gives a noble gas symbol (in square brackets) plus the orbitals that need to be added on top of it.
Write a MATLAB program that does the following jobs:
- Reads the entire periodic table file into variable(s).
- A pop-up file selection dialog box should be displayed for choosing the TXT file to be read from.
- You should check for errors that might occur while opening the file. If the filer dialog box is cancelled, stop the program. If there is any error while opening the file, an explanation describing the nature of the error should be printed into the command window, and the program should be terminated.
- The first line of the file is a header line, and should be skipped.
- You can decide to read the file into any kind of variable—matrix, record structure array, cell array.
- You should assume that number of lines in the file changes as more elements are added; so you should not assume a fixed number of lines in the file, but rather read the file until end-of-file.
- Expand the electron configurations of all atoms, and determine the atomic number for each by adding up the electron counts in all s, p, d, and f atomic orbitals. Note that atomic number of an element is NOT equal to its sequence in our list, as some elements are omitted from the given file.
- In your list of chemical elements, add a string field to describe the group of the element. For elements that belong to the groups Alkali Metals, Alkali Earth Metals, Halogens, and Noble Gases, change their group value to “A”, “E”, “H”, and “N”, respectively; for all others, leave this value as blank.
- Noble gases have six electrons in outermost p-orbital (except Helium, which you should include as an exceptional noble gas).
- Alkali metals follow noble gases in atomic number.
- Alkali earth metals follow alkali metals in atomic number.
- Halogens come just before noble gases in atomic number. Note that Hydrogen is an exception to this rule; it is not a Halogen.
4. From
Earth Metals, Halogens, Noble Gases into four separate files in your MATLAB work folder. Four fields should be written into the output file in tabular format: Symbol, element name, atomic number, and expanded electron configuration. Files should be named as follows (no need to ask for their names):
your list of elements, write those that belong to the groups Alkali Metals, Alkali
Group |
File name |
||||
Alkali Metals |
Group1A.txt |
||||
Alkali Earth Metals |
Group2A.txt |
||||
Halogens |
Group7A.txt |
||||
Noble Gases |
Group8A.txt |
The file Group1A.txt should be laid out exactly as follows. The other individual raw data files (Group2A.txt, Group7A.txt, Group8A.txt) should have similar format as Group1A.txt.
Files:https://drive.google.com/open?id=1lLHt_BW1z3f2XNUuMncKpTPKGbj86LGF
Solutions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
clear clc fclose all; j=1; a=1; b=1; c=1; d=1; flag1=false; flag2=false; % Open input file [file,folder]= uigetfile('*.txt','Select input file'); % Get the name of txt file from user if file==0 return end [fin,err]= fopen([folder file],'r'); % Open file for read if fin==-1 disp(err); return end [fout1,err]= fopen('Group1A.txt','w');% Open files for write if fout1==-1 disp(err); fclose all; return end [fout2,err]= fopen('Group2A.txt','w'); if fout2==-1 disp(err); fclose all; return end [fout7,err]= fopen('Group7A.txt','w'); if fout7==-1 disp(err); fclose all; return end [fout8,err]= fopen('Group8A.txt','w'); if fout8==-1 disp(err); fclose all; return end fgetl(fin); fprintf(fout1,'Group 1A\r\n%-7s%-12s%-5s %s\r\n','SYMBOL','NAME','Atom#','ELECTRON CONFIGURATION'); fprintf(fout2,'Group 2A\r\n%-7s%-12s%-5s %s\r\n','SYMBOL','NAME','Atom#','ELECTRON CONFIGURATION'); fprintf(fout7,'Group 7A\r\n%-7s%-12s%-5s %s\r\n','SYMBOL','NAME','Atom#','ELECTRON CONFIGURATION'); fprintf(fout8,'Group 8A\r\n%-7s%-12s%-5s %s\r\n','SYMBOL','NAME','Atom#','ELECTRON CONFIGURATION'); while ~feof(fin) % read file until end of file x= fgetl(fin); i= 1; sum = 0; while x(i)~=' ' % read symbol i= i+1; end electron(j).symbol= x(1:i-1); i2= 8; while x(i2)~=' ' % read element name i2= i2+1; end electron(j).name= x(8:i2-1); i3= 23; electron(j).configuration= x(23:end); if electron(j).configuration == "1s2" % Select first two condition because they are different than others group8(a).symbol = electron(j).symbol(1:end); %Initilaze the helium in group8 group8(a).name = electron(j).name(1:end); group8(a).configuration = electron(j).configuration(1:end); %Save in the structure splitted = strsplit(group8(a).configuration,'.'); for count=1:length(splitted) sum = sum +str2num(splitted{1,count}(3:end)); % Calculate electron numbers end fprintf(fout8, '%-7s%-12s%5g %s\r\n',group8(a).symbol ,group8(a).name,sum,group8(a).configuration); a = a +1; flag1= true; elseif electron(j).configuration == "1s1" % Select first two condition because they are different than others group7(b).symbol = electron(j).symbol(1:end);%Initilaze the hydrogen in group7 group7(b).name = electron(j).name(1:end); group7(b).configuration = electron(j).configuration(1:end);%Save in the structure splitted = strsplit(group7(b).configuration,'.'); for count=1:length(splitted) sum = sum +str2num(splitted{1,count}(3:end));% Calculate electron numbers end fprintf(fout7, '%-7s%-12s%5g %s\r\n',group7(b).symbol ,group7(b).name,sum,group7(b).configuration); b = b +1; elseif electron(j).configuration(end-1:end) == 'p6' %Find if configuration finish with p6 it is 8A group8(a).symbol = electron(j).symbol(1:end); group8(a).name = electron(j).name(1:end); group8(a).configuration = [group8(end-1).configuration electron(j).configuration(5:end)]; %%Find full configuration use above group8A elements configuration splitted = strsplit(group8(a).configuration,'.'); for count=1:length(splitted) sum = sum + str2num(splitted{1,count}(3:end)); end fprintf(fout8, '%-7s%-12s%5g %s\r\n',group8(a).symbol ,group8(a).name,sum,group8(a).configuration); a = a +1; flag1= true; elseif electron(j).configuration(end-1:end) == 'p5' group7(b).symbol = electron(j).symbol(1:end); group7(b).name = electron(j).name(1:end); group7(b).configuration = [group8(end).configuration electron(j).configuration(5:end)];%%Find full configuration use above group8A elements configuration splitted = strsplit(group7(b).configuration,'.'); for count=1:length(splitted) sum = sum + str2num(splitted{1,count}(3:end)); %Calculate totoal electron end fprintf(fout7, '%-7s%-12s%5g %s\r\n',group7(b).symbol ,group7(b).name,sum,group7(b).configuration); b = b +1; elseif flag1 %%IF 8A group found flag1 is true so that after 8A is 1A. group1(c).symbol = electron(j).symbol(1:end); group1(c).name = electron(j).name(1:end); group1(c).configuration = [group8(end).configuration electron(j).configuration(5:end)]; splitted = strsplit(group1(c).configuration,'.'); for count=1:length(splitted) sum = sum + str2num(splitted{1,count}(3:end)); end fprintf(fout1,'%-7s%-12s%5g %s\r\n',group1(c).symbol ,group1(c).name,sum,group1(c).configuration); c = c +1; flag1=false; flag2=true; %Preparation for the 2A elseif flag2 %%IF 1A group found flag1 is true so that after 1A is 2A. group2(d).symbol = electron(j).symbol(1:end); group2(d).name = electron(j).name(1:end); group2(d).configuration = [group8(end).configuration electron(j).configuration(5:end)]; splitted = strsplit(group2(d).configuration,'.'); for count=1:length(splitted) sum = sum + str2num(splitted{1,count}(3:end)); end fprintf(fout2,'%-7s%-12s%5g %s\r\n',group2(d).symbol ,group2(d).name,sum,group2(d).configuration); d = d +1; flag2=false; end j = j+1; end fclose all; disp('Done'); |