How to change the number of digits in a clabelm?

3 views (last 30 days)
Hello,
I need to change the number of digits in a clabelm.
Specifically, from 800000000000 to 8.00e+11.
Thanks

Answers (1)

Anmol Dhiman
Anmol Dhiman on 30 Jan 2020
Hello Christopher,
Unfortunately, ”clabelm” cannot be used directly to achieve this functionality.
Unfortunately MATLAB updates the text everytime the plot is redrawn - so you need to add a listener to update them each time that happens - hence why you listen for this particular event.
A possible workaround is to use the undocumented “MarkedClean” event to get the intended behavior as shown in below example.
function ScientificNotation
close all
figure;
[X,Y,Z] = peaks;
Z = Z*10000000000;
[C,h] = contourm(X, Y, Z,5,'ShowText','on');
% add a listener and call your new format function
addlistener(h,'MarkedClean',@(a,b)ReFormatText(a))
end
function ReFormatText(h)
textObj = findobj(h.Children,'Type','Text');
for i=1:size(textObj,1)
% get all the text items from the contour
t = textObj(i).String;
%for ii=1:length(t)
% get the current value (Matlab changes this back when it redraws the plot)
v = str2double(t);
% Update with the format you want - scientific for example
textObj(i).String = sprintf('%0.5e',v); % here it is specified as 0.2e
end
end
More information on how to specify the format can be found on the link below:
Please note that as MarkedClean is undocumented, the behavior of it might change in a future release of MATLAB.
Hope it Helps

Categories

Find more on Just for fun 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!