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

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});

 채택된 답변

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

when i execute this code, i Get Too many input arguments as an error message......how can i fix this message??
You shouldn't get that error message. Just paste this into a blank/new editor window and run it. It will ask you to save it. Call this test program "test.m" It will run. I know, I tried it.
I am trying to get the result periodictable('symbol',2) ans = 'Be' 'Mg'
When I run this function in the command window, I get the error message of too many inputs. What am I doing wrong
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
@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개)

카테고리

도움말 센터File Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

질문:

2013년 10월 28일

댓글:

2013년 10월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by