Matlab: formatting decimal places on outline labels
I want to reduce the number of decimal places in the following contour plot. I AM:
[cc,hh] = contour(X,Y,Z,levels,'LineColor','k');hold on
texth = clabel(cc,hh,'FontSize',8);
which gets me the first outline with long decals. Then, to strip out the decimals, I do:
for i = 1:size(texth); textstr=get(texth(i),'String'); textnum=str2double(textstr); textstrnew=sprintf('%0.0f', textnum) ; set(texth(i),'String',textstrnew); end
And that gives the second plot. As you can see, there is a large gap between the label and the contour lines, which looks terrible. Any ideas how to solve this?
Thank!
source to share
Instead of changing the result, create a contour plot with the levels you want, so you don't have to cheat on the data.
Define levels, for example. levels=997:1010
and then
contour(X,Y,Z,levels,'LineColor','k','ShowText','on');
Creates an outline plot with text included, and the levels, specifically those specified in the variable levels
, in this case 997,998,999, ..., 1009,1010
If, as @David suggests, your level variable is already a vector, then replace it with round(levels)
as he himself suggested.
source to share