1

how can I change the color of one of my bar element in the bar graph? Because it seems my Matlab version (2015b) doesn't let me use either b.LineWidth b.EdgeColor Or CData. my code is something like below;

b = bar(1:30); 
b.FaceColor = 'flat'; 
[bv,bi] = max(1:30); 
b(bi).LineWidth = 2;
b(bi).EdgeColor = 'red';

With this error for using b.LineWidth and b.EdgeColor;

No public property LineWidth exists for class matlab.graphics.GraphicsPlaceholder. Error in tt (line 5)

and the error for using CData;

b = bar(1:30); 
b.FaceColor = 'flat'; 
[bv,bi] = max(1:30);
b.CData(bi) = [0.4,0.4,0.4];

No appropriate method, property, or field 'CData' for class 'matlab.graphics.chart.primitive.Bar'.

MahD
  • 77
  • 9
  • Are you sure it doesn't have `LineWidth` or `EdgeColor`? [The documentation for R2015b](https://www.mathworks.com/help/releases/R2015b/matlab/ref/bar.html#namevaluepairs) states otherwise. This is also all explicitly addressed in the [9th example in the R2015b documentation](https://www.mathworks.com/help/releases/R2015b/matlab/ref/bar.html#bthxche). – sco1 May 17 '18 at 19:09
  • Yes, definitely. It is the error; No appropriate method, property, or field 'CData' for class 'matlab.graphics.chart.primitive.Bar'. Error in tt (line 5) b.CData(bi) = [0.4,0.4,0.4]; – MahD May 17 '18 at 19:13
  • and also for the others; No public property LineWidth exists for class matlab.graphics.GraphicsPlaceholder. Error in tt (line 5) b(bi).LineWidth = 2; – MahD May 17 '18 at 19:15
  • `matlab.graphics.GraphicsPlaceholder` means you're not properly addressing the output of `bar`. Please [edit] your post to contain a [mcve]. – sco1 May 17 '18 at 19:25
  • I made it. thank you – MahD May 17 '18 at 19:44
  • There is not supposed to be a 'CData' property. `b.LineWidth = 3` works as expected, as does `b.FaceColor = [0.4,0.4,0.4]` and `b.EdgeColor=[1,0,0]`. (R2017a, but following docs for R2015b linked by excaza.) – Cris Luengo May 17 '18 at 20:19
  • 1
    `b(bi).LineWidth = 2;` is nowhere in the code you've provided. Again, please read [mcve]. If you don't do this, we can't help you. – sco1 May 17 '18 at 21:01
  • `b` is a scalar, you can't index into it. By doing `b(bi)` you are adding "null" handles to the array `b`, which of course have no properties. – Cris Luengo May 17 '18 at 23:20
  • ok, what is the solution? – MahD May 18 '18 at 00:33

1 Answers1

2

(I just learned something new today!)

It seems that bar has two main modes of operation, producing different handle graphics object types. The style input argument selects the mode of operation:

  • bar(...,'grouped') or bar(...,'stacked') produces a Bar object. Note that grouped is the default style.

  • bar(...,'hist') or bar(...,'histc') produces a Patch object.

The documentation does not specify that the hist mode produces a different object type. In R2015b these same options existed, I presume the output types were the same as they are with my version of MATLAB (R2017a).

The Bar object produced by the first mode does not have a CData property. There is a FaceColor and EdgeColor property. See the Bar properties documentation for more information. But note that this is a single object, so you cannot index into the handle and set properties for individual bars. The properties control all bars at the same time:

b = bar(1:30); 
b.FaceColor = 'flat'; 
b.LineWidth = 2;
b.EdgeColor = 'red';

The exception is the XData and YData property, which have one value per bar.

The Patch object produced by the second mode does have a CData property. It's a bit more complex to manipulate, because the Patch has coordinates for each vertex and each edge. But the CData property can be set in different ways depending on your needs. Set to a Nx1 array (with N the number of bars) it gives an index into the color map for each bar (but see also the axis' Clim property for how that index is interpreted). Set to a Nx1x3 array it gives an RGB triplet for each bar. See the Patch properties documentation for more information. Here is an example:

b = bar(1:30,'hist'); 
b.FaceColor = 'flat'; 
b.LineWidth = 2;
b.EdgeColor = 'red';
cols = zeros(30,1,3)+0.5;
cols(5,1,:) = [1,0,0];
b.CData = cols;

[Credit to gnovice in this answer.]

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Thank you so much @Cris Luengo. I was wondering how I can set the FaceColor to e.x. blue? – MahD May 18 '18 at 15:59
  • If you want them all the same color, simply do `b.FaceColor='blue'`, If you want them all blue except for one, use the `hist` mode, and set `cols=zeros(30,1,3); cols(:,1,3)=1; cols(5,1,:)=[1,0,0]; b.CData=cols;` or something to that extent – Cris Luengo May 18 '18 at 16:03