MATLAB: How to loop inputs into a structure

structures

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.
clc
clear
HowManyElements = 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: ');
end
end
%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;
end
Elements = 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

First guess at your problem is that the initial for loop where you define all of the inputs doesn't save the results in an indexed format, just overwrites the results each loop. So your structure calling looks to be correct, but your values of ElementNames, ElementNumber, etc don't actually contain more than the last set of inputs.
If you wanted, you could combine the initial loop and the structure definition into one, and just enter your inputs directly into the structure.
for k = 1:HowManyElements; % It's ok to run a for loop just once, so you can combine the inital inputs and the loop into one.
[Elements(k).Name] = input('Enter the NAME of the element: ','s');
[Elements(k).Chemical_Symbol] = input('Enter the atomic SYMBOL of the element: ','s');
...
end
%Initialize a 7 field vector of zeros
EShells = zeros(1,7);
...
Related Question