フィルターのクリア

Retrieving data from structures

2 ビュー (過去 30 日間)
Ashlee
Ashlee 2012 年 10 月 26 日
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 日
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 日
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 日
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.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by