Search code examples
matlabplotdrawingmatlab-figuredraw

How to draw circles in given places?


I want to draw a circle marker at the top of each peak on the following plot: plot

I achieve this with the following code:

data = mzcdfread('sample1.cdf');

[pks,locs,w,p] = findpeaks(data.ordinate_values)
plot(data.ordinate_values)

where

locs returns the x value at the top of each peak and pks returns the y value of each peak. A sample of locs and pks is given below:

pks =

  10×1 single column vector

    0.6649
    0.7314
    0.7536
    1.3797
    0.2394
    0.4322
    0.2090
    0.5402
    0.6797
    0.3059

locs =

        1199
        2399
        3599
        4799
        5999
        7199
        8399
        9599
       10799
       11999

This is what I tried to draw a circle marker at the top of each peak, but it does not work:

% r as radius
r = 0.2
ang=0:0.01:2*pi; 
xp=r*cos(ang);
yp=r*sin(ang);

plot(data.ordinate_values, locs+xp, pks+yp)

Solution

  • You should use the conventional plot() built-in function of Matlab with the 'Marker' option to draw the circles:

    plot(locs, pks, 'Marker', 'o');
    

    You can also set the size and color of the circle markers:

    
    plot(locs,pks,...
        'Color', 'r',...
        'Marker', 'o',...
        'MarkerSize', 14);
    
    

    This implies that the curve given by data.ordinate_values and the circles at each peak are plotted separately. Therefore, you need to use hold on between your two plot commands (or when you initialize the figure).

    figure;
    plot(data.ordinate_values);
    hold on;
    plot(locs,pks,...
        'Color', 'r',...
        'Marker', 'o',...
        'MarkerSize', 14);