Matlab:bar不存在公共属性CData [英] Matlab: bar No public property CData exists
问题描述
Matlab示例代码无法在Matlab 2017a(Linux 64位)中运行: https://cn.mathworks.com/help/matlab/ref/bar. html
The Matlab example code cannot run in Matlab 2017a (Linux 64bits): https://cn.mathworks.com/help/matlab/ref/bar.html
b = bar(rand(10,1));
b.FaceColor = 'flat';
b.CData(2,:) = [.5 0 .5];
它不能为特定的栏着色并警告为:
It can not color the specific bar and warns as:
对于类别matlab.graphics.chart.primitive.Bar,不存在公共属性CData.
No public property CData exists for class matlab.graphics.chart.primitive.Bar.
我不知道这仅仅是Matlab 2017b的特定功能吗?
I don't know it is a specific feature merely for Matlab 2017b?
此代码仅用于为特定的条着色.在Matlab 2012a中,可以通过以下方式轻松实现:
This code is only to color the specific bar. In Matlab 2012a, it can be easily done by:
bh=bar(MyStem); h=get(bh, 'children');
shading flat; set(h,'FaceVertexCData',MyColor);
我不明白为什么Matlab 2017会删除此功能.
I cannot understand why Matlab 2017 removes this feature.
推荐答案
关于CData
属性,我已经在注释中写过.
无论如何,我认为比建议的方法更简单的替代方法可能是:
About the CData
property, I wrote already in the comment.
Anyway, another workaround that I think can be more simple than those that been suggested, can be that:
x=rand(1,10);
b=bar([x;zeros(1,length(x))]);
xlim([0.5 1.5])
set(b,'FaceColor','r')
set(b(2),'FaceColor','b')
如果要像在常规条形图中那样对条进行编号,则可以添加以下内容:
If you want to number the bars as in a regular bar graph, you can add this:
set(gca,'XTick',0.5+[b.XOffset])
set(gca,'XTicklabels',1:length(x))
这篇关于Matlab:bar不存在公共属性CData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!