Selecting categorical variable range to plot on bar graph

17 ビュー (過去 30 日間)
Nex
Nex 2021 年 9 月 29 日
コメント済み: Nex 2021 年 10 月 4 日
I don't understand why the categorical data is shown in its entirety on the graph despite only a portion of it being selected.
Am I indexing it wrongly?
C = categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'});
N = [5 4 2 3 4 8];
bar(C(3:end), N(3:end))
  2 件のコメント
Siddharth Bhutiya
Siddharth Bhutiya 2021 年 10 月 4 日
When you create a cateogorical array, you specify the list of categories. That list now becomes a property of the categorical object. Taking a subset of the array using indexing would not update that list. If you want to add/remove a category from your array then you would have to use something like addcats/removecats.
>> C = categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'});
>> categories(C)
ans =
6×1 cell array
{'A'}
{'B'}
{'C'}
{'D'}
{'E'}
{'F'}
>> C1 = C(3:end)
C1 =
4×1 categorical array
C
D
E
F
>> categories(C1)
ans =
6×1 cell array
{'A'}
{'B'}
{'C'}
{'D'}
{'E'}
{'F'}
Nex
Nex 2021 年 10 月 4 日
Thank you very much for the clear explanation and suggestion of a solution.

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

採用された回答

VBBV
VBBV 2021 年 9 月 29 日
C = categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'});
N = [5 4 2 3 4 8];
bar(C(3:end), N(3:end))
xticks(C(3:end))
xticklabels(C(3:end))
Use Xticks & Xticklabels

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by