Hello, I'm a student and beginner with MatLab. I have an assignment and I'm having difficulty getting a structure setup using a loop. If there is more then one set of data I only get the last one inputted. Here is the assignment description:
Create a data structure to store information about the elements in the periodic table of elements. For every element, store the name, atomic number, chemical symbol, class, atomic weight, and a sevenelement vector for the number of electrons in each shell. Create a structure variable to store the information. Write a script that requests the inputs for each element and properly stores the elements. You can get this information from http://www.ptable.com/. Your script should be able to request all the elements in the table. Enter the first 18 elements in the table and write the table to a comma separated value spreadsheet.
And here is what I have so far.
clcclearHowManyElements = input('How many elements are you entering?: ');ElementNames = input('Enter the NAME of the element: ','s');ElementAbrev = input('Enter the atomic SYMBOL of the element: ','s');ElementNumber = input('Enter the atomic NUMBER of the element: ');ElementClass = input('Enter the CLASS of the element: ','s');ElementWeight = input('Enter the atomic WEIGHT of the element: ');if HowManyElements > 1 for k = 1:(HowManyElements-1) ElementNames = input('\nEnter the NAME of the element: ','s'); ElementAbrev = input('Enter the atomic SYMBOL of the element: ','s'); ElementNumber = input('Enter the atomic NUMBER of the element: '); ElementClass = input('Enter the CLASS of the element: ','s'); ElementWeight = input('Enter the atomic WEIGHT of the element: '); endend%Initialize a 7 field vector of zeros
EShells = zeros(1,7);%Initialize cell array for faster process
ElecShells = cell(1,ElementNumber);%For loop for the electron shells
for i = 1:ElementNumber %First shell is only 2 electrons
if i <= 2 EShells(1) = i; %Shells after are 8
else %Set first shell equal to 2
EShells(1) = 2; %Find number of shells after first shell
NumShells = floor((i-2)./8); %Calculate remainder after first shell
RemainderE = mod((i-2),8); %Shell 2 to number of shells after first set to 8
EShells(2:NumShells+1) = 8.*ones(1,NumShells); %Fill following shell with remainder
EShells(NumShells+2) = RemainderE; end %For loop the display of electron shells
ElecShells{i} = EShells;endElements = struct('Name', ElementNames, 'Atomic_Number', ElementNumber,... 'Chemical_Symbol', ElementAbrev, 'Class', ElementClass,... 'Atmoic_Weight', ElementWeight, 'Electrons_in_Each_Shell', ElecShells);for i = 1:length(HowManyElements) disp(Elements(i))end
I also have no idea how to do the last part. "Enter the first 18 elements in the table and write the table to a comma separated value spreadsheet."
Best Answer