MATLAB: Bar Graphing the results of two Switch statements

bar graphMATLABswitch statements

Hello,
I am making a program that involves two switch statements and the entire periodic table. The program goes from switch statement #1, Pick a group from the periodic table, pick an element within that group, enter mass of the element and then it will calculate the number of atoms in the said amount of grams of the element. Then it goes directly to switch statement #2 and the same process happens all over again. My objective is to let the user pick two elements, set a mass for both of them, find how many atoms are in the grams of the selected elements and show a bar graph. I was hoping if someone knows how I can take those two results and put them in a bar graph so I could compare them vertically on a Y-axis. Here is what I have so far.
Thanks Edwin.

Best Answer

Common practice for switch statements like you have done is to use the same output variable for all the cases. So instead of:
case 1
massofH = input('Enter mass of Hydrogen in grams:');
numofHatoms = (massofH/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofHatoms,massofH);
case 2
massofLi = input('Enter mass of Lithium in grams:');
numofLiatoms = (massofLi/Li)*Avosnum;
fprintf('There are %g atoms in %g grams of Lithium \n;',numofLiatoms,massofLi);
%etc.



you would have
case 1
massofatoms = input('Enter mass of Hydrogen in grams:');
numofatoms = (massofatoms/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofatoms,massofatoms);
case 2
massofatoms = input('Enter mass of Lithium in grams:');
numofatoms = (massofLi/Li)*Avosnum;
fprintf('There are %g atoms in %g grams of Lithium \n;',numofatoms ,massofatoms);
%etc.
Because you have the same variables for all cases, after the switch statement you can then do something useful with it.
One of the rule of programming: if you write the same code more than once, time to make it into a function. So instead of the two switch statements which are identical, extract the code into a function:
function [numofatoms, massofatoms] = atompicker
%Periodic Table: Element's Atomic Weight
%Group 1A's (Alkaline Metals and Hydrogen) Atomic Weight
H = 1.01; Li = 6.94; Na = 22.99; K = 39.09; Rb = 85.47; Cs = 132.9; Fr = 223;
%Group 2A's (Alkali Earth Metals) Atomic Weight
Be = 9.01; Mg = 24.31; Ca = 40.08; Sr = 87.62; Ba = 137.33; Ra = 226;
%etc.
choice = menu('Pick a group within the Periodic Table:','1A','2A','3B','Lanthanides','Actinides','4B','5B','6B','7B','8B','9B','10B','11B','12B','3A','4A','5A','6A','7A','8A');
switch choice
case 1
element = menu('Pick element from the group:','Hydrogen','Lithium','Sodium','Potassium','Rubidium','Cesium','Francium');
switch element
case 1
massofatoms = input('Enter mass of Hydrogen in grams:');
numofatoms = (massofatoms/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofatoms,massofatoms);
case 2
massofatoms = input('Enter mass of Lithium in grams:');
numofatoms = (massofLi/Li)*Avosnum;
fprintf('There are %g atoms in %g grams of Lithium \n;',numofatoms ,massofatoms);
%etc.
end
end
end
In you script then it's simply
[numofatoms(1), massofatoms(1)] = atompicker
[numofatoms(2), massofatoms(2)] = atompicker
bar(numofatoms);