フィルターのクリア

How to call/create column variables from changeable table?

3 ビュー (過去 30 日間)
Ana Castanheiro
Ana Castanheiro 2018 年 1 月 29 日
コメント済み: Peter Perkins 2020 年 4 月 15 日
Hi! I have this table (see image) from which I need to call all individual columns using their column names. For example, for using a variable called Fe to retrieve the Fe column, and so on for all columns. I know I can do this by using tablename.Fe, but I'm looking for a more efficient and automatic way, because:
  1. there are dozens of columns to be retrieved
  2. the order/size of the initial data table differs from sample to sample (especially because of this!)
In the end, I want to be able to use these variables for further operations, for example to do cluster analysis by focusing on specific elements only (e.g. data1 = [S Al Si Ca Fe];).
Any help is really appreciated!

採用された回答

Peter Perkins
Peter Perkins 2018 年 1 月 30 日
編集済み: Peter Perkins 2018 年 1 月 31 日
I think what you are looking for is either
data1 = data(:,{'S' 'Al' 'Si' 'Ca' 'Fe'})
or
data1 = data{:,{'S' 'Al' 'Si' 'Ca' 'Fe'}}
depending on whether you want a subtable or a numeric matrix. And of course you could substitute [10 7 8 13 17] for those names in either case. There's a whole section in the doc about table subscripting.
  2 件のコメント
Jörg Ho
Jörg Ho 2020 年 3 月 31 日
Hi,
I have a similar problem, but the solution won't fit. 'data1' is a table as well as 'data'. What I want is to create a number of arrays (in this example would it be for the names of the arrays: data.Properties.VariableNames) with its data from the table.
I thought of a loop over the names like:
for item=data.Properties.VariableNames
? = table2array(data(:,item))
end
But, how do I get the value of 'item' in place of the '?'?
Thanks a lot,
Jörg Ho
Peter Perkins
Peter Perkins 2020 年 4 月 15 日
Very likely you do not want to do that. Unless you have a very small number of variables in your table, you will end up with many things in your workspace, and no good way to address them. If you do have only a very small number of variables in the table, do it by hand.
You have those data in a table. You can refer to them by name using dot subscripting, for example
data.ItemName
and you can easily loop over them, referring to each one as
data.(data.Properties.VariableNames{i})
or even
data.(i)
" 'data1' is a table as well as 'data'."
No, it isn't if you use braces, not parentheses. More information in the doc section I referred to.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by