Search code examples
matlabplotoctavematlab-figure

How to change the marker in a plot


my question is about this code:

w = linspace(-5,5,1000);
figure
for alpha = -1:0.2:1
    delay = (alpha.*cos(w)-alpha^2)./(1-2*alpha.*cos(w)+alpha^2);
    plot(w,delay)
    hold on
end
grid on
xlabel('$\omega$', 'interpreter', 'latex')

I'd like to know if it's possible to change the type of the graphic in each iteration. For example, once with circles ('o-'), another with diamons ('d-'), etc.

Thanks for your responses.


Solution

  • Try this:

    w = linspace(-5,5,1000);
    alpha = -1:0.2:1;
    shapes = '+o*.xsd^v><ph';
    figure, hold on
    for ii=1:numel(alpha)
        delay = (alpha(ii).*cos(w)-alpha(ii)^2)./(1-2*alpha(ii).*cos(w)+alpha(ii)^2);
        plot(w,delay,[shapes(ii),'-'])
    end
    grid on
    xlabel('$\omega$', 'interpreter', 'latex')
    

    In this plot, the points are so close together that you can't really make up the shapes. You can reduce the number of markers by plotting first the line without markers, then a subsampled version using only markers:

    w = linspace(-5,5,1000);
    alpha = -1:0.2:1;
    figure, hold on
    shapes = '+o*.xsd^v><ph';
    cols = jet(numel(alpha));
    for ii=1:numel(alpha)
        delay = (alpha(ii).*cos(w)-alpha(ii)^2)./(1-2*alpha(ii).*cos(w)+alpha(ii)^2);
        plot(w,delay,'-','color',cols(ii,:))
        plot(w(1:50:end),delay(1:50:end),shapes(ii),'color',cols(ii,:))
    end
    grid on
    xlabel('$\omega$', 'interpreter', 'latex')
    

    output of code above, a graph as described

    If the number of lines is larger than the number of available markers, the code above will throw an indexing error. You can cycle through the markers instead by using mod. Replace shapes(ii) with

    shapes(mod(ii-1,numel(shapes))+1)