MATLAB Answers

Control decimal places of contour labels

6 views (last 30 days)
Day Hong Kim
Day Hong Kim on 9 May 2021
Commented: Day Hong Kim on 11 May 2021
Hello all, hope you having a great day. I am trying to figure out how to control decimal places of contour labels. What I would like to do is that all the values have 1 decimal place. However, when the value is an integer, it does not show its decimal. For example, if the value is 10.0, the contour label only shows 10. If you have any idea and share it for me, I would be really thankful.

Accepted Answer

dpb on 9 May 2021
Edited: dpb on 9 May 2021
Yet another case where TMW (The MathWorks) has been too aggressive in hiding stuff that clearly users would want to and should be able to control...there is no 'format' or 'precision' property for contour labels exposed; in fact, there isn't one even in the hidden properties of the contour object.
There is an array of text objects that are the text written that can be munged on with some effort...
[~,hC]=contour(X,Y,Z,'ShowText','on'); % create contour plot from example doc, save handle
hTxt=hC.TextPrims; % array of text contour objects handles
isNoDec=find(~arrayfun(@(h)contains(h.String,'.'),hTxt)); % find those missing decimal
arrayfun(@(h)set(h,'String',strcat(h.String,'.0')),hTxt(isNoDec)) % and add a trailing ".0"
"Oh, the tangled web Handle Graphics weaves...when the vendor doth hide the leaves."
One needs Yair Altman's UNDCOUMENTED File Exchange submission to easily probe inside the hidden properties to be able find these goodies.
The above example with a tile layout produced the following figure--note the center line at zero on the lower subplot is "0.0" rather than "0" in the upper...
It's truly remarkable what effort is wasted having to clean up things for such inconsistencies. I've ranted about the same issue regarding the default formatting for the standard 0-1 axes that is the result of the defaultl '%g' format string -- at least it is fixable and one can change the default; these like this buried in the bowels and hidden are beyond ... one either just accepts it as "the way things are" or spends an hour or more digging around...
  1 Comment
Day Hong Kim
Day Hong Kim on 11 May 2021
Thank you for the information. It really helped me a lot. As you mentioned above, I agree that it is frustrating when just a small thing such as changing decimal places bothers you. Hope MATLAB updates for more user-friendly.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by