Search code examples
matlabplotmatlab-figure

add parameters to the figure in Matlab


I have a plot in Matlab as following, I want to add some parameters to the figure like the figure which I have added. Could you help me with how to solve that? Thank you so much.

delta = 0.5;
I1=2;
I2 =4;
T = 5;
tau = 0.5;
b = 8;
a = 6;
c = 8;
d = 4;
t = [0:0.001:5];
y =(a-b*exp(-tau*t)).*(t>=0 & t<delta*T )+(-d+c*exp(-tau*(t-delta*T))).*(t>=delta*T & t<=T);
plot(t,y ,  'b',  'LineWidth',1.8)
hold on

x = [0:0.01:12];

max = 4*ones(1,1201);
min = -2*ones(1,1201);
z = 0*ones(1,1201);
plot(x,max ,  '--g',  'LineWidth',1)
hold on 
plot(x,min ,  '--g',  'LineWidth',1)

plot(2,min ,  '--g',  'LineWidth',1)
plot(x,z ,  'k',  'LineWidth',1.7)

axis([0 6 -4 6])

plot


Solution

  • I think this code might generate something close for what you are looking. It is important to say that the arrows for x and y can be added using the same "annotation command".

    delta = 0.5;
    I1=2;
    I2 =4;
    T = 5;
    tau = 0.5;
    b = 8;
    a = 6;
    c = 8;
    d = 4;
    t = [0:0.001:5];
    y =(a-b*exp(-tau*t)).*(t>=0 & t<delta*T )+(-d+c*exp(-tau*(t-delta*T))).*(t>=delta*T & t<=T);
    plot(t,y ,  'b',  'LineWidth',1.8,'DisplayName',"Data")
    % Change the y limits
    ylim([-4 6])
    hold on
    
    % %% Set the horizontal lines and its name in the y axis
    % MAX LINE - Create a yline Cte along the plot
    hLine = yline(4);
    hLine.LineWidth = 1;
    hLine.Color = "g";
    hLine.LineStyle = "--";
    hLine.DisplayName = "Max Line";
    % MIN LINE - Create a yline Cte along the plot
    hLine = yline(-2);
    hLine.LineWidth = 1;
    hLine.Color = "g";
    hLine.LineStyle = "--";
    hLine.DisplayName = "Min Line";
    % Zero LINE - Create a yline Cte along the plot
    hLine = yline(0);
    hLine.LineWidth = 1.7;
    hLine.Color = "k";
    hLine.LineStyle = "-";
    
    % Change the "name" in the y axis
    hAxis = gca;
    % Get index 
    %  - when y is equal to "Max" in this case 4
    %  - when y is equal to "MIN" in this case -2
    idx = hAxis.YTick == 4;
    hAxis.YTickLabel{idx} = 'X';
    idx = hAxis.YTick == -2;
    hAxis.YTickLabel{idx} = 'Y';
    
    % %% Set the vertical lines and its name in the x axis
    % Delta LINE - Create a xline Cte along the plot
    hLine = xline(2.5);
    hLine.LineWidth = 1;
    hLine.Color = "k";
    hLine.LineStyle = "-";
    % MIN LINE - Create a yline Cte along the plot
    hLine = xline(4);
    hLine.LineWidth = 1;
    hLine.Color = "k";
    hLine.LineStyle = "-";
    
    % Add the notes
    annotation('textbox',[.55 .2 .3 .3],"String","Delta",...
        'FitBoxToText','on','LineStyle','none')
    annotation('textbox',[0.8 0.2 .3 .3],"String","A",...
        'FitBoxToText','on','LineStyle','none')
    

    The previous code will generate the next figure.

    enter image description here