Contour labels: Text handle output is not supported for managed labels.

16 views (last 30 days)
Hi all,
I am trying to change the labels of a contour plot and this used to be possible via a string modification in the clabel structure. However this ain't possible as far as I can see with the new graphic module introduced in MatLAB 2014b.
Testcase:
[x,y,z] = peaks;
[C,h] = contour(x,y,z);
hcl = clabel(C,h)
% now we change it arbitrarily per string:
for ii = 1:size(get(hcl),1)
set(hcl(ii),'String',num2str(ii,'%2.0f'));
end
MatLAB2014b or later will give the following warning:
Warning: Text handle output is not supported for managed labels.
> In clabel at 67
Where is the label information stored as strings that can be modified manually? Does anybody experienced the same and what was your solution?
Many thanks,
Erwin

Answers (1)

Matthias
Matthias on 1 Apr 2015
Edited: Matthias on 1 Apr 2015
I have exactly the same problem. I usually need logarithmic contour plots (which are not supported by MATLAB) and therefore I have to apply log() to my data and then adjust the colorbar and contour labels accordingly. With the new version of MATLAB the old workarounds are no longer applicable. To just get a quick result I used the following approach:
1. If you wish to access the text labels, you can no longer use the contour object "h" when calling "clabel". The MATLAB help on clabel shows, that you can get a the text and line object of clabel when you call "tl = clabel(C))", but this is no longer possible with "clabel(C,h)". So you will have to change your code there.
2. The structure of the return value in "tl" has changed when compared the the preivious version of MALTAB. Just look at the structure in the workspace once you have the data and you will see a graphics array with alternating entries for line and text. If you wish to alter the labels, you will have to alter the texts in this graphic array. I have the additional problem, that the use of log() and exp() leads to small rounding errors, so that the labels would look odd if simply used them. So I have to do some mapping. Here is the part of the code which works for me when trying to show a magnetic field:
if true
figure('Position',[20,20,950,800]); axis equal; grid on; hold on;
Contours=[0.0001 0.0003 0.0005 0.001 0.003 0.005 ...
0.01 0.03 0.0679 0.3 0.5 1 3 5];
colormap(jet);
[C,hc] = contourf(x_shape,y_shape,log(B_shape),log(Contours));
% Label only specified contours
lab = [0.003 0.0679];
th = clabel(C,log(lab),'Color','k');
% exch. linear with logarithmic labels
for i = 2:2:length(th)
% take only every second entry, because
% every first entry is line info
% every second is text
oldLabelText = get(th(i), 'String');
oldnum = str2double(oldLabelText);
oldnumexp = exp(oldnum);
% now clean the number from rounding errors
% due to log() and exp()
[trash,min_id] = min(abs(lab-oldnumexp));
newLabelText = num2str(lab(min_id)); % exchange the label
set(th(i), 'String', newLabelText);
end
% Adjust color range
caxis(log([Contours(1) Contours(end)]));
h=colorbar('YTickMode','manual',...
'YTick',log(Contours),...
'YTickLabel',Contours);
% caxis must be issued before YTick,
% it otherwise destroys the ticks, but not the labels
hcbt = get(h,'title'); % handle to colorbar title
set(hcbt,'String','|B| [T]') % set color bar title
...
xlabel('X in cm'); ylabel('Y in cm'); zlabel('Z in cm');
hold off; grid on;
set(gca,'layer','top'); % get the grid above the contour
title('Streufeld NPC1, 10 kA, Z = 0m');
end
I also try to attach a figure which shows the results. This is only a quick new workaround, there may be better codes. It would be perfect if MATLAB would some day have an inbuilt logarithmic contour plot.
Best regards,
Matthias

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!