Write a function that returns the requested property of atoms belonging to the requested groupnumber

조회 수: 3 (최근 30일)
I am trying to write a function periodic table that will return the requested property of atoms that belong to the requested group number.
Here is what I have so far....I am stuck as to where to go from here:
unction [ output_args ] = periodictable(p, g) %that returns the requested property of atoms belonging to the requested %groupnumber. Consider only the first 18 atoms in the periodic table (i.e., %up to and including Argon). Inside your function, store the periodic table %of elements as a structure array. Store the atomicnumber, group, period, %and symbol for each atom.
p=struct('symbol', {'H' 'He' 'Li' 'Be' 'B' 'C' 'N' 'O' 'F' 'Ne' 'Na' 'Mg' 'Al' 'Si' 'P' 'S' 'Cl' 'Ar'}, 'atomicnumber', {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}, 'period', {1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3} , 'group', {1 18 1 2 13 14 15 16 17 18 1 2 13 14 15 16 17 18});

채택된 답변

Image Analyst
Image Analyst 2013년 10월 28일
Pretty close, but try it like this:
function test
% Main test harness to call the function.
results = periodictable(6) % Ask for Carbon.
function [ output_args ] = periodictable(atomicNumber)
%that returns the requested property of atoms belonging to the requested
%groupnumber. Consider only the first 18 atoms in the periodic table (i.e.,
%up to and including Argon). Inside your function, store the periodic table
%of elements as a structure array. Store the atomicnumber, group, period,
%and symbol for each atom.
p = struct('symbol', {'H' 'He' 'Li' 'Be' 'B' 'C' 'N'...
'O' 'F' 'Ne' 'Na' 'Mg' 'Al' 'Si' 'P' 'S' 'Cl' 'Ar'}, ...
'atomicnumber', {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}, ...
'period', {1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3}, ...
'group', {1 18 1 2 13 14 15 16 17 18 1 2 13 14 15 16 17 18});
if atomicNumber > length(p)
uiwait(warndlg('Enter a number less than or equal to 18'));
output_args = [];
return;
end
output_args.symbol = p(atomicNumber).symbol;
output_args.atomicnumber = p(atomicNumber).atomicnumber;
output_args.period = p(atomicNumber).period;
output_args.group = p(atomicNumber).group;
  댓글 수: 5
Image Analyst
Image Analyst 2013년 10월 28일
편집: Image Analyst 2013년 10월 28일
Oh, okay, I see how you'd like to use it now. That's not what I did. I'll have to make changes. Try this:
function test
clc;
% Main test harness to call the function.
results = periodictable('symbol',2) % Ask for helium.
function [ output_args ] = periodictable(member, groupNumber)
%that returns the requested property of atoms belonging to the requested
%groupnumber. Consider only the first 18 atoms in the periodic table (i.e.,
%up to and including Argon). Inside your function, store the periodic table
%of elements as a structure array. Store the atomicnumber, group, period,
%and symbol for each atom.
p = struct('symbol', {'H' 'He' 'Li' 'Be' 'B' 'C' 'N'...
'O' 'F' 'Ne' 'Na' 'Mg' 'Al' 'Si' 'P' 'S' 'Cl' 'Ar'}, ...
'atomicnumber', {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}, ...
'period', {1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3}, ...
'group', {1 18 1 2 13 14 15 16 17 18 1 2 13 14 15 16 17 18});
if groupNumber > 18
uiwait(warndlg('Enter a number less than or equal to 18'));
output_args = [];
return;
end
elementsMatchingGroup = groupNumber == [p.group]
switch lower(member)
case 'symbol'
output_args = {p(elementsMatchingGroup).symbol};
case 'atomicnumber'
output_args = {p(elementsMatchingGroup).atomicnumber};
case 'period'
output_args = {p(elementsMatchingGroup).period};
case 'group'
output_args = {p(elementsMatchingGroup).group};
end
Cedric
Cedric 2013년 10월 28일
@KayLynn: don't forget to [ Accept ] the answer that is most useful to you. You have 0% accept rate and 0 vote up, yet people spent time writing answers to your questions and comments with valuable information.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by