Labelling Curves on a Graph, This code worked a decade ago, but will not work in 2023a

This code worked fine a decade ago, but will not work in 2023a
X = [0 1100 1600 2400 2400 2800 3400 3800];
Y = fliplr(X);
figure
hold on
C(1, 1) = 100; % data value to plot on the line
C(2, 1) = length(X); % number of data points
C(1, 2 : length(X)+1) = X; % x data points for the line
C(2, 2 : length(X)+1) = Y; % y data points for the line
h_b = plot(X, Y, '-b', 'LineWidth', 2);
clabel(C, h_b, 'LabelSpacing', 1000)
It now results in
"Error using clabel
Not enough contour handles."
and it worked fine. It made a graph similar to:
Now the clabel command errors and I can't figure out how to fix it. Any help would be appreciated.

 Accepted Answer

Looking at the current clabel documentation, the second argument to it has to be the second output of the contour call. It is likely looking for information in ‘h_b’ that does not exist because it is a plot handle and not a contour handle. (MATLAB has changed significantly in the past decade.)
.

4 Comments

Hi Star Strider, Thanks for the reply! Any ideas on how to work around this, or to accomplish the same thing (putting in-line labels on line plots automatically without having to calculate the text coordinates)?
My pleasure!
The only aproach I can think of is to recalculate the contours using contour and then use its outputs. (It would probably also be necessary to significantly update the rest of the code.) The current online documentation only goes back to R2018b, and that is likely newer than the decade-old version you are using.
Aside from that, you could probably use the text function, however it would be necessary to write your own supporting code (calculating the rotation angles and such), as well as code to determine the positions of the text objects. (I chose the second and second-to-last coordinate positions here.)
Example —
X = [0 1100 1600 2400 2400 2800 3400 3800];
Y = fliplr(X);
figure
hold on
C(1, 1) = 100; % data value to plot on the line
C(2, 1) = length(X); % number of data points
C(1, 2 : length(X)+1) = X; % x data points for the line
C(2, 2 : length(X)+1) = Y; % y data points for the line
h_b = plot(X, Y, '-b', 'LineWidth', 2);
% clabel(C, h_b, 'LabelSpacing', 1000)
% a = atan2d(diff(Y), diff(X)); % Rotation Angles
a = atan2d(gradient(Y), gradient(X)); % Rotation Angles
kv = [2 7];
for k = 1:numel(kv)
text(X(kv(k)), Y(kv(k)), "100", 'Rotation',a(kv(k)), 'BackgroundColor','w', 'FontSize',14)
end
EDIT — (7 Jan 2024 at 15:45)
Substituted gradient for diff in the ‘a’ calculations. It gives a better result, and the output is the same size as the input, so the indices now match. (My apologies for not using it originally.)
.
That looks like it will work, Thanks for your help!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 6 Jan 2024

Edited:

on 7 Jan 2024

Community Treasure Hunt

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

Start Hunting!