Subsetting variables in bar function gives a zero for excluded x-values.

When subsetting variables within the bar function, the subsetting only works for the y values, and not for the x values. Here is an MRE of the issue:
x = categorical({'a','b','c'});
y = 1:3;
bar(x,y) % works just fine
bar(x(1:2),y(1:2)) % c shouldn't be there
The x value corresponding to 'c' should not appear, right? My goal is to have a bar plot without the 'c' value. Assigning the subset to a different variable, e.g. temp = x(1:2); does not work. In my code, I can't just do the equivalent of x=1:2; since I need to subset from a variable containing names. Thanks!

1 个评论

"The x value corresponding to 'c' should not appear, right?"
It definitely should appear, that is rather the point of catgorial arrays. Consider a simpler (rather contrived) example of two values TRUE and FALSE. If one way our data only happens to contain FALSE values, that does not change the range of possible values, which remains unchanged (as TRUE and FALSE). The possible values is not changed by what values actually occur in any specific data set: literally the categories remain unchanged.
If I want to plot several plots of a categorical array, then I want my independent axes to be the same in all cases.

请先登录,再进行评论。

 采纳的回答

I understand Stephen's comment, and why categories would be "preserved" even if there's no data "attached" to them (lazy explanation, sorry). Benjamin's answer was along the lines of what I needed, but there's a simpler way to get what I needed. This is why I'm writing this answer: in case anyone finds this helpful and finds themselves in the same situation I was and wants an easy way out it.
Starting from the same base case:
x = categorical({'a','b','c'});
y = 1:3;
bar(x,y) % works just fine
x = x(1:2);
% if we run categories(x) now, we will still see 'c'
x = removecats(x); % this removes "empty" categories
% if we run categories(x) now, we will not see 'c'
bar(x,y(1:2)) % c shouldn't be there
This is not an explanation for why categories are preserved, for that see Stephen's comment and Stephen's other comment. Again, this is just a workaround. Thanks to both Stephen and Benjamin for their comments/answers.

更多回答(1 个)

I'm not sure this is the best way (in fact I'm pretty sure it's not), but it seems to do the job:
x = categorical({'a','b','c'});
y = 1:3;
c = categories(x)
c = 3×1 cell array
{'a'} {'b'} {'c'}
xx = setcats(x,c(1:2))
xx = 1×3 categorical array
a b <undefined>
% xx = removecats(x,c(3)) % appears to have the same effect
display(categories(xx));
2×1 cell array {'a'} {'b'}
figure();
bar(xx,y); % note that we still pass all elements of y in to bar()

3 个评论

Hi, Benjamin, and thanks for your answer. I haven't tried this out yet, but it should work and I can implement it in my program. Before I accept your answer, I would like to wait for other answers to see if there is a "simple" or "robust" enough way to deal with this (otherwise, I'll accept it). I'm no expert, but I do believe there should be an easier way to do this, whether someone finds it or whether it is a bug/feature. I can see it being viewed as a feature (e.g. factor variables in R can still contain factor levels of factors which are not contained in such variables), but perhaps it is an unintended consequence of such "feature".
I agree, it seems like there would be a way to completely remove categories from a categorical array. I never use categorical arrays so I don't know, but I figured out this method that seems to do the trick (whether it's the 'correct' way or not), so I figured I'd go ahead and post it so you can move forward with your project.
I'd be interested to see how people with more experience in categorical arrays would do this.
"it seems like there would be a way to completely remove categories from a categorical array"
x = categorical({'a','c','a'}, {'a','b','c'});
categories(x)
ans = 3×1 cell array
{'a'} {'b'} {'c'}
x = removecats(x);
categories(x)
ans = 2×1 cell array
{'a'} {'c'}

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Annotations 的更多信息

产品

版本

R2019b

提问:

2021-12-20

回答:

2021-12-21

Community Treasure Hunt

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

Start Hunting!

Translated by