How to put string labels on contours for contour sections in MATLAB

I am wondering if it is possible to mark the contours of a MATLAB contour with a set of user-defined strings?

I am currently using the following scheme to create a labeled outline:

%Create Data X = 0.01:0.01:0.10 Y = 0.01:0.01:0.10 Z = repmat(X.^2,length(X),1) + repmat(Y.^2,length(Y),1)'; %Create Plot hold on [C,h] = contourf(X,Y,Z); %Add + Format Labels to Plot hcl = clabel(C,h,'FontSize',10,'Color','k','Rotation',0); set(hcl,'BackgroundColor',[1 1 1],'EdgeColor',[0 0 0],'LineStyle','-',) hold off 

The problem with this code is that tags are automatically generated by MATLAB. Even if I can easily change the outlines that are labels, I cannot change the labels that they receive.

Ideally, I would like to label them with a set of lines that I define myself. However, if this is not possible, I wonder if it is possible to change the number format of the labels. The reason for this is because the code above actually creates a contour graph for the error rate, which I would like to display as the% value (i.e. use 1% on the contour label instead of 0.01, etc.).

+4
source share
1 answer

In this case, hcl is actually an array that stores the descriptors of each contour label on your graph. When you set properties using an array (as in your code),

 set(hcl, 'name', 'value') 

You set the property of each label to one value.

You can change the properties of individual labels, iterating over an array. For example, this is how you add a percent sign:

 for i = 1:length(hcl) oldLabelText = get(hcl(i), 'String'); percentage = str2double(oldLabelText)*100; newLabelText = [num2str(percentage) ' %']; set(hcl(i), 'String', newLabelText); end 
+3
source

Source: https://habr.com/ru/post/1482465/


All Articles