MATLAB Answers

How to display colorbar label with App Designer

93 views (last 30 days)
Hi,
I'm trying to display the colorbar label in an axis from App Designer but the text goes outside the limits and is not properly displayed as shown below:
The code I'm usig:
c = colorbar(app.UIAxes)
colormap(app.UIAxes, jet)
c.Label.String = 'Example Color Label';
Screenshot_2.png
How can I properly display the label?
Thanks.

  0 Comments

Sign in to comment.

Accepted Answer

Yogesh Khurana
Yogesh Khurana on 18 Jul 2019
Hi,
You can try editing axes font size to a value less than the default value that is 12. If you change the axes font size, then MATLAB automatically sets the font size of the colorbar to 90% of the axes font size. I have written the following code to change axes font size:
c = colorbar(app.UIAxes);
colormap(app.UIAxes, jet);
c.Label.String = 'Example Color Label';
app.UIAxes.FontSize = 10;
The output of above code is:
Another way is to set the position of colorbar manually using Position property of colorbar.
You can also refer to documentations of Axes properties and Colorbar properties:
Colorbar Properties:

  4 Comments

Show 1 older comment
Chris Oreinos
Chris Oreinos on 25 Sep 2019
I am suprised by two things here; (1) that this ad-hoc workaround was given as an official answer by MathWorks Staff, and (2), that this reply was accepted as an "answer".
So, the offerred "solution" maybe works in some cases. It is not universal. However, colorbar formatting should work out-of-the-box! As of R2019a versions, it only works for figures/axes but NOT for uifigures/uiaxes. Try it yourselves:
% Colorbar label appears fine
hFig = figure;
hAxes = axes(hFig);
imagesc(hAxes, magic(5))
hClr = colorbar(hAxes);
hClr.Label.String = 'Magic numbers';
% Colorbar label is cropped
hFig = uifigure;
hAxes = uiaxes(hFig);
imagesc(hAxes, magic(5))
hClr = colorbar(hAxes);
hClr.Label.String = 'Magic numbers';
Mathworks seems to be promoting the transition to App-designer (as opposed to the old GUIDE), which goes hand-in-hand with the replacement of figure/axes by uifigures/uiaxes. However, it hasn't even tested its built-in functions against the new framework. Let alone provide the customization possible in the "old" figures/axes world.
Please provide a real solution to the cropped colorbar labels. Also, the ability to have the label on top of the colorbar, as was the case in older MATLAB versions (by using the XLable property of the underlying colorbar axes), is something that should also be provided now.
Chris Oreinos
Chris Oreinos on 14 Oct 2019
I contacted the Mathwors support and was given the following 2, still not-satisfying workarounds:
% Workaround #1
% Move the colorbar y axis label to the left.
hFig = uifigure;
hAxes = uiaxes(hFig);
imagesc(hAxes, magic(5))
hClr = colorbar(hAxes);
hClr.Label.String = 'Magic numbers';
hClr.AxisLocation = 'in';
% Workaround #2
% Expand the width of the outer position of the axes a bit
hFig = uifigure;
hAxes = uiaxes(hFig);
imagesc(hAxes, magic(5))
hClr = colorbar(hAxes);
hClr.Label.String = 'Magic numbers';
hAxes.OuterPosition(3) = 500; % Change this value to fit your needs
On a positive note though, they confirmed that this is a known bug for their development team. Nonetheless, there was no time frame for solving that bug.

Sign in to comment.

More Answers (0)