How can one adjust the format of the numbers displayed on a contour plot using clabel and a vector specifying the levels of the contours?

22 views (last 30 days)
I have Reduced my code to this simple form. My code actually calculates the vector V that sets the contours in an evenly spaced way no matter what. Well equal area way to be exact. How can I control the format of the numbers displayed in the labels? IE e,g,f, precision of 3 etc.... The code below provides the form XXXXX.XXXXXX but I want say x.xxe^x in the labels on the contour plot for example. I have tried to find a tick or label but didn't see them available in the get / set command for contour / clabel which is how I would have approached this for other axis objects like colorbar.
clear all, clc, format short eng, format compact,
a=linspace(0,100,100);
[X,Y] = meshgrid(a,a);
Z = X.^2 .*Y.^2;
V =[60.6005e+003 405.3483e+003 1.2596e+006 2.9207e+006 5.7763e+006,...
10.6068e+006 18.7857e+006 34.3230e+006];
figure(1)
clf
grid on, hold on,
[C,h] = contour(X,Y,Z,V,'-k');
clabel(C,h,V,'LabelSpacing',1000),
hold off,

Accepted Answer

Walter Roberson
Walter Roberson on 11 Oct 2012
Notice this part of clabel:
text_handles = clabel(...) returns the handles of text objects created by clabel. The UserData properties of the text objects contain the contour values displayed. If you call clabel without the h argument, text_handles also contains the handles of line objects used to create the '+' symbols.
So to get the format you want, call clabel and record the handles, then loop through the handles fetching the UserData, formatting that number as desired, and set() the String property of the text handle to the new string, possibly also setting the Interpreter property as well if you want to use LaTeX superpositioning for the exponents.
  1 Comment
Nathan
Nathan on 11 Oct 2012
Walter,
Thanks for the fast post! I implemented all your suggestions, but the LaTex one kinda through me. Since I am trying to do all this in an automated way I used the num2str method and I'm not sure how to also get the exponents looking nice like in Latex (Which I use extensively).
Here is my coding attempt:
text_handles=clabel(C,h,V,'LabelSpacing',1000);
[m n]=size(V);
for i=1:n,
a=get(text_handles(i),'UserData');
y=char(num2str(a,'%5.2e'));
set(text_handles(i),'String', y, 'FontSize',14);% ,'Interpreter','LaTex');
end
Changing from tex to LaTex just changed the fonts for me not the format...
Thanks again for the "kick" in the right direction.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!