How to separate the elements of a table with their names?

1 回表示 (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 26 日
コメント済み: Ajay Pattassery 2019 年 10 月 29 日
Attached is the table. I need to plot rad vs k_val and total vs k_val for each individual k_name. The output would be TWO plots each have all k_names plot stacked: K10, K11, K12, K1, K2, K6 and so on.
My question is how to separate the Xs and Ys from table based on their k_name. One way is by all k_names in a for loop if the ith and ith+1 k_name are the same or not.
I wanted to check whether there is easier way so that I can avoid for loop? I dont want to separate them by their period as their frequency might alter. Capture.JPG
  2 件のコメント
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 26 日
@ Walter Roberson Was wonderinf if you have any suggestion?
Walter Roberson
Walter Roberson 2019 年 10 月 29 日
findgroups() and splitapply()

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

回答 (1 件)

Ajay Pattassery
Ajay Pattassery 2019 年 10 月 29 日
I am not sure of getting rid of the for-loop. But the following code can achieve what you are looking for neatly.
Assume T is the name of the table.
k_names = unique(T.k_name);
for value = 1:length(k_names)
h = string(T.k_name) == k_names(value);
rad(:,value) = T.rad(h);
k_val(:,value) = T.k_val(h);
total(:,value) = T.total(h);
end
plot(rad,k_val);
plot(total,k_val);
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 29 日
Note that this assumes that there are the same number of occurances of each name.
Ajay Pattassery
Ajay Pattassery 2019 年 10 月 29 日
Yes. Thanks for pointing out.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by