Bar Graphing the results of two Switch statements

조회 수: 2 (최근 30일)
Edwin J Santiago Alvarez
Edwin J Santiago Alvarez 2015년 3월 9일
댓글: Guillaume 2015년 3월 10일
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.

채택된 답변

Guillaume
Guillaume 2015년 3월 10일
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);
  댓글 수: 5
Edwin J Santiago Alvarez
Edwin J Santiago Alvarez 2015년 3월 10일
If I were to use:
function [numofatoms, massofatoms] = atompicker
how would I input the switch statement version of my program with the function above.
Guillaume
Guillaume 2015년 3월 10일
I'm not sure I understand your question. Can you rephrase or show an example of what you mean?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Christiaan
Christiaan 2015년 3월 10일
Hello Edwin,
What you could do is to add a line for each case, where the calculated amount of atoms is defined in only one variable. Repeat this for choice 2.
Then, since it is good to know which element you have chosen in the end, you could also add for each case a string, that you know which element is chosen.
An example how it could look like is this:
switch elegr1
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);
numofatoms_choice1 = numofHatoms; label_choise1='Hydrogen';
case 2
If you have done this, in the end of the m-file you will have for choice 1 and 2, not only the amount of atoms but also which atom it is.
Then you can easily use the bar plot, to plot the results. (and use a legend which atom it is)
Good luck! Christiaan

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by