Retrieving data from structures

I am working on a function for the periodic table that when I input a property and group number, the output is the requested property of the atom belonging to the requested group number. I have already created a structure for the first 18 elements that I know works. It holds the properties: atomic number, group, period, and symbol.
If I have: function periodictable(property,groupnumber), how would I be able to extract the data...if that makes sense.
Example:
>> periodictable('symbol',2) ans = 'Be' 'Mg'
>> periodictable('atomicnumber',1) ans = [1] [3] [11]

댓글 수: 4

Matt J
Matt J 2012년 10월 26일
편집: Matt J 2012년 10월 26일
Clarify what your structure looks like. Is the structure you have for the first 18 elements a scalar struct holding arrays of properties? Or is it a struct array with yourstruct(i), i=1...18 corresponding to the i-th element?
Ashlee
Ashlee 2012년 10월 26일
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});
Matt J
Matt J 2012년 10월 26일
See my solution below, then.
Ashlee
Ashlee 2012년 10월 26일
you sir, are my hero.

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

 채택된 답변

Matt J
Matt J 2012년 10월 26일
편집: Matt J 2012년 10월 26일

0 개 추천

I'm assuming you have a structure array, yourstruct, and that yourstruct(i) corresponds to the i-th element.
idx=[yourstruct.group]==number_to_search;
out = {yourstruct(idx).(property)};

추가 답변 (1개)

Ryan G
Ryan G 2012년 10월 26일
편집: Ryan G 2012년 10월 26일

0 개 추천

Assuming your structure is like:
periodicTable.Helium periodicTable.Carbon etc...
names = fieldnames(myTable);
inGrp = cellfun(@(x) periodicTable.(x).group==groupnumber,names);
grpNames = names(inGrp);
output = cellfun(@(y) periodicTable.(y).('symbol'),grpNames)
Where you would replace symbol with the variable you choose, not in single quotes.

카테고리

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

질문:

2012년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by