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

1 次查看(过去 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 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by