How do I label lines in a MatLab plot?

Viewed 105

What my plot looks like

What the plot should look like

The code is working like it should but im trying to get the labels to show up on each line from (1-8). Just like the picture above.

I have read a bunch of posts and tried to search Matlab but i havent been able to figure it out.

clc;clear;close all;
V_inf = 20;                         % freestream velocity
R = 1;                              % cylinder radius
n = 8;                              % number of panels
d_theta = 2*pi/n;                   % resolution of angles
alpha = 0;                          % angle of attack
theta = pi+pi/n:-d_theta:-pi+pi/n;  % angles of boundary points of panels
X = R*cos(theta);                   % X coordinates of boundary points of panels
Y = R*sin(theta);                   % Y coordinates of boundary points of panels

Phi =   zeros(n,1);                 % angle from Vinf to bottom of panel
beta =  zeros(n,1);                 % angle from Vinf to outward normal of panel
conX =  zeros(n,1);                 % X coordinates of control points
conY =  zeros(n,1);                 % Y coordinates of control points
S =     zeros(n,1);                 % panel length
for i = 1:n
    Phi(i) = -alpha + atan2((Y(i+1)-Y(i)),(X(i+1)-X(i)));             
    beta(i) = Phi(i)+pi/2;  
    if beta(i)>2*pi, beta(i)=beta(i)-2*pi; 
    elseif beta(i)<0, beta(i)=beta(i)+2*pi; end
    conX(i) = (X(i+1)+X(i))/2;                                          
    conY(i) = (Y(i+1)+Y(i))/2;                                          
    S(i) = sqrt((X(i+1)-X(i))^2 + (Y(i+1)-Y(i))^2);                     
end


close all
plot(R*cos(0:0.01:2*pi),R*sin(0:0.01:2*pi),'b', X,Y,'r',conX,conY,'g^'); 
axis equal; legend('Exact shape','Panel approximation','Control points')
xlabel('x, m'); ylabel('y, m'); title('Fig. 1 Panel layout (n = 8, R = 1m)');
1 Answers

Possibly plotting the labels along the points of a circle using the text() function may suffice. There's some shifting of points and flipping that needs to be done to get the order you wish but otherwise it's just 8 points taken along a circle that is smaller in diameter in comparison to the octagon. An alternative would be using the green triangles as reference instead but that involves more math. As long as your octagon is expected to be symmetrical vertically and horizontally this should work alright.

clc;clear;close all;
V_inf = 20;                         % freestream velocity
R = 1;                              % cylinder radius
n = 8;                              % number of panels
d_theta = 2*pi/n;                   % resolution of angles
alpha = 0;                          % angle of attack
theta = pi+pi/n:-d_theta:-pi+pi/n;  % angles of boundary points of panels
X = R*cos(theta);                   % X coordinates of boundary points of panels
Y = R*sin(theta);                   % Y coordinates of boundary points of panels

Phi =   zeros(n,1);                 % angle from Vinf to bottom of panel
beta =  zeros(n,1);                 % angle from Vinf to outward normal of panel
conX =  zeros(n,1);                 % X coordinates of control points
conY =  zeros(n,1);                 % Y coordinates of control points
S =     zeros(n,1);                 % panel length
for i = 1:n
    Phi(i) = -alpha + atan2((Y(i+1)-Y(i)),(X(i+1)-X(i)));             
    beta(i) = Phi(i)+pi/2;  
    if beta(i)>2*pi, beta(i)=beta(i)-2*pi; 
    elseif beta(i)<0, beta(i)=beta(i)+2*pi; end
    conX(i) = (X(i+1)+X(i))/2;                                          
    conY(i) = (Y(i+1)+Y(i))/2;                                          
    S(i) = sqrt((X(i+1)-X(i))^2 + (Y(i+1)-Y(i))^2);                     
end


close all
plot(R*cos(0:0.01:2*pi),R*sin(0:0.01:2*pi),'b', X,Y,'r',conX,conY,'g^'); 
axis equal; legend('Exact shape','Panel approximation','Control points')
xlabel('x, m'); ylabel('y, m'); title('Fig. 1 Panel layout (n = 8, R = 1m)');

%*************************************************************************%
%ADDING LABELS BY PLOTTING LABELS ALONG CIRCLE%
%*************************************************************************%

Radius = 0.8;
Number_Of_Data_Points = 9;
theta = linspace(0,2*pi,Number_Of_Data_Points);
X_Circle = Radius*cos(theta);
X_Circle = X_Circle(1:end-1);
Y_Circle = Radius*sin(theta);
Y_Circle = Y_Circle(1:end-1);

X_Circle = flip(circshift(X_Circle,3));
Y_Circle = flip(circshift(Y_Circle,3));

for Point_Index = 1: numel(conX)
    
    X_Displacement = X_Circle(Point_Index);
    Y_Displacement = Y_Circle(Point_Index);

    text(X_Displacement,Y_Displacement,num2str(Point_Index),'HorizontalAlignment','center','fontsize',20);
end
    

To Plot on Control Points:

%*************************************************************************%
%ADDING LABELS BY PLOTTING LABELS ALONG CONTROL POINTS%
%*************************************************************************%
for Point_Index = 1: numel(conX)
    text(conX(Point_Index),conY(Point_Index),num2str(Point_Index),'HorizontalAlignment','center','fontsize',20);
end
Related